-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #580 from jennydaman/openapi-delete-collectionjson
Remove all vnd.collection+json content from OpenAPI spec
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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'] |
This file contains 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