apispec icon indicating copy to clipboard operation
apispec copied to clipboard

Sample does not reference components

Open laktak opened this issue 6 years ago • 6 comments

When I run the sample from https://github.com/marshmallow-code/apispec/blob/dev/docs/quickstart.rst

the expected output

# {'components': {'parameters': {},
#                 'responses': {},
#                 'schemas': {'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': {'200': {'content': {'application/json': {'schema': {'$ref': '#/definitions/Gist'}}}}}}})]),
#  'tags': []}

includes an additional OrderedDict (see responses) and does not set $ref to the schema:

{'components': {'schemas': {'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': 'Gist'}}})])}})])}

(version 3.3.0)

laktak avatar Apr 09 '20 14:04 laktak

I found a workaround:

spec.path(
    path="/gist/{gist_id}",
    operations=dict(
        get=dict(
            responses={"200": {"content": {"application/json": spec.get_ref("response", "Gist")}}}
        )
    ),
)

laktak avatar Apr 10 '20 09:04 laktak

Looks like a bug. You shouldn't have to do that.

The OrderedDict is normal. It doesn't appear in the docs because it is functionally equivalent and clearer without it. But it doesn't matter.

We need to investigate the ref issue.

lafrech avatar Apr 10 '20 09:04 lafrech

Confirmed.

Looks like it dates back to at least 2.0.0. In fact, I'm wondering how this ever worked.

The logic to resolve the reference seems to be only in the marshmallow plugin.

To fix this (and other potential equivalent issues), we'd need to review clean_parameters and clean_operations to make the right get_ref calls.

We're gonna need to manage the OAS2 and OAS3 cases like in schema_resolver.py:

        # OAS 2 component or OAS 3 header
        if "schema" in data:
            data["schema"] = self.resolve_schema_dict(data["schema"])
        # OAS 3 component except header
        if self.openapi_version.major >= 3:
            if "content" in data:
                for content in data["content"].values():
                    if "schema" in content:
                        content["schema"] = self.resolve_schema_dict(content["schema"])

lafrech avatar Apr 21 '20 17:04 lafrech

I fixed this in #554.

Anyone, feel free to take over this PR if I don't finish it.

lafrech avatar Apr 21 '20 21:04 lafrech

I just stumbled on this in a slightly more complicated setting where I was using multiple schemas and trying to reference them with "oneOf". The workaround worked for me.

Will the fix apply to "oneOf" etc, too?

If this does not belong here, I will gladly open a separate issue.

lfiedler avatar Apr 22 '20 06:04 lfiedler

Done with the PR. Review welcome.

"oneOf" can be treated in another issue. When working with marshmallow schemas, I think this should be done by extending the marshmallow plugin to manage the specific polymorphic schema used. When working with doc passed as dicts, this should be done in core.py but I didn't address this in #554.

@lfiedler yes, please open a dedicate issue with an example of your input.

lafrech avatar Apr 22 '20 21:04 lafrech