-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[python-nextgen] use __fields_set__ to determine if the field is needed in to_dict #15086
Conversation
Thanks a lot for proposing a fix! I'm a bit worried that this does not provide a "clean" way of checking whether a field is "null" or unset in response content. That can that only be done by inspecting the |
@robertschweizer have you reviewed the new tests: https://github.com/OpenAPITools/openapi-generator/pull/15086/files#diff-638b6ec3d1b35e0b1694a2c1ba3c3d24fb00291092e5abe63d8ec3a86b4bdb6d ? I think this is also what the pydantic users get used to: https://docs.pydantic.dev/usage/models/#creating-models-without-validation |
Yes, I have reviewed the new test. It inspects whether fields are set with I think there's a trade-off: Using # Check if field is set
"field" in instance.to_dict()
"field" in instance.__fields_set__
# Unset field
instance.__fields_set__.remove("field") Using an # Check if field is set
instance.field is Unset
"field" in instance.to_dict()
# Unset field
instance.field = Unset I find the This is not a request to change this MR or so, I just wanted to clarify the trade-off coming with this solution. |
i'm not against this solution or any other solutions. My approach is simply to go with what pydantic offers at the moment as pydantic seems to be pretty mature in handling many use cases. I wonder if you can make a suggestion via https://github.com/pydantic/pydantic to create a new type called |
Thanks for the suggestion! Looking more closely at pydantic, I think version 2's Required vs. Nullable Cleanup offers everything needed for correct representation of JSON schemas. The only thing missing in version 1 is actually the validation of non-required non-nullable fields like here: from pydantic import BaseModel
class ModelClass(BaseModel):
not_required_not_nullable: int = None
inst = ModelClass(not_required_not_nullable=None) # Fails in pydantic-2 but not pydantic-1 Only missing is a pretty way of checking for membership and modifying instances after creation. I find it unlikely that they will redo things only for such usability improvement so shortly before the v2 release. To use what pydantic offers as much as possible, |
By doing so, the |
pydantic's from pydantic import BaseModel
class ModelClass(BaseModel):
not_required_and_nullable: Optional[int] = None
not_required_not_nullable: int = None
required_but_nullable: Optional[int]
required_not_nullable: int But the So there is no way to use pydantic's Thanks for your replies! I did end up opening pydantic/pydantic#5326 to ask pydantic maintainers' opinion about an |
@robertschweizer can you please PM me via Slack for a quick chat (IM)? https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g |
__fields_set__
to determine if the nullable field is needed (set explicitly to None) in to_dictnull
and unset values #15056PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
(6.3.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks)FYI @taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @arun-nalla (2019/11) @spacether (2019/11) @krjakbrjak (2023/02)