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

Support schemas in callbacks #544

Merged
merged 6 commits into from
Jan 21, 2021

Conversation

kortsi
Copy link
Contributor

@kortsi kortsi commented Mar 17, 2020

Callback definitions are iterated and checked for schemas. This is
implemented by running operation_helper on all events defined in a
callback definition. These events may contain request and response
definitions similar to regular operation definitions.

Callback definitions are iterated and checked for schemas. This is
implemented by running operation_helper on all events defined in a
callback definition. These events may contain request and response
definitions similar to regular operation definitions.
Copy link
Member

@lafrech lafrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @kortsi for this contribution.

See my comment inline.

The tests seem comprehensive. And kinda massive. I guess the massiveness is due to black formatting the dicts like this. Perhaps they'd look better if the callback was declared as a local variable first. That's just cosmetic anyway. I can rework this later if I'm not happy with it.

Also, since all tests are a copy of a schema v3 test, I'm wondering if there could be room for factorization.

@@ -196,6 +196,9 @@ def operation_helper(self, operations, **kwargs):
operation["parameters"]
)
if self.openapi_version.major >= 3:
for callback in operation.get("callbacks", {}).values():
for event in callback.values():
Copy link
Member

@lafrech lafrech Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback may be a reference. Let's not assume it is a callback object dict.

Right now we don't offer the possibility to define callback components and we don't resolve the strings as reference either (see #245 (comment)). But still, a user could pass {$ref: "MyCallback"}, I suppose.

Also I try to stick to OAS naming. I might have used path rather than event, but maybe event is better. Just wondering out loud. No big deal anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we don't support {$ref: "MyCallback"} in other places, so let's not support it here. However, we could expect ref as strings, even if we don't support them yet as per #245. So let's check callback is actually a dict.

            if self.openapi_version.major >= 3:
                for callback in operation.get("callbacks", {}).values():
                    if isinstance(callback, dict):
                        for path in callback.values():

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be convenient if this code block were implemented in a separate method so that support for callbacks can be documented similar to the other OpenAPI objects that are parsed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bangertm do you mean add callback_helper to the plugin interface and have it called from APISpec.path?

Copy link
Collaborator

@Bangertm Bangertm Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No just putting that code block in a method called something like resolve_callback and then having a docstring similar to the ones in SchemaResolver. Then there is some user documentation on the fact that users can put a Marshmallow schema in a callback.

Ideally this method would be part of SchemaResolver too, but given the recursion it can't. You were probably right about making SchemaResolver it a mixin.

@kortsi
Copy link
Contributor Author

kortsi commented Dec 7, 2020

I implemented a "resolve_callback" method with documentation as proposed, made sure only dict callback objects are processed, and refactored tests a bit to make them a little less massive. I then merged the latest dev on top. Is there still something left to do to get this ready?

@kortsi kortsi requested review from lafrech and Bangertm December 7, 2020 10:29
Copy link
Collaborator

@Bangertm Bangertm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the resolve_callback method to be in the schema_resolver module so that all of the code for looking for schemas in OpenAPI object is in the same place. Other than that this looks great.

@kortsi
Copy link
Contributor Author

kortsi commented Dec 9, 2020

I'd prefer the resolve_callback method to be in the schema_resolver module so that all of the code for looking for schemas in OpenAPI object is in the same place. Other than that this looks great.

This is now done. I also had to move the code from operation_helper to SchemaResolver as resolve_operations since resolve_callbacks uses it.

Copy link
Member

@lafrech lafrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks.

Let's merge this.

Sorry for the delay.

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#callbackObject`_.

This is done recursively, so it it is possible to define callbacks in your callbacks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/it it/it

@lafrech lafrech merged commit d54554b into marshmallow-code:dev Jan 21, 2021
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

Successfully merging this pull request may close these issues.

3 participants