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

Add support of separate schema per status code #328

Closed
wants to merge 1 commit into from

Conversation

victorcrimea
Copy link

Now apiflask supports multiple @app.output decorators

@greyli
Copy link
Member

greyli commented Jul 14, 2022

Thanks for working on this. I'm not sure if we should support using multiple output decorators. If we want to support adding examples for the non-main response, making some enhancements on the doc decorator seems like a better way to go. I will do some experiments this weekend.

@victorcrimea
Copy link
Author

With the approach I proposed apiflask chooses the response schema based on the response code provided by the view function. We may allow explicit declaration of output schema having a special response type like this:

      @app.route("/lessons", methods=['GET'])
      @app.input(LessonsRequestSchema, location="querystring")
      @app.output(LessonSchema, 201, description="Single lesson")
      @app.output(LessonsResponseSchema, 244, description="list of lessons")
      @app.output(LessonsResponseBriefSchema, 244, description="list of lessons(brief form)")
      def lessons(query):
          #implementation goes here
          data = {......} 
          return ApiflaskResponse(data, 204, LessonsResponseBriefSchema)

This approach will allow to have multiple Schemas per response status. I don't advocate that this is a good way to design an API, but this is what OpenAPI 3 supports with oneOf, allOf, etc (https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/) and someone may move legacy API implementation to apiflask and have to deal with that.

@victorcrimea
Copy link
Author

The reason I modified output decorator and not doc is that it already supports examples. So it was easier change.
What was the original idea having doc and output separately? I understand input is separate because it also provides input data validation, but why output is separate from doc?

@greyli
Copy link
Member

greyli commented Aug 7, 2022

What was the original idea having doc and output separately?

output will serialize the return value and generate the spec, while doc only changes the spec.

Support oneOf, allOf, and anyOf needs more work on the schema resolver (take a look at https://github.com/marshmallow-code/marshmallow-oneofschema). For the spec part, apispec already supports these schema combinator types (marshmallow-code/apispec#701).

I still don't think it's the right direction. Closing now.

@greyli greyli closed this Aug 7, 2022
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.

Support multiple responses with different response codes and schemes
2 participants