Skip to content

Commit

Permalink
fix: override serialization of OpenAPI schema
Browse files Browse the repository at this point in the history
to fix the ref error of the JS generator we manually create
a component for work_type facet that we only reference in the
route.
  • Loading branch information
sennierer committed Dec 9, 2024
1 parent 87e6c29 commit 925ff2f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
18 changes: 1 addition & 17 deletions apis_ontology/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,7 @@ def get_paginated_response_schema(self, schema):
"work_type": {
"type": "array",
"nullable": True,
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
},
"key": {
"type": "string",
},
"count": {
"type": "integer",
},
"children": {
"$ref": "#/components/schemas/PaginatedWorkPreviewList/properties/facets/properties/work_type"
},
},
},
"items": {"$ref": "#/components/schemas/WorkTypeFacet"},
"example": [
{
"id": 4,
Expand Down
3 changes: 3 additions & 0 deletions apis_ontology/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ class ApisOntologyConfig(AppConfig):
"""

name = "apis_ontology"

def ready(self):
import apis_ontology.schema # noqa: E401

Check failure on line 17 in apis_ontology/apps.py

View workflow job for this annotation

GitHub Actions / ruff-linter / ruff

Ruff (F401)

apis_ontology/apps.py:17:16: F401 `apis_ontology.schema` imported but unused
16 changes: 16 additions & 0 deletions apis_ontology/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def postprocess_facets(result, generator, request, public):
work_type_facet_component = {
"type": "object",
"required": ["id", "key", "count"],
"properties": {
"id": {"type": "integer"},
"key": {"type": "string"},
"count": {"type": "integer"},
"children": {
"type": "array",
"items": {"$ref": "#/components/schemas/WorkTypeFacet"},
},
},
}
result["components"]["schemas"]["WorkTypeFacet"] = work_type_facet_component
return result
4 changes: 4 additions & 0 deletions apis_ontology/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
SPECTACULAR_SETTINGS["DEFAULT_GENERATOR_CLASS"] = (
"apis_core.generic.generators.CustomSchemaGenerator"
)
SPECTACULAR_SETTINGS["POSTPROCESSING_HOOKS"] = [
"drf_spectacular.hooks.postprocess_schema_enums",
"apis_ontology.schema.postprocess_facets",
]


# Database
Expand Down

0 comments on commit 925ff2f

Please sign in to comment.