feat: auto-generate OpenAPI schema with drf-spectacular#55
Open
feat: auto-generate OpenAPI schema with drf-spectacular#55
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SpectacularAPIViewso the OpenAPI schema is generated automatically from DRF codeSPECTACULAR_SETTINGSwith version, description, and generation options@extend_schemadecorators toRunViewSetfor the two critical endpoints (POST /run,PATCH /run/<id>)GeoFeatureModelSerializercrash during schema generationChanges
backend/api/urls.pyReplaced the
docs/schema/path from a static file handler (get_schema) toSpectacularAPIView. Previously, the endpoint served a hardcodedrealtime.ymlfile. Now Spectacular introspects the registered ViewSets and generates a live OpenAPI YAML automatically. The Redoc UI (docs/) already pointed tourl_name="schema", so it picks up the generated schema with no other change.backend/api/views.pyRemoved the now-unused
get_schemafunction (it served the static file that no longer needs a handler). Added@extend_schemadecorators toRunViewSet.createandRunViewSet.partial_update:createneeded this because it returns{"id": ...}instead of the full serializer — Spectacular cannot infer that on its own.partial_update(theend_runendpoint) needed a human-readable summary and description to make the intent clear in the docs.backend/databus/settings.pyExtended
SPECTACULAR_SETTINGSfrom just a title to a complete configuration block:VERSION: required for a valid OpenAPI document.DESCRIPTION: explains what the API is for.SERVE_INCLUDE_SCHEMA: prevents the schema endpoint itself from appearing in the schema.COMPONENT_SPLIT_REQUEST: generates separate request/response schemas for the same serializer, which is necessary when read and write fields differ (e.g.idis read-only onRun).backend/api/serializers.pyAdded
id_field = NonetoGeoStopSerializer.MetaandGeoShapeSerializer.Meta.GeoFeatureModelSerializer(fromdjangorestframework-gis) promotes theid_fieldto the GeoJSONFeature.idlevel, which removes it from the regular serializer fields. When Spectacular's GIS extension then tries to pop that field from the generatedpropertiesdict, it is already gone, causing aKeyError: 'id'crash that aborted the entire schema generation. Settingid_field = Nonetells the serializer not to promote any field, so the field stays inpropertiesand Spectacular never tries to pop it.