-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
[BUG] [Python] __setitem__ throwing an error for composed instance #10083
Comments
/cc @spacether @wing328 |
All variables should be set in all composed model instances. |
@spacether some queries,
By design, variables might not be present in all of the composed model instances. For this reason, the older code(51b6b9d#diff-2996debf5a47e29df72469d8fede8b41a687d367de6a46d2e2ddf21e69d4abe3) was setting them only on matching composed model instances. OpenApi specification says that all "matching" schemas need to validate the properties. And so the older code was calculating the "matching" schemas first. The new code is invoking on all composed model schemas. This does not seem right.
My understanding is that older code was doing exactly that.
I can submit a patch, but help us understand what might be the right way to fix this, if not the older code. Do you prefer a slight re-organization, where properties are set on every composed model instance but the model instance internally figures out that the property is not present in that schema and returns? |
Variables must be validatable on all required schemas composed or otherwise. If a propertyis not defined then it must be acceptable as an additional property through additionalProperties accepting anything. Let's have a meeting to clear this up. Full payloads must be used. If a payload fails to validate then the schema is not a match and an exception should be thrown. If that schema is required then the exception will not be caught and will be raised. |
Bug Report Checklist
Description
When a schema references another schema under allOf and when setattr operation is performed on an schema instance, the latest code is trying to set attribute on self and composed instances.
If the composed schema does not contain that property, the openapi-generated code is trying to validate and convert the attribute into primitive data types.
If the data type for the attribute is already a primitive data type then no error will be thrown.
But if the attribute is expecting a relationship schema then conversion of such attribute will throw an error.
Snippet of spec file
As we can see in the above case Dog class contain Animal class as part of allOf composed schema.
The Dog class contain an attribute legs which references to Legs schema.
The Animal class does not contain this attribute(legs).
When an operation :
is performed(Where legs is an instance of Legs class) the latest code will try to set the attribute for self and composed instance, then for composed instance i.e. Animal class as this attribute is not present in openapi_types, the code will try to convert legs into a valid_type which in our case is into primitive data type.
Hence the code will throw an error because the expected type for that attribute will be primitive data type but what the code will recieve is of type of an instance of Legs class.
Code used to test:
Error captured:
Expected output:
No Error should be thrown.
Actual output:
openapi-generator version
5.2.1
OpenAPI declaration file content or url
https://gist.github.com/AKKI191289/fef7a46578c4070ce86ed9f45b9f46b0
Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i animal.yaml -g python -o animal
Steps to reproduce
Related issues/PRs
#8802
Suggest a fix
Suggestion:
Earlier the code was finding out the valid class and then only setting the attributes.
There is a variable name _var_name_to_model_instances which was having a mapping of attribute and list of valid classes.
So for above example the mapping for legs will be Dog and DogAllOf class.
So then when attribute was getting initialized for both Dog and DogAllOf no error is thrown as both the class contains
legs as an attribute.
But now the code is initializing legs attribute for Dog, DogAllOf and Animal and since Animal does not contain the legs
attribute it is throwing an error.
The older logic for setting the attribute for proper class should be used.
The text was updated successfully, but these errors were encountered: