-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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] Fixes additional_properties_type for models #8802
[python] Fixes additional_properties_type for models #8802
Conversation
@@ -10,7 +10,52 @@ | |||
'_additional_properties_model_instances', | |||
]) | |||
|
|||
{{> model_templates/method_init_shared }} | |||
@convert_js_args_to_python_args | |||
def __init__(self, *args, **kwargs): # noqa: E501 |
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.
requiredVars have been moved out of the init signature because the required vars were probably defined in the composed oneOf/anyOf/allOf schemas and this schema does not know about them. Please read why this was done in this PR's description.
8725fe3
to
106d6f4
Compare
This PR is blocked by the fact that v2 specs do not correctly set additionalProperties. Update: I merged my PR that sets model.additionalProperties in #9033 |
…test of it, fixes frit and gmfruit tests
… presence of addprosp in schema, updates sample spec composed schemas to remove addprops False form two
…omposed schema init sig
…itional_properties_model_instances
Re-opening to run CI against the 5.2.X branch |
Problem: The following PR removed the logic for adding the default value for required_vars in composed model's __init__ and _from_openapi_data method due to which it is failing in allOf model class stating missing required positional argument. OpenAPITools#8802 Now it will become users responsibility to add the required_vars as it will become mandatory input in the payload. Since in our application end users may not be knowing these required_vars all the time, the SDK failure create 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 using to set the default value of required_vars in __init__ and _from_openapi_data as positional arguement.
Before this PR, master branch python client code incorrectly set the additional_properties_type of models to None when it was unset, when it should be set to allow any type. Allowing any type is the default behavior when additionalProperties is unset per openapi versions >= 2.
This PR fixes additionalProperties values for models #8771, correctly setting additional_properties_type.
requiredVars have been moved out of the composed schema init signature
requiredVars have been moved out of the composed schema init signature because the required vars were probably defined in the composed oneOf/anyOf/allOf schemas and this schema does not know about them. Every schema is validated separately. So that means that for a composed schema with no type required or properties or additionalProperties definition inside it:
This change is needed because in our java code, if a oneOf class include required variables, those variable are set in composed schema that includes them. This is incorrect because those variable are not required in the composed schema, they are optional there.
One can see this by looking at the Fruit is a oneOf apple, banana example.
This PR updates it so apple and banana include required parameters. Before making the composed schema init change, those parameters were incorrectly present as positional arguments in Fruit. That's because the java code added those apple and banana requiredVars into Fruit's requiredVars which is incorrect.
v2 Spec Implications
There is a bug in swagger-parser for v2 specs. With that bug, values of True, False, and unset are all seen as null additionalProperties which now becomes AnyType schema.
Previously, the default behavior was to treat all 3 conditions as setting additional_properties_type to None, which did not allow any additional properties to be sent for a given object payload.
In order to have additionalProperties True/False, and unset work correctly, users need to upgrade their specifications to v3.
If users still need the old behavior where additionalProperties was None for additionalProperties=True/False/unset, then they should set disallowAdditionalPropertiesIfNotPresent to True. Doing that will break composition usages oneOf/anyOf/allOf because for that to work, in the composed schema additionalProperties must be any type to allow in properties not defined in the composed schema.properties.
There's another bug where schemas with unset type (AnyType) have their type set to object. This PR works around that bug. If you need to differentiate between AnyType schemas and free form objects (type: object with no properties) then update your spec to v3 or higher.
Breaking Change with Fallback
For v2 specs that use compositiona and were setting disallowAdditionalPropertiesIfNotPresent=True this is a breaking change because composition (allOf schemas) will stop working. If you need composition to work, you must omit setting disallowAdditionalPropertiesIfNotPresent which will then default it to False. Composed schemas will then work. This conforms with the openapi tech spec.
Note: this different PR #8816 will fix it so the variables defined in any schema/model in python only include variables for schema.properties and do not include composed schema properties.
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 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
,5.1.x
,6.0.x