marshmallow-code / apispec

A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..

Home Page:https://apispec.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

callbacks in operation are not resolved to refs

codectl opened this issue · comments

Callbacks are currently not resolving refs in its body.

Take the following example:

...
post:
    callbacks:
        onEvent:
            http://localhost/callback:
                post:
                    requestBody:
                        content:
                            application/json:
                                schema: FooSchema

It should resolve FooSchema but it doesn't.

Reading "test_callback_schema_v3", it looks like a real schema is required (but not a schema name as string, but full path as string works).

I agree it would be nicer to allow a schema name.

Can't look at it right now (and not on top of my priorities since I don't use callbacks). Would you like to investigate and perhaps add the feature?

@lafrech thank you for having a look.

I had a look at your test and it got me wondering how that test is working because I also had a look at the _resolve_refs_in_operation and I don't see any mention to callbacks:

# apispec/core.py
...
    def _resolve_refs_in_operation(self, operation) -> None:
        if "parameters" in operation:
            parameters = []
            for parameter in operation["parameters"]:
                parameter = self.get_ref("parameter", parameter)
                self._resolve_refs_in_parameter_or_header(parameter)
                parameters.append(parameter)
            operation["parameters"] = parameters
        if "requestBody" in operation:
            self._resolve_refs_in_request_body(operation["requestBody"])
        if "responses" in operation:
            responses = {}
            for code, response in operation["responses"].items():
                response = self.get_ref("response", response)
                self._resolve_refs_in_response(response)
                responses[code] = response
            operation["responses"] = responses

It got me wondering how that test case was even working so I went to check the conftest:

# tests/confest.py

...
def make_spec(openapi_version):
    ma_plugin = MarshmallowPlugin()
    spec = APISpec(
        title="Validation",
        version="0.1",
        openapi_version=openapi_version,
        plugins=(ma_plugin,),
    )
    return namedtuple("Spec", ("spec", "marshmallow_plugin", "openapi"))(
        spec, ma_plugin, ma_plugin.converter
    )


@pytest.fixture(params=("2.0", "3.0.0"))
def spec_fixture(request):
    return make_spec(request.param)

So that test case in question is testing MarshmallowPlugin which it does contain some spec resolution for callbacks so that is why that test case works. The bug is that apispec.core does not resolve callbacks and it needs to be added.

I can implement that condition and create a test case for apispec.core, no problem !

Also would you know why does MarchmallowPlugin implement a schema resolver? To me that seems redundant considering apispec.core already provides logic to resolve schemas. Having MarchmallowPlugin schema resolver override the core resolver can lead to inconsistencies as seen in the example above. Would you know what motivated the decision to have MarchmallowPlugin a schema resolver of its own?

Oh, right.

The point of the marshmallow schema resolver is to transform a Schema object into a dict or a ref. You're right that transforming a string into a ref should be done by core.

PR #828 can close this issue

Done in #828.

Released in apispec 6.2.0.

Thanks.

I love it when a plan comes together