Skip to content

Commit

Permalink
Merge pull request #958 from luhn/allow-properties-with-refs
Browse files Browse the repository at this point in the history
Allow properties on $ref objects for OpenAPI 3.1.
  • Loading branch information
lafrech authored Dec 2, 2024
2 parents 7e5f5ed + a942edd commit a52e7bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/apispec/ext/marshmallow/field_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,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)
Expand Down
35 changes: 28 additions & 7 deletions tests/test_ext_marshmallow_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,43 @@ 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",
"x_extension": "A great extension",
},
)
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):
Expand Down

0 comments on commit a52e7bc

Please sign in to comment.