-
-
Notifications
You must be signed in to change notification settings - Fork 773
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
WIP: Fix validation of endpoint with multiple "produces" mimetypes. #652
WIP: Fix validation of endpoint with multiple "produces" mimetypes. #652
Conversation
This is a WIP, so here be discussion. Firstly, I've created a failing test that recreates this behavior: an endpoint that produces either json or text/plain (depending on Accept header) and a test that requests both variants. The text variant fails on validating data against json schema. Since I'm not a swagger specialist, the first question is if this is really a valid example, does such behavior correspond to spec? Swagger 2.0 does not provide a way to supply several schemas for different response content-types, so must connexion validate every content-type against single schema? There are types (e.g text/plain) that do not even have the notion of "validating". |
This is something I got stuck on for OpenAPI 3 support. I'm happy to take on this feature when I get that feature set landed.
Right now, IIRC the behavior is:
It would be kind of cool from a user perspective to be able to just provide a decorator interface that the user could subclass. @my_xml_serializer
@my_xml_validator
def add5_handler(param1):
return param1 + 5 |
As I understand, right now connexion only tries to validate JSON and nothing else (neither XML, which is possible but rather complex to add, nor anything else). The problem is, in some cases (see below) it also tries to validate other Content-types as JSON as well e.g. validate a text/plain against a JSON schema (hence the failing test I've added, it shows this). My proposal for validator is this:
It differs from current implementation, which is:
As for checking if response Content-type corresponds to Accept header, I propose to not bother with it, as it's governed by HTTP standard, not Swagger standard and connexion is about Swagger. Same goes to weight factors. Of course, this only relates to swagger 2. In Openapi 3 each content-type has its own schema and that requires its own thinking as well (leaving a question on how to validate a text/plain or text/csv against schema. Probably stick to "only validate JSON" solution for now). BTW, there is a |
You proposal makes sense to me. Regarding the produces decorator: class CustomOperation(Operation):
@property
def __content_type_decorator(self):
mimetype = self.get_mimetype()
if mimetype == "application/xml":
decorator = XMLProduces(mimetype)
return decorator
else:
return BaseSerializer() class XMLProduces(Produces):
def __call__(self, function):
@functools.wraps(function)
def wrapper(request):
response = function(request)
return make_this_into_xml(response)
return wrapper At least that's my best guess :) |
Hey, sorry for jumping in. I had a case recently when we discussed having endpoint that may produce sth like Obviously speaking, some common mime types could've been validated by installing some extras, i.e. your endpoint returns What do you think? Is the example good enough to be taken as potential extension here? |
Hey @kornicameister @positron96 have a look at my demo for request validation: #760 |
@dtkav yes, it seems to be going in a nice direction. As long as there will be some simple way to register custom handlers, it will be very nice ;-) |
Closing this as a PR and suggest to continue discussion on this in #593. |
This will fix #593 .
Changes proposed in this pull request:
produces
stanza of spec.