Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): generate OpenAPI v3 swagger docs #5259

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Akuukis
Copy link
Contributor

@Akuukis Akuukis commented Nov 13, 2024

🗒️ Checklist

  1. run linter locally
  2. update all related docs (API, README, inline, etc.), if any
  3. draft PR with a title <type>(<scope>)<!>: <title> TASK-1234
  4. tag PR: at least frontend or backend unless it's global
  5. fill in the template below and delete template comments
  6. review thyself: read the diff and repro the preview as written
  7. open PR & confirm that CI passes
  8. request reviewers, if needed
  9. delete this section before merging

📣 Summary

Automatically generate and display API schema.

📖 Description

Automatically generate OpenAPI v3 schema and display it in both swagger and redoc. See urls:

  • /api/schema/swagger
  • /api/schema/redoc

👀 Preview steps

  1. ℹ️ note that python dependencies has changed
  2. open /api/schema
  3. 🟢 notice that a schema downloads
  4. open /api/schema/swagger
  5. 🟢 notice that it's a swagger docs of the API schema
  6. open /api/schema/redoc
  7. 🟢 notice that it's a redoc docs of the API schema

💭 Notes

The schema most likely is not complete nor accurate. The scope of this PR is auto-generate schema as-is as a starting point, to enable iteration and improvement of the schema over time.

The purpose of auto-generated schema is twofold:

  • use it as basis for auto-generating API types in typescript for the frontend. That's out of scope for another PR.
  • a critical part of the automated testing architecture, see internal proposal here.

I considered three options for schema generation:

Reviewers, please help me to pack this properly for merging, this PR is rough like a quick PoC.

Screenshots of swagger and redoc:

image
image

@@ -58,7 +58,7 @@ def filter_renderers(self, renderers, format):

class MediaFileRenderer(BaseRenderer):
media_type = '*/*'
format = None
format = 'TODO'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drf_spectacular requires format to be a string, otherwise it crashes. What would the right format here?

@@ -24,7 +24,7 @@ class AssetJsonRenderer(renderers.JSONRenderer):

class MediaFileRenderer(renderers.BaseRenderer):
media_type = '*/*'
format = None
format = 'TODO'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drf_spectacular requires format to be a string, otherwise it crashes. What would the right format here?

Comment on lines +23 to +33
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
path(
'api/schema/swagger/',
SpectacularSwaggerView.as_view(url_name='schema'),
name='swagger-ui',
),
path(
'api/schema/redoc/',
SpectacularRedocView.as_view(url_name='schema'),
name='redoc',
),
Copy link
Contributor Author

@Akuukis Akuukis Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied the example from docs for a quick PoC. Questions:

  1. what api path you'd like?
  2. is this the right urls.py to add spectacular to urls?
  3. let's use swagger or redoc, or both? My preference is swagger.

@Akuukis Akuukis force-pushed the kalvis/swagger branch 3 times, most recently from 6f27d32 to c242689 Compare November 13, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants