Skip to content

Commit

Permalink
Merge pull request #580 from jennydaman/openapi-delete-collectionjson
Browse files Browse the repository at this point in the history
Remove all vnd.collection+json content from OpenAPI spec
  • Loading branch information
jennydaman authored Oct 1, 2024
2 parents 51bc343 + ff9aa57 commit 5ca0271
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
32 changes: 32 additions & 0 deletions chris_backend/collectionjson/spectacular_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Any

def postprocess_remove_collectionjson(result, **_kwargs):
"""
Delete all `vnd.collection+json` content types from the given `result`.
:param result: an OpenAPI specification
"""
for path in result['paths'].values():
for operation in path.values():
if 'parameters' in operation:
operation['parameters'] = [p for p in operation['parameters'] if not _is_format_qs(p)]
_del_collectionjson_content(operation.get('requestBody', {}))
for response in operation.get('responses', {}).values():
_del_collectionjson_content(response)
return result


def _is_format_qs(parameter: dict[str, Any]):
return (
parameter.get('in', None) == 'query'
and parameter.get('name', None) == 'format'
and parameter.get('schema', {}).get('type', None) == 'string'
and set(parameter.get('schema', {}).get('enum', [])) == {'collection+json', 'json'}
)


def _del_collectionjson_content(x):
if 'content' not in x:
return
if 'application/vnd.collection+json' in x['content']:
del x['content']['application/vnd.collection+json']
6 changes: 5 additions & 1 deletion chris_backend/config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@
'SERVE_INCLUDE_SCHEMA': True,
'COMPONENT_SPLIT_REQUEST': env.bool("SPECTACULAR_SPLIT_REQUEST", False),
'PREPROCESSING_HOOKS': [
'drf_spectacular.hooks.preprocess_exclude_path_format'
'drf_spectacular.hooks.preprocess_exclude_path_format',
],
'POSTPROCESSING_HOOKS': [
'drf_spectacular.hooks.postprocess_schema_enums',
'collectionjson.spectacular_hooks.postprocess_remove_collectionjson'
],

'SCHEMA_PATH_PREFIX': '/api/v1/',
Expand Down

0 comments on commit 5ca0271

Please sign in to comment.