Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How can we reflect the optional/required json items in documentation #388

Closed
humming-bird-21 opened this issue Jul 15, 2019 · 10 comments
Closed

Comments

@humming-bird-21
Copy link

Description

How can I mark the optional/required json elements in the swagger documentation generated using FastAPI?
Currently, for POST verb, it just marked "* required" for the whole body and not individual json elements.

@humming-bird-21 humming-bird-21 added the question Question or problem label Jul 15, 2019
@haizaar
Copy link
Contributor

haizaar commented Jul 16, 2019

Are you asking about optional/required fields of the JSON body?

@humming-bird-21
Copy link
Author

@haizaar - yes, that's about reflecting the optional/required json elements in the body for swagger documentation

@euri10
Copy link
Contributor

euri10 commented Jul 16, 2019

It's working out of the box, if you use pydantic objects the generated schema will "translate" the requirements, for instance this pydantic object from the very beginning of the documentation (https://pydantic-docs.helpmanual.io/):

class UserM(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: datetime = None
    friends: List[int] = []

used this way:

@app.post("/userm")
async def userm(userm: UserM):
    pass

shows in the documentation as

Imgur

So quoting pydantic doc, you can check id is required (red star), name has a default so not-required (no red star) etc....

What’s going on here:
id is of type int; the annotation only declaration tells pydantic that this field is required. Strings, bytes or floats will be coerced to ints if possible, otherwise an exception would be raised.
name is inferred as a string from the default, it is not required as it has a default.
signup_ts is a datetime field which is not required (None if it’s not supplied), pydantic will process either a unix timestamp int (e.g. 1496498400) or a string representing the date & time.
friends uses python’s typing system, it is required to be a list of integers, as with id integer-like objects will be converted to integers.

@euri10
Copy link
Contributor

euri10 commented Jul 16, 2019

which makes me think reading the spec on
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
that the generated schema defaults to True for the request body when the spec defaults to False, I guess a minor thinkg

@humming-bird-21
Copy link
Author

thanks, @euri10 and @haizaar

@haizaar
Copy link
Contributor

haizaar commented Jul 16, 2019

which makes me think reading the spec on
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
that the generated schema defaults to True for the request body when the spec defaults to False, I guess a minor thinkg

@euri10, What if you define the handler as async def userm(userm: UserM = None): (or any other default value)?

@euri10
Copy link
Contributor

euri10 commented Jul 16, 2019

if you do that @haizaar the red "required" next to the Request Body disappears

@haizaar
Copy link
Contributor

haizaar commented Jul 16, 2019 via email

@euri10
Copy link
Contributor

euri10 commented Jul 16, 2019

yep indeed, seems like I got things reversed, like right and left, one day it'll be better :)

@tiangolo
Copy link
Member

Hehe, thanks for the help @euri10 and @haizaar.

And thanks @sl2016 for reporting back and closing the issue.

@tiangolo tiangolo changed the title [QUESTION] - How can we reflect the optional/required json items in documentation - How can we reflect the optional/required json items in documentation Feb 24, 2023
@tiangolo tiangolo changed the title - How can we reflect the optional/required json items in documentation How can we reflect the optional/required json items in documentation Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8181 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants