Multiple media types for the main response #492
Unanswered
ricardogsilva
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi
I'm evaluating APIFlask for a project and would like to know how/if OpenAPI generation works when a view's main response can have more than one media type. I've looked through the docs and could not find mention of such a use case.
My specific use case is that I need to support both
JSON
andHTML
as output media types. So I want to be able to inspect the request'sAccept
header in order to figure out which output media type to use for the response and then return that.Would APIFlask be able to generate an OpenAPI specification that has two different media types for the response?
The docs state that:
So it looks like if I want to have two different media types then I need to pass in a complete spec dict for the response - based on this I am trying out the following:
and indeed my OpenAPI spec does show me the HTML media type for the response. However, as also alluded in the docs, this is now the only documented response, i.e. even if I am using the
@app.output
decorator, I don't get the autogenerated response for the JSON media type anymore - meaning I would need to add it manually myself, just like I did for HTML. This becomes verbose and impractical to use, as my API will have a lot of views and I don't really want to miss out on APIFlask's autogenerating features.Is there some way to make APIFlask merge the additional media type definition together with the autogenerated one? In the past I have used a similar feature in fastapi and it worked nicely - I was able to both enjoy autogenerated schema for the
JSON
media type while also have a way to add an additional media type manually.As an example, in fastapi this works (here deciding between outputting a PDF or JSON):
The resulting OpenAPI specification shows how a client can perform requests using either
application/json
orapplication/pdf
as the value of theAccept
headerBeta Was this translation helpful? Give feedback.
All reactions