question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Create response_model schema with header

See original GitHub issue

Is 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:

image

What I want:

image

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mostaphaRoudsaricommented, Apr 5, 2019

@tiangolo, beautiful! I will try it out and let you know the results. cheers.

1reaction
mostaphaRoudsaricommented, Apr 5, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Step 5: Response example and schema (API reference tutorial)
The response example shows a sample response from the request example; the response schema defines all possible elements in the response.
Read more >
Response Model - FastAPI
Response Model ¶ · Convert the output data to its type declaration. · Validate the data. · Add a JSON Schema for the...
Read more >
4. Metadata Design - REST API Design Rulebook [Book]
Rule: Cache-Control, Expires, and Date response headers should be used to encourage caching. Caching is one of the most useful features built on...
Read more >
Working with models and mapping templates - Amazon API ...
The payload can have a data model according to the JSON schema draft 4 . You must define the model in order to...
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
The patch version SHOULD NOT be considered by tooling, making no ... Models are defined using the Schema Object, which is an extended...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found