openapi-python-generator icon indicating copy to clipboard operation
openapi-python-generator copied to clipboard

Error with illegal characters in property/enum names

Open altescy opened this issue 11 months ago • 0 comments

Describe the bug

Hello 👋 I encountered some errors with names like below:

  • property names starting with digit character (e.g. 30d_active)
  • enum member name containing hyphen (e.g. admin-user)

To Reproduce

❯ cat << EOF > openapi.json
{"openapi":"3.0.2","info":{"title":"My Service","version":"local"},"paths":{"/users":{"get":{"summary":"Get users","description":"Returns a list of users.","operationId":"users_get","parameters":[{"name":"type","in":"query","required":true,"schema":{"$ref":"#/components/schemas/UserType"}}],"responses":{"200":{"description":"Successful response","content":{"application/json":{"schema":{"type":"array","items":{"$ref":"#/components/schemas/User"}}}}}}}}},"components":{"schemas":{"UserType":{"title":"UserType","description":"An enumeration.","enum":["admin-user","regular-user"]},"User":{"title":"User","description":"A user.","type":"object","properties":{"id":{"type":"string","format":"uuid"},"name":{"type":"string"},"type":{"$ref":"#/components/schemas/UserType"},"30d_active":{"type":"boolean"}}}}}}
EOF

❯ openapi-python-generator openapi.json myclient
Generating data from openapi.json
Error in model UserType: cannot assign to expression here. Maybe you meant '==' instead of '='? (<string>, line 5)
Error in model User: invalid decimal literal (<string>, line 21)
Traceback (most recent call last):
   ( ...... )
black.parsing.InvalidInput: Cannot parse: 21:6:     30d_active : Optional[bool] = Field(validation_alias="30d_active" , default = None )

Expected behavior

Illegal names should be converted into safe names like:

  • Property name: 30d_active -> var_30d_active
  • Enum member name: admin-user -> ADMIN_USER

altescy avatar Jan 29 '25 11:01 altescy