apispec icon indicating copy to clipboard operation
apispec copied to clipboard

Components are not referenced when combined with oneOf, anyOf, allOf, not

Open lfiedler opened this issue 6 years ago • 10 comments

When combining multiple schemas with oneOf, anyOf, allOf or not that where previously added to components, they will not get referenced. I.e. running

from pprint import pprint

from apispec import APISpec

spec = APISpec(
    title="Gisty",
    version="1.0.0",
    openapi_version="3.0.2",
    info=dict(description="A minimal gist API"),
)

spec.components.schema(
    "Gist",
    {
        "properties": {
            "id": {"type": "integer", "format": "int64"},
            "name": {"type": "string"},
        }
    },
)

spec.components.schema(
    "AnotherGist",
    {
        "properties": {
            "id": {"type": "integer", "format": "int64"},
            "name": {"type": "string"},
            "age": {"type": "integer", "format": "int32"},
        }
    },
)

spec.path(
    path="/gist/{gist_id}",
    operations=dict(
        get=dict(
            responses={
                "200": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "oneOf": [
                                    "Gist", "AnotherGist"
                                ]
                            }
                        }
                    }
                }
            }
        )
    ),
)

if __name__ == "__main__": 
    pprint(spec.to_dict())

I get

{'components': {'schemas': {'AnotherGist': {'properties': {'age': {'format': 'int32',
                                                                   'type': 'integer'},
                                                           'id': {'format': 'int64',
                                                                  'type': 'integer'},
                                                           'name': {'type': 'string'}}},
                            'Gist': {'properties': {'id': {'format': 'int64',
                                                           'type': 'integer'},
                                                    'name': {'type': 'string'}}}}},
 'info': {'description': 'A minimal gist API',
          'title': 'Gisty',
          'version': '1.0.0'},
 'openapi': '3.0.2',
 'paths': OrderedDict([('/gist/{gist_id}',
                        {'get': {'responses': OrderedDict([('200',
                                                            {'content': {'application/json': {'schema': {'oneOf': ['Gist',
                                                                                                                   'AnotherGist']}}}})])}})])}

whereas I expected that components would be referenced automatically, i.e.

{'components': {'schemas': {'AnotherGist': {'properties': {'age': {'format': 'int32',
                                                                   'type': 'integer'},
                                                           'id': {'format': 'int64',
                                                                  'type': 'integer'},
                                                           'name': {'type': 'string'}}},
                            'Gist': {'properties': {'id': {'format': 'int64',
                                                           'type': 'integer'},
                                                    'name': {'type': 'string'}}}}},
 'info': {'description': 'A minimal gist API',
          'title': 'Gisty',
          'version': '1.0.0'},
 'openapi': '3.0.2',
 'paths': OrderedDict([('/gist/{gist_id}',
                        {'get': {'responses': OrderedDict([('200',
                                                            {'content': {'application/json': {'schema': {'oneOf': [{'$ref': '#/components/schemas/Gist'},
                                                                                                                   {'$ref': '#/components/schemas/AnotherGist'}]}}}})])}})])}

The expected result can be generated with a workaround using spec.get_ref as mentioned in #550
Analog results for anyOf, allOf and not.

(version 3.3.0 on python 3.7)

lfiedler avatar Apr 25 '20 11:04 lfiedler

APISpec is not currently attempting to resolve schemas in oneOf, anyOf, allOf or not.

When doing one of polymorphism I recommend using marshmallow-oneofschema for the schema and apispec-oneofschema for documentation.

Bangertm avatar Apr 27 '20 13:04 Bangertm

Related to #391.

lafrech avatar Apr 27 '20 14:04 lafrech

Thank you for the reply and hinting me to the packages. I have not been aware of them and in principle they would aid with this (at least for the oneOf case). But as it stands for my use case, I'd rather like to avoid having to take such a detour. Especially since this would mean defining an additional marshmallow schema wrapping my existing schemas using a third party library, and then using another third party library to make it work with apispec. For me this seems like too much of an artificial overhead, especially if I simply can explicitly set the reference by hand. Also I'm not sure, if these packages can handle other cases than oneOf.

Anyhow, it would be great if apispec could handle this automatically. This would eliminate some redundant pieces of code and make the behaviour much more consistent.

As for the issue referenced by @lafrech, which seems to address this issue, is this still actively worked on and will it be included in the main branch at any time?

lfiedler avatar Apr 27 '20 18:04 lfiedler

As for the issue referenced by @lafrech, which seems to address this issue, is this still actively worked on and will it be included in the main branch at any time?

It is not actively worked on but feel free to take it over if you wish. The change is rather simple. It just needs at least a test.

lafrech avatar Apr 27 '20 19:04 lafrech

Sure, I'll give it a try.

lfiedler avatar May 01 '20 10:05 lfiedler

I think I got a bit confused there earlier. I now had a closer look at the APISpec.operations, and additionally at issue #391. In the latter a similar problem when handling marshmallow schemas is adressed. However, my example did not use marshmallow schemas at all, but was merely working with openapi schemas described by dictionaries. So I think it is more related to what was addressed in #554 and should be fixed in core as well.

If that was what @lafrech meant, than its fine. I just want to make sure we are talking about the same thing. Also, if it is so as I think it is, I could implement the appropriate behaviour after #554 is avaliable in dev branch.

lfiedler avatar May 01 '20 12:05 lfiedler

You're right. The PR I pointed to is related, as it achieves the same goal, but it applies to marshmallow schemas so it won't fix your issue. Sorry about the confusion.

And yes, I think you're right about #554 being a prerequisite to what you want to achieve.

@Bangertm I wouldn't mind another pair of eyes double-checking #554. Meanwhile, @lfiedler, you may try to draft something based on it.

lafrech avatar May 01 '20 14:05 lafrech

I created a draft in #560. Feel free to comment.

lfiedler avatar May 09 '20 19:05 lfiedler

@lafrech is there any activity? also get in trouble with apispec in such example:

post:
  summary: Create pet
  requestBody:
    description: How to create pet
    required: true
    content:
      application/json:
        schema:
          oneOf:
            - pet.schemas.CreateDogSchema
            - pet.schemas.CreateCatSchema
            - pet.schemas.CreateParrotSchema

I read topic and understood about using marshmallow-oneofschema for the schema and apispec-oneofschema for documentation. But i want to know is it actual rule to use additional packages (i.e marshmallow-oneofschem, apispec-oneofschema) or is anything better?

SomeAkk avatar Jun 28 '21 11:06 SomeAkk

I believe this was fixed in #671.

The part involving marshmallow schemas is still to be done, however.

lafrech avatar Jul 26 '21 15:07 lafrech