-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Advanced use of generic fields in schema generation #2375
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
Conversation
Fixed test_assert_raises_validation_error.
Fixed test_assert_raises_validation_error.
Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>
Codecov Report
@@ Coverage Diff @@
## master #2375 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 25 25
Lines 5109 5088 -21
Branches 1050 1044 -6
=========================================
- Hits 5109 5088 -21
Continue to review full report at Codecov.
|
|
@Mazyod What do you think? It may be useful to you. |
|
Looks like a great addition! (although the details of schema generation as per openAPI standard is beyond me 🙇♂️) |
|
conflicts. But honestly, this is all beyond me. Can you give the simplest possible example of how this might be used IRL? |
� Conflicts: � pydantic/schema.py
|
@samuelcolvin, thanks for your comment. |
|
@samuelcolvin If you are going to issue new version, please take my PR. I'm living on my fork in my project waiting for it. :) Thanks! |
|
I'm sorry this is still open and I've just got around to looking at it properly. There are some trivial conflicts again, but I have some more fundamental questions here:
I'm worried this might need to wait for v2, unless either:
please update |
� Conflicts: � tests/test_schema.py
|
Thanks for you comment.
#2262 produces: #2275 produces: |
# Conflicts: # pydantic/schema.py # tests/test_schema.py
|
@samuelcolvin Looks like I managed to merge. |
|
@samuelcolvin Should I do something else? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
* Added schema generation for Generic fields with tests. Fixed test_assert_raises_validation_error. * Added schema generation for Generic fields with tests. Fixed test_assert_raises_validation_error. * tested on python 3.6, 3.7, 3.8, 3.9 * tested on python 3.6, 3.7, 3.8, 3.9 * restore formatting * fix mistakes * formatting * formatting * formatting * fixed coverage * changes file * changes file * remove redundant len * Update pydantic/schema.py Co-authored-by: Maz Jaleel <mazjaleel@gmail.com> * Generic fields with params * Generic fields with params * Fix my previous test * formatting * merging with nested models-generics * formatting * formatting * rewording Co-authored-by: Maxim Berg <berg@petrostyle.com> Co-authored-by: Maz Jaleel <mazjaleel@gmail.com> Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Change Summary
Continuing #2262, I started to develop the use of generics as field types in models.
My initial implementation of this functionality was rather naive. I made a revision.
In particular, I changed how
modify_schemais called for generics and how the nested type schema is generated.For example, you are using a generic type
MyGeneric[MyCustomType, str].You expect the data to be passed to validation as a Tuple, and a schema must be defined for each nested type and for the entire field.
The sequence of forming the scheme should be, as it seems to me, like this:
allOfschema.modify_schemaof the genericMyGeneric, to where we pass the schema collected at the previous stage. There it can be processed.Moreover, there are two options for nested types - if one type is specified or if several are specified.
For one type, the schema will look like this:
MyGeneric[MyCustomType]->{"allOf": [{"$ref": "#/definitions/MyCustomType"}]}For two nested types like this:
MyGeneric[MyCustomType, str]->{"allOf": [{"type": "array", "items": [{"$ref": "#/definitions/CustomType"}, {"type": "string "}]}]}Related issue number
Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)