Fastapi: [FEATURE] Let us boost our OpenAPI capabilities!!

Created on 11 Nov 2019  路  6Comments  路  Source: tiangolo/fastapi

Is your feature request related to a problem? Please describe.
I want to control the "Example value" of my "Request body". RIght now, we only get something like this:

{
  "string_field": "string"
}

I should be able to control this behaviour, like

{
  "string_field": "Default value for string_field"
}

Describe alternatives you've considered
I thought that declaring default values might change the values in the swagger, but even that does not work. It would even be a bad behaviour, as we would tightly couple Swagger documentation with API behaviour.

Additional context
Add any other context or screenshots about the feature request here.
Screenshot 2019-11-11 at 5 50 49 PM

enhancement

Most helpful comment

Hi,

The documentation does not contain the "example" field. Can you get this info added, as it helps a lot with OpenAPI configuration.

Thanks for the help!! Closing this ticket 馃槃

Best,
Gautam

All 6 comments

Hello! At the moment you already can set the example value of field.
For example:

async def create_user(
    uid: int = Query(..., description="Id of user for add to database", example=123),
    is_superuser: bool = Query(..., description="User status", example=True),
):
    new_user: User = await UserQuery.create_user(uid, is_superuser)
    return UserSchema.from_orm(new_user)

More about it: https://fastapi.tiangolo.com/tutorial/query-params-str-validations/

Hi,

The documentation does not contain the "example" field. Can you get this info added, as it helps a lot with OpenAPI configuration.

Thanks for the help!! Closing this ticket 馃槃

Best,
Gautam

Also, we might want to mention this field for all types (Body, Params, etc), or mention it in a separate OpenAPI related page that combines it.

Reopening it so that this work is there in your backlog, please close it thereafter.

Thanks 馃槃

Uh.. i really forgot https://fastapi.tiangolo.com/tutorial/body-schema/. In new pydantic, this named as Field, but... fastapi doesn't work with new pydantic ;)

Thanks for the help here @prostomarkeloff !

@gautamsinghania95 you can add examples for the schema of your Pydantic models: https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization

The Pydantic docs have an example of that. Also, recent versions of FastAPI have support for recent versions of Pydantic. :smile:

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings