Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #11133
Closed
Open
Issue created Dec 15, 2021 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG] [Python] Attribute Names are not Validated or Converted when Creating Models

Created by: ckoegel

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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

Assignee
Assign to
Time tracking