Skip to content

Commit

Permalink
add spectacular hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed Dec 6, 2024
1 parent 3ce5c6b commit 8adeebb
Show file tree
Hide file tree
Showing 4 changed files with 599 additions and 559 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/oas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ jobs:


steps:
- uses: actions/checkout@v4 # the artifact generated in the previous job has errors which I have not been able to fix, currently removing the culprit manually when generating spec locally.
- name: Download generated OAS
uses: actions/download-artifact@v4
with:
name: open_producten-oas
- uses: actions/setup-node@v4
with:
node-version: '20'
Expand All @@ -157,13 +160,13 @@ jobs:
- name: Validate schema
run: |
openapi-generator-cli \
validate -i src/${{matrix.base_endpoint}}-open.yaml
validate -i ${{matrix.base_endpoint}}-openapi.yaml
- name: Generate Java client
run: |
openapi-generator-cli \
generate \
-i src/openapi.yaml \
-i ${{matrix.base_endpoint}}-openapi.yaml \
--global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
-o ./sdks/java \
-g java \
Expand All @@ -173,7 +176,7 @@ jobs:
run: |
openapi-generator-cli \
generate \
-i src/openapi.yaml \
-i ${{matrix.base_endpoint}}-openapi.yaml \
--global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
-o ./sdks/net \
-g csharp \
Expand All @@ -183,7 +186,7 @@ jobs:
run: |
openapi-generator-cli \
generate \
-i src/openapi.yaml \
-i ${{matrix.base_endpoint}}-openapi.yaml \
--global-property=modelTests=false,apiTests=false,modelDocs=false,apiDocs=false \
-o ./sdks/python \
-g python \
Expand Down
4 changes: 4 additions & 0 deletions src/open_producten/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
"SWAGGER_UI_FAVICON_HREF": "SIDECAR",
"REDOC_DIST": "SIDECAR",
"SERVE_INCLUDE_SCHEMA": False,
"POSTPROCESSING_HOOKS": (
"drf_spectacular.hooks.postprocess_schema_enums",
"open_producten.utils.spectacular_hooks.custom_postprocessing_hook",
),
}

#
Expand Down
31 changes: 31 additions & 0 deletions src/open_producten/utils/spectacular_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from sys import stdout


def custom_postprocessing_hook(result, generator, request, public):
"""
DRF Spectacular adds default = [] to the item inside an array on PrimaryKeyRelatedField fields with many=True like
`onderwerp_ids = serializers.PrimaryKeyRelatedField(
many=True,
write_only=True,
queryset=Onderwerp.objects.all(),
source="onderwerpen",
)`
(src/open_producten/producttypen/serializers/producttype.py)
"""

schemas = result.get("components", {}).get("schemas", {})

for schema_key, schema in schemas.items():
properties = schema.get("properties", {})

for prop_key, prop in properties.items():
if prop_key.endswith("_ids"):
stdout.write(f"removed default=[] from {schema_key}.{prop_key}\n")
items = prop.get("items", {})
items.pop("default", None)
items.pop("writeOnly", None)

return result
Loading

0 comments on commit 8adeebb

Please sign in to comment.