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

Incorrect handling of nested schemas where nullable=True and has description #955

Open
luhn opened this issue Nov 13, 2024 · 2 comments
Open

Comments

@luhn
Copy link
Contributor

luhn commented Nov 13, 2024

#953 changed how nullable nested schemas were handled, to fix #952.

It seems that the old behavior is still present if you set a description in the metadata field.

from apispec import APISpec

from apispec.ext.marshmallow import MarshmallowPlugin
from marshmallow import Schema, fields

spec = APISpec(
    title="Test",
    version="1.0.0",
    openapi_version="3.0.2",
    plugins=[MarshmallowPlugin()],
)


class NestedSchema(Schema):
    id = fields.Int()


class TestSchema(Schema):
    id = fields.Int()
    nested = fields.Nested(NestedSchema, allow_none=True)
    nested_with_description = fields.Nested(
        NestedSchema,
        allow_none=True,
        metadata={'description': 'Foobar'},
    )


spec.components.schema("Test", schema=TestSchema)
print(spec.to_yaml())

Output:

paths: {}
info:
  title: Test
  version: 1.0.0
openapi: 3.0.2
components:
  schemas:
    Nested:
      type: object
      properties:
        id:
          type: integer
    Test:
      type: object
      properties:
        id:
          type: integer
        nested:
          anyOf:
          - type: object
            nullable: true
          - $ref: '#/components/schemas/Nested'
        nested_with_description:
          description: Foobar
          allOf:
          - $ref: '#/components/schemas/Nested'
          nullable: true

I would expect that nested and nested_with_description would be identical except for the description field, but you can see that nested_with_description has the behavior that was deemed incorrect in #952.

@luhn
Copy link
Contributor Author

luhn commented Nov 13, 2024

Interestingly if I bump the OpenAPI version to 3.1.1, it doesn't appear to have the same issue.

paths: {}
info:
  title: Test
  version: 1.0.0
openapi: 3.1.1
components:
  schemas:
    Nested:
      type: object
      properties:
        id:
          type: integer
    Test:
      type: object
      properties:
        id:
          type: integer
        nested:
          anyOf:
          - $ref: '#/components/schemas/Nested'
          - type: 'null'
        nested_with_description:
          description: Foobar
          anyOf:
          - $ref: '#/components/schemas/Nested'
          - type: 'null'

@lafrech
Copy link
Member

lafrech commented Nov 14, 2024

Thanks for reporting. Would you like to investigate this and propose a fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants