Skip to content

feat: auto-generate OpenAPI schema with drf-spectacular#55

Open
Kroenenn wants to merge 1 commit intomainfrom
feat/openapi-spectacular
Open

feat: auto-generate OpenAPI schema with drf-spectacular#55
Kroenenn wants to merge 1 commit intomainfrom
feat/openapi-spectacular

Conversation

@Kroenenn
Copy link
Copy Markdown
Contributor

Summary

  • Wire up SpectacularAPIView so the OpenAPI schema is generated automatically from DRF code
  • Enrich SPECTACULAR_SETTINGS with version, description, and generation options
  • Add @extend_schema decorators to RunViewSet for the two critical endpoints (POST /run, PATCH /run/<id>)
  • Fix GeoFeatureModelSerializer crash during schema generation

Changes

backend/api/urls.py

Replaced the docs/schema/ path from a static file handler (get_schema) to SpectacularAPIView. Previously, the endpoint served a hardcoded realtime.yml file. Now Spectacular introspects the registered ViewSets and generates a live OpenAPI YAML automatically. The Redoc UI (docs/) already pointed to url_name="schema", so it picks up the generated schema with no other change.

backend/api/views.py

Removed the now-unused get_schema function (it served the static file that no longer needs a handler). Added @extend_schema decorators to RunViewSet.create and RunViewSet.partial_update:

  • create needed this because it returns {"id": ...} instead of the full serializer — Spectacular cannot infer that on its own.
  • partial_update (the end_run endpoint) needed a human-readable summary and description to make the intent clear in the docs.

backend/databus/settings.py

Extended SPECTACULAR_SETTINGS from 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. id is read-only on Run).

backend/api/serializers.py

Added id_field = None to GeoStopSerializer.Meta and GeoShapeSerializer.Meta. GeoFeatureModelSerializer (from djangorestframework-gis) promotes the id_field to the GeoJSON Feature.id level, which removes it from the regular serializer fields. When Spectacular's GIS extension then tries to pop that field from the generated properties dict, it is already gone, causing a KeyError: 'id' crash that aborted the entire schema generation. Setting id_field = None tells the serializer not to promote any field, so the field stays in properties and Spectacular never tries to pop it.

@Kroenenn Kroenenn requested a review from fabianabarca April 26, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant