Bug Report Checklist
Description
Generated python models currently accept attributes with any name in their initialization, which allows for objects to be created with attributes that do not match their attribute map. There is also no attempt to convert attributes to their corresponding names in the attribute map. This can lead to issues with reserved words if a dictionary with a reserved word as a key is used to initialize the object. An example is shown below.
Model Attributes:
class Message(ModelNormal):
# some code
attribute_map = {
'application_id': 'applicationId', # noqa: E501
'to': 'to', # noqa: E501
'_from': 'from', # noqa: E501
}
# some code
Input:
message_body = {
"applicationId":"app-abcd",
"to":"+19195551234",
"from":"+19195554321",
"does_not_exist":"fake attribute"
}
new_message = Message(**message_body)
print(new_message)
Expected Output:
{'application_id': 'app-abcd', 'to': '+19195551234', '_from': '+19195554321'}
# Some error mentioning does_not_exist as an invalid and unconvertable attribute
Actual Output
{'applicationId': 'app-abcd', 'to': '+19195551234', 'from': '+19195554321', 'does_not_exist': 'fake attribute'}
This Actual Output is problematic because attempting to access the from attribute is impossible since it cannot be referenced with new_message._from or new_message.from since it was not converted and from is a reserved word. The does_not_exist attribute also should not be created since it does not exist as a valid attribute of the Message object.
openapi-generator version
0.5.3
OpenAPI declaration file content or url
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_utils.mustache
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_templates/method_from_openapi_data_normal.mustache
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_templates/method_init_normal.mustache
Generation Details
using the spec at https://github.com/Bandwidth/api-docs/blob/main/site/specs-source/messaging.json and generating using openapi-generator-cli generate -g python -i messaging.json -o ./msg-test allows for viewing the generated SDK.
Steps to reproduce
Using the SDK generated above, attempting to create a BandwidthMessage object with different combinations of valid and invalid attributes reproduces the different errors.
Related issues/PRs
Indirectly relates to #11092 and #11125, since they allow for more methods of object creation.
Suggest a fix
Adding logic to the set_attribute function within model_utils.mustache should allow for this to be fixed. I'll be opening a PR shortly and linking it here with my solution. PR is #11134
Bug Report Checklist
Description
Generated python models currently accept attributes with any name in their initialization, which allows for objects to be created with attributes that do not match their attribute map. There is also no attempt to convert attributes to their corresponding names in the attribute map. This can lead to issues with reserved words if a dictionary with a reserved word as a key is used to initialize the object. An example is shown below.
Model Attributes:
Input:
Expected Output:
{'application_id': 'app-abcd', 'to': '+19195551234', '_from': '+19195554321'} # Some error mentioning does_not_exist as an invalid and unconvertable attributeActual Output
{'applicationId': 'app-abcd', 'to': '+19195551234', 'from': '+19195554321', 'does_not_exist': 'fake attribute'}This Actual Output is problematic because attempting to access the
fromattribute is impossible since it cannot be referenced withnew_message._fromornew_message.fromsince it was not converted andfromis a reserved word. Thedoes_not_existattribute also should not be created since it does not exist as a valid attribute of theMessageobject.openapi-generator version
0.5.3
OpenAPI declaration file content or url
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_utils.mustache
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_templates/method_from_openapi_data_normal.mustache
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_templates/method_init_normal.mustache
Generation Details
using the spec at https://github.com/Bandwidth/api-docs/blob/main/site/specs-source/messaging.json and generating using
openapi-generator-cli generate -g python -i messaging.json -o ./msg-testallows for viewing the generated SDK.Steps to reproduce
Using the SDK generated above, attempting to create a BandwidthMessage object with different combinations of valid and invalid attributes reproduces the different errors.
Related issues/PRs
Indirectly relates to #11092 and #11125, since they allow for more methods of object creation.
Suggest a fix
Adding logic to the
set_attributefunction withinmodel_utils.mustacheshould allow for this to be fixed. I'll be opening a PR shortly and linking it here with my solution. PR is #11134