[PYTHON] Setting default value for Required variables#10809
[PYTHON] Setting default value for Required variables#10809spacether merged 11 commits intoOpenAPITools:masterfrom
Conversation
|
@spacether - this has the fix for one of the issues we spoke about early on. Please review this when you get the time. @wing328 The CI failure does not look related to the change. Does OP need to do anything for this failure. |
8feea0d to
4ed0521
Compare
|
As discussed with @the-akhil-nair, the spec has been updated with new test cases to cover the change: 4ed0521 |
|
@the-akhil-nair
In that case ComposedSchema would probably have a required property set, but we need to allow it to be omitted when None is passed in when making an instance of NullableObjectPropWithRequiredVars OR ComposedSchema |
d51695f to
fbb142e
Compare
It is a configuration variable and not part of the generator property. To avoid confusion I have renamed it to initRequiredVars.
This variable is not part of the generator code. It's not a setter function or setter variable. It's just a configuration variable that will be used to switch back to older code if the flag is turned on.
This PR change will do the same. It's a flag that will turn on the behavior. If the flag is not mentioned in the config file or not used anywhere then the normal openAPI code will be used. Once the flag is turned on it will use the older openAPI code where the required variables will automatically be set. |
How does one use initRequiredVars? |
@spacether I have added new commit which will include |
To handle it generically, you would need to check all allOf schemas and if all of them allow in null, then you need to allow in null at the composed schema level and make the required values at the composed schema optional. This is a headache and oneOf and anyOf and combinations of properties + oneOf/anyOf/allOf also cause cases like this. That's why I separated out the variables in the way that openapi schema validation describes (only store the properties in the schema that they are defined in). |
|
Can you rebase on master and resolve the merge conflicts again? |
| public static final String USE_ONEOF_DISCRIMINATOR_LOOKUP = "useOneOfDiscriminatorLookup"; | ||
| public static final String USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC = "Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped."; | ||
| public static final String INIT_REQUIRED_VARS = "initRequiredVars"; | ||
| public static final String INIT_REQUIRED_VARS_DESC = "If set to false the original beaviour of required vars will be used and if set to true the older beaviour of required vars will be used (behaviour prior to PR #8802)"; |
There was a problem hiding this comment.
People probably aren't going to take time to read a PR to learn what it does. Can you fully describe what it does?
something like
When true required variables are included as positional arguments in init and from openapi data. Note: this can break some composition use cases. To learn more read PR blah.
There was a problem hiding this comment.
Changed the Description for additional property.
https://github.com/OpenAPITools/openapi-generator/pull/10809/files#diff-e1b4dad5dfdcaee3164087471224118ebb7958563501fec4171518883a8887c6R400
spacether
left a comment
There was a problem hiding this comment.
This looks great. Thank you for making those updates. Thank you for your PR.
Problem:
In the following PR:
#8802
The requiredVars have been moved out of the composed schema
__init__and_from_openapi_datasignature because the required vars were probably defined in the composed oneOf/anyOf/allOf schemas and this schema does not know about them.We understand that each schema should be validated separately and required vars of a schema should not be present in composed schema, but while doing the validation of input payload in allOf schema the schema is expecting those requiredVars
to be set and sent in the input payload itself.
For Example, consider the following snippet from the specification file:
As we can see in the above case Dog class contain the Mammals class as part of allOf composed schema.
Now if we will not specify the className in Dog class as payload and when the payload will be validated against Mammals class and Animal class, Mammal class will throw an error.
Code used to test:
Error Captured:
And because of this now it will become the user's responsibility to add the requiredVars as a mandatory input in the payload.
That is user need to always specify the className in their payload like the following:
Since in our application end users may not be know these requiredVars always, the SDK failure creates an issue for us.
Solution:
The proposed solution is to add a configuration variable: setRequiredVars.
If it is set to true, then the older logic will be used. The older logic was used to set the default value of requiredVars in
__init__and_from_openapi_dataas positional arguments.PR 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 bypassing 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(5.3.0),6.0.x@taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @Jyhess (2019/01) @arun-nalla (2019/11) @spacether (2019/11) @wing328