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

Adding examples to docs without creating multiple classes #640

Closed
cca32 opened this issue Jan 2, 2023 · 3 comments
Closed

Adding examples to docs without creating multiple classes #640

cca32 opened this issue Jan 2, 2023 · 3 comments

Comments

@cca32
Copy link

cca32 commented Jan 2, 2023

Let's say I have a ninja.Schema as such:

class MySchema(ninja.Schema):
   var1: bool
   var2: str = ''

If I have an end-point that returns this schema, I get nice docs. Which I can customize based on error response:

@router.post('/test', response={200: MyResponse, 400: MyResponse})
def test(request):
   # some logic here to determine 200 vs 400
   return 200, MyResponse(var1=True)

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 customization

responses={
        200: {
            "model": MySchema,
            "description": "customized description here",
            "content": {
                "application/json": {
                    "example": {
                        'custom key': 'custom value'
                    }
                }
            }
        },
}

I'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:

response={
        200: MySchema(var1=True, var2='200 message'), 201: MySchema(var1=True, var2='201 message'), 
        400: MySchema(var1=False, var2='400 message')
    }

then allowing return values of 200/201/400 with the same schema , just varying instantiations of the schema class

@vitalik
Copy link
Owner

vitalik commented Jan 3, 2023

@cca32
maybe just provide multiple example for one schema:

https://docs.pydantic.dev/usage/schema/#schema-customization

class MySchema(ninja.Schema):
   var1: bool
   var2: str = ''

    class Config:
        schema_extra = {
            ...
        }

@cca32
Copy link
Author

cca32 commented Jan 5, 2023

I've tried adding the schema customization above but don't see any changes to the docs in regards to the example values
image

Perhaps I'm doing something wrong?

class MySchema(ninja.Schema):
   var1: bool
   var2: str = ''

    class Config:
        schema_extra = {
            'examples': [
                {
                    'var1': True,
                    'var2': '200 message',
                },
                {
                    'var1': False,
                    'var2': '400 message',
                },
            ]
        }
@router.post('/test', response={200: MySchema, 400: MySchema})
def test(request):
   # some logic here to determine 200 vs 400
   return 200, MySchema(var1=True)

Is there anyway to include examples in the docs through ninja.Schema without creating a hard-coded Schema?

# hard-coded Schema
class MySchema200(ninja.Schema):
    var1: bool = True
    var2: str = '200 message'
class MySchema400(ninja.Schema):
    var1: bool = False
    var2: str = '400 message'
@router.post('/test', response={200: MySchema200, 400: MySchema400})

@vitalik
Copy link
Owner

vitalik commented Jan 5, 2023

@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

SCR-20230105-hsb

@vitalik vitalik closed this as completed Jan 13, 2023
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

No branches or pull requests

2 participants