diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 63fbe967..5ea40f63 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -460,7 +460,17 @@ def nested2properties(self, field: marshmallow.fields.Field, ret) -> dict: field, marshmallow.fields.Pluck ): schema_dict = self.resolve_nested_schema(field.schema) # type:ignore - if ret and "$ref" in schema_dict: + if ( + ret + and "$ref" in schema_dict + and ( + self.openapi_version.major < 3 + or ( + self.openapi_version.major == 3 + and self.openapi_version.minor == 0 + ) + ) + ): ret.update({"allOf": [schema_dict]}) else: ret.update(schema_dict) diff --git a/tests/test_ext_marshmallow_field.py b/tests/test_ext_marshmallow_field.py index 5e5250b0..837cf0d3 100644 --- a/tests/test_ext_marshmallow_field.py +++ b/tests/test_ext_marshmallow_field.py @@ -387,10 +387,13 @@ class MyEnum(Enum): assert ret["enum"] == [1, 2, None] +@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) def test_field2property_nested_spec_metadatas(spec_fixture): - spec_fixture.spec.components.schema("Category", schema=CategorySchema) + class Child(Schema): + name = fields.Str() + category = fields.Nested( - CategorySchema, + Child, metadata={ "description": "A category", "invalid_property": "not in the result", @@ -398,11 +401,29 @@ def test_field2property_nested_spec_metadatas(spec_fixture): }, ) result = spec_fixture.openapi.field2property(category) - assert result == { - "allOf": [build_ref(spec_fixture.spec, "schema", "Category")], - "description": "A category", - "x-extension": "A great extension", - } + version = spec_fixture.openapi.openapi_version + if version.major < 3: + assert result == { + "allOf": [ + {"$ref": "#/definitions/Child"}, + ], + "description": "A category", + "x-extension": "A great extension", + } + elif version.minor < 1: + assert result == { + "allOf": [ + {"$ref": "#/components/schemas/Child"}, + ], + "description": "A category", + "x-extension": "A great extension", + } + else: + assert result == { + "$ref": "#/components/schemas/Child", + "description": "A category", + "x-extension": "A great extension", + } def test_field2property_nested_spec(spec_fixture):