From 117bd4495a3a8f555ed0486ceec95c1e5c22b347 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 3 Jan 2024 16:41:03 +0100 Subject: [PATCH 1/3] Fix handling of metadata when using OAS 3.1 --- drf_spectacular/plumbing.py | 2 +- tests/test_regressions.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drf_spectacular/plumbing.py b/drf_spectacular/plumbing.py index 1595271b..249083e0 100644 --- a/drf_spectacular/plumbing.py +++ b/drf_spectacular/plumbing.py @@ -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 diff --git a/tests/test_regressions.py b/tests/test_regressions.py index b056f560..9d375e12 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -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 class XSerializer(serializers.ModelSerializer): @@ -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'}, From 1e28d3e51301c36549bc3ee402337fc11fb858d4 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 3 Jan 2024 16:41:10 +0100 Subject: [PATCH 2/3] Remove empty lines --- CONTRIBUTING.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 674cac59..e2890665 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -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 - - - From 691ac461142041dc82a4ec9364ac665981e6b730 Mon Sep 17 00:00:00 2001 From: "T. Franzel" Date: Sat, 6 Jan 2024 14:16:03 +0100 Subject: [PATCH 3/3] differentiate test cases for 3.1 null cases #1139 --- tests/test_regressions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 9d375e12..0e33b356 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -3266,7 +3266,8 @@ 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, help_text="field_json desc") # case 1 + field_json = models.JSONField(null=True) # case 1 + field_json2 = models.JSONField(null=True, help_text="field_json desc") # case 3 class XSerializer(serializers.ModelSerializer): @@ -3286,7 +3287,8 @@ 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'}], 'description': 'field_json desc'}, + 'field_json': {'oneOf': [{}, {'type': 'null'}]}, + 'field_json2': {'oneOf': [{}, {'type': 'null'}], 'description': 'field_json desc'}, 'field_method_hint': { 'oneOf': [ {'type': 'integer'},