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
Adding examples to docs without creating multiple classes #640
Comments
@cca32 https://docs.pydantic.dev/usage/schema/#schema-customization class MySchema(ninja.Schema):
var1: bool
var2: str = ''
class Config:
schema_extra = {
...
} |
@cca32 , sorry - it's not plural but single "example" - you can do it wit inherinatance overriding example config: class MySchema(Schema):
var1: bool
var2: str = ''
class MySchema200(MySchema):
class Config:
schema_extra = {
"example": {
"var1": True,
"var2": "200 message",
}
}
class MySchema400(MySchema):
class Config:
schema_extra = {
"example": {
"var1": False,
"var2": "400 message",
}
}
@api.post("/demo", response={200: MySchema200, 400: MySchema400})
def demo(request):
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's say I have a
ninja.Schema
as such:If I have an end-point that returns this schema, I get nice docs. Which I can customize based on error response:
I am trying to customize the docs to include example values that make sense. Things you would see under 200 vs. 400
I know I can probably create a class for 200's and a class for 400's, change some default values. But was wondering if there was anyway to directly change the "example-values" that show up in the automated docs? I'd prefer to keep one schema class, and then just customize the "example-value" field in the docs
Thanks!
For example in FastAPI, the
responses
argument in the.post()
method allow for such customizationI'm not sure if I'm a fan of the verbose FastAPI customization, a class would probably make more sense.
I was wondering if maybe an object of the Schema could also be passed?
Something like:
then allowing return values of 200/201/400 with the same schema , just varying instantiations of the schema class
The text was updated successfully, but these errors were encountered: