This is the 10th day of my participation in the August Text Challenge.More challenges in August

What is the Example

You may have noticed that there was no instance in the Example Value in Response in docs. How do you do that?

The answer is to use Filed objects, or you can use schema_extra in the model.

Requests Example Value

Filed

from pydantic import BaseModel,Field

class ss(BaseModel) :
    name:str= Field(... ,example='phyger')
    age:int= Field(... ,example=18)

@app.post('/ff/')
async def get_ff(ff:ss) :
    res = {'res':ff}
    return res
Copy the code

docs

As you can see, you already have the instance data

schema_extra

from pydantic import BaseModel,Field

class ss(BaseModel) :
    name:str
    age:int
    class Config:
        schema_extra = {
            "example": {"name":"phyger678"."age":20}}@app.post('/ff/')
async def get_ff(ff:ss) :
    res = {'res':ff}
    return res
Copy the code

docs

As you can see, it’s already showing.

Response Example Value

With the Requests sample information out of the way, let’s take a look at the Reponse sample information.

code

from pydantic import BaseModel
class info(BaseModel) :
    name:str
    age:int

@router.post('/test')
def test(info:info) :
    msg = {"people_info":info}
    return msg
Copy the code

The effect

You’ll notice that the code above has no example information for Reponse in Docs.

Code – New

from pydantic import BaseModel
class info(BaseModel) :
    name:str
    age:int

class repMd(BaseModel) :
    people_info:info

@router.post('/test',response_model=repMd)
def test(info:info) :
    msg = {"people_info":info}
    return msg
Copy the code

Effect – New

You’ll find that the sample data for Reponse is done.

What’s the use of that?

In addition to the information refinement and request body hints that can be done on the interface in Docs, we mentioned above. The Example part can also play an unexpected good effect on the reconstruction of the whole project, system docking, delivery testing, etc.

It is recommended that we add sample information for Requests and Reponse in the actual development process, and we should not fail to cut the work. Only when we set rules in the initial stage of the project, we can make a good product.

All the above are xiaobian in the actual work deeply realized, if there is no perfect related notes and documents in the early stage, it will cause great trouble for the subsequent maintenance and testing. The cost of rework is far greater than the immediate benefits obtained when blindly rushing to work!

Do products or have to be down-to-earth, steady progress!

Thank you for reading, don’t forget to follow, like, comment, forward four consecutive yo!