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
JSON Schema for serialised model, rather than data required for validation #4697
Comments
Thanks so much for this, see #4666 for some context on what we're doing with Json Schema. You pose (indirectly) an interest question - right now the schema generated for a model represents "the data required to pass validation and create a model", in this scenario marking But what I think you're talking about is generating a JSON Schema for the data generated from
This is not something we currently do (or claim we do AFAIK). There are two options:
My immediate thinking is that we should add docs on this, and leave users to patch this themselves rather than put the effort into supporting this properly, but I'm open to a discussion. |
I agree - this is correct from that point of view, but we have two "views". In FastAPI it is common to have
My use case: We generate TypeScript models based on OpenAPI/Swagger JSON (which I assume is a pretty common use case). This is typed as I don't know if it would make sense to have a |
This could also potentially be altered in FastAPI without any involvement of Pydantic itself? If a model is defined in |
Well, if someone - me, @tiangolo, you, or some other volunteer is going to do the work to add this, we might as well do it in pydantic so everyone can benefit from the result. Thinking about this, the only differences between the input schema and output schema is: fields with a default are "not required" in the input schema, but "required" in the output schema. Note - as per #4516, in pydantic V2, Apart from that, I can't think of any other differences between input and output schema (Of course, you can create a model that won't match the schema - with a post validation validator, or with assignment with If not, this sounds like a reasonable feature request, happy to try to add it in V2. |
Agree 😊
I guess the use case for
I'm not familiar with |
Thank you, I'll read up on this after work. Oh wow, you're that far already! 🎉 😊 Good job! |
I see #4666 has been assigned to @kennethreitz 🎉 Let me know if I can help with anything, I'd love to have this issue and #624 implemented. |
Quick update: we do have a plan for distinguishing between the validation and serialization JSON schemas; I'm hoping to get to this at some point in the next week or so (no promises...). |
I think this was completed in #5707 |
Discussed in #4577
Originally posted by JonasKs October 1, 2022
Hi,
When I have a model like this:
The list will always be there, it won't be
None
. I have also not type annotated it aslist[str] | None
. However, the OpenAPI documentation does not mark it as required.The solution is to do this:
but that also forces me to always create
X(list_item=[])
instead of justX()
. Not the end of the world, but for some large models this is a bit annoying.Is OpenAPI documentation generated without using type hints, since this don't work?
Any clean workarounds?
The text was updated successfully, but these errors were encountered: