Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of metadata when using OAS 3.1 #1139

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,3 @@ With that out of the way, we hope to hear from you soon.
.. _blueprints: https://drf-spectacular.readthedocs.io/en/latest/blueprints.html
.. _early feedback: https://github.com/tfranzel/drf-spectacular/issues
.. _test_regressions.py: https://github.com/tfranzel/drf-spectacular/blob/master/tests/test_regressions.py



2 changes: 1 addition & 1 deletion drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def append_meta(schema: _SchemaType, meta: _SchemaType) -> _SchemaType:
schema = {'oneOf': [schema, {'type': 'null'}]}
elif len(schema) == 1 and 'oneOf' in schema:
schema['oneOf'].append({'type': 'null'})
elif not schema and not meta:
elif not schema:
schema = {'oneOf': [{}, {'type': 'null'}]}
else:
assert False, 'Invalid nullable case' # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3266,7 +3266,7 @@ def view_func(request, format=None):
@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_basic_oas_3_1_nullable_cases(no_warnings, django_transforms):
class M14(models.Model):
field_json = models.JSONField(null=True) # case 1
field_json = models.JSONField(null=True, help_text="field_json desc") # case 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind adding a new field instead of changing the existing one. we'll get more coverage that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing it, hadn't had time to do it


class XSerializer(serializers.ModelSerializer):

Expand All @@ -3286,7 +3286,7 @@ class XViewset(viewsets.ReadOnlyModelViewSet):
schema = generate_schema('m2', XViewset)
assert schema['components']['schemas']['X']['properties'] == {
'id': {'readOnly': True, 'type': 'integer'},
'field_json': {'oneOf': [{}, {'type': 'null'}]},
'field_json': {'oneOf': [{}, {'type': 'null'}], 'description': 'field_json desc'},
'field_method_hint': {
'oneOf': [
{'type': 'integer'},
Expand Down