Skip to content

null in an Enum crashes the generator #500

Closed
@juspence

Description

@juspence

Describe the bug
Similar to #357
Client generation fails and gives a Python traceback if an enum like below is present in the generated OpenAPI schema.

NullEnum:
  enum:
  - null

To Reproduce
Steps to reproduce the behavior:

  1. Make a new OpenAPI Spec File matching the one below.
  2. Run "openapi-python-client generate --path your_spec_file.yaml"
  3. See a Python traceback about "AttributeError: 'NoneType' object has no attribute 'replace'"
  4. Change "null" in the OpenAPI spec to "anyothervalue".
  5. See that generating the API client now succeeds.
  6. Change "anyothervalue" back to "null".
  7. Change line 62 of parser/properties/enum_property.py like below? Not sure if this is the right behavior:
    output[sanitized_key] = utils.remove_string_escapes(value) if isinstance(value, str) else value
  8. See that generating the API client now succeeds.

Expected behavior
API client is generated successfully, or an error message is shown about "Failed to parse OpenAPI document" if this enum is invalid.

OpenAPI Spec File

openapi: 3.0.3
info:
  title: title
  version: 1.0.0
paths:
  /path/to/something/:
    get:
      responses:
        '200':
          description: ''
components:
  schemas:
    ValueEnum:
      enum:
      - value
    NullEnum:
      enum:
      - null

Desktop (please complete the following information):

  • OS: Linux 4.18
  • Python Version: 3.6.8
  • openapi-python-client version 0.10.4

Additional context
Traceback (most recent call last):
File "/usr/local/bin/openapi-python-client", line 11, in
sys.exit(app())
File "/usr/local/lib/python3.6/site-packages/typer/main.py", line 214, in call
return get_command(self)(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 829, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/typer/main.py", line 497, in wrapper
return callback(**use_params) # type: ignore
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/cli.py", line 148, in generate
config=config,
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/init.py", line 329, in create_new_client
config=config,
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/init.py", line 296, in _get_project_for_url_or_path
openapi = GeneratorData.from_dict(data_dict, config=config)
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/openapi.py", line 446, in from_dict
schemas = build_schemas(components=openapi.components.schemas, schemas=schemas, config=config)
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/properties/init.py", line 655, in build_schemas
schemas_or_err = update_schemas_with_data(ref_path=ref_path, data=data, schemas=schemas, config=config)
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/properties/schemas.py", line 93, in update_schemas_with_data
data=data, name=ref_path, schemas=schemas, required=True, parent_name="", config=config
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/properties/init.py", line 627, in property_from_data
name=name, required=required, data=data, schemas=schemas, parent_name=parent_name, config=config
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/properties/init.py", line 532, in _property_from_data
config=config,
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/properties/init.py", line 323, in build_enum_property
values = EnumProperty.values_from_list(enum)
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/parser/properties/enum_property.py", line 62, in values_from_list
output[sanitized_key] = utils.remove_string_escapes(value)
File "/usr/local/lib/python3.6/site-packages/openapi_python_client/utils.py", line 96, in remove_string_escapes
return value.replace('"', r""")

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions