Sample does not reference components
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)
I found a workaround:
spec.path(
path="/gist/{gist_id}",
operations=dict(
get=dict(
responses={"200": {"content": {"application/json": spec.get_ref("response", "Gist")}}}
)
),
)
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.
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"])
I fixed this in #554.
Anyone, feel free to take over this PR if I don't finish it.
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.
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.