[FEATURE] Create response_model schema with header
See original GitHub issueIs your feature request related to a problem? Please describe.
It’s currently possible to set the response model to a pydantic class which is very handy but as far as I can see it’s not possible to set up the header properties inside the object. I know that I can set up the header in Response
but that won’t be included in opeanapi documentation.
What I get:
What I want:
Describe the solution you’d like
I think one should be able to create a pydantic
object for responses which supports headers
, content
, etc.
from pydantic import BaseModel, Schema, UrlStr
from uuid import UUID
class CreatedHeader(BaseModel):
"""Header for created response."""
Location: UrlStr = Schema(
...,
description='Location of the newly created resource.'
)
class CreatedContent(BaseModel):
"""Content for created response."""
id: UUID = Schema(
...,
description='Id for the newly created resource.',
example='3fa85f64-5717-4562-b3fc-2c963f66afa6'
)
message: str = Schema(
None,
description=' A human readable message',
example='Use Location in headers to access the new object.'
)
# This should be used as the response_model
class Created(BaseModel):
headers: CreatedHeader
content: CreatedContent
Describe alternatives you’ve considered
For now I create a response model only for content. Header is missing from documentation but I add to description that see header for Location.
Additional context
This can be considered/ addressed as part of #16. Generally speaking the response documentation/ validation similar to what is possible for parameters and query will be very helpful.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top GitHub Comments
@tiangolo, beautiful! I will try it out and let you know the results. cheers.
Thank you for the explanation. Since the first solution already addresses this feature request and there are alternate ways for validating the header I close this issue. Thank you for addressing it quickly.