MarcoMuellner / openapi-python-generator

A client generator from openapi for python.
MIT License
54 stars 22 forks source link

invalid code being created which fails black stage due to variable name with hyphen #64

Closed ITJamie closed 6 months ago

ITJamie commented 6 months ago

Describe the bug fails to generate due to invalid variable name (template issue)

To Reproduce using this file: https://gist.githubusercontent.com/ITJamie/c5dd1ff2480b5985428e742d6adeff53/raw/94c638f8e277684b66600ef76a78fbc56574c783/openapi.json

openapi-python-generator ./openapi-swagger-ui.json CheckMK
Generating data from ./openapi-swagger-ui.json
Error in model ObjectCollectionMember: illegal target for annotation (<string>, line 20)
Error in model ObjectActionMember: illegal target for annotation (<string>, line 19)
Traceback (most recent call last):
  File "src/black/__init__.py", line 1512, in assert_equivalent
  File "src/black/parsing.py", line 144, in parse_ast
SyntaxError: illegal target for annotation (<unknown>, line 20)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/scratching/checkmk/venv/bin/openapi-python-generator", line 8, in <module>
    sys.exit(main())
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/openapi_python_generator/__main__.py", line 56, in main
    generate_data(
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/openapi_python_generator/generate_data.py", line 157, in generate_data
    write_data(result, output)
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/openapi_python_generator/generate_data.py", line 101, in write_data
    write_code(models_path / f"{model.file_name}.py", model.content)
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/openapi_python_generator/generate_data.py", line 43, in write_code
    raise e
  File "/scratching/checkmk/venv/lib/python3.10/site-packages/openapi_python_generator/generate_data.py", line 34, in write_code
    formatted_contend = black.format_file_contents(
  File "src/black/__init__.py", line 1073, in format_file_contents
  File "src/black/__init__.py", line 1047, in check_stability_and_equivalence
  File "src/black/__init__.py", line 1514, in assert_equivalent
AssertionError: cannot use --safe with this file; failed to parse source file AST: illegal target for annotation (<unknown>, line 20)
This could be caused by running Black with an older Python version that does not support new syntax used in your source file.

Expected behavior code to get generated

Desktop (please complete the following information):

Additional context i put a quick patch into openapi_python_generator/generate_data.py which allowed it to create the file and continue:

            except NothingChanged:
                formatted_contend = content
            except AssertionError:
                formatted_contend = content

this is the model it and error on. notice the invalid variable name x-ro-invalidReason

from typing import *

from pydantic import BaseModel, Field

from .Link import Link

class ObjectActionMember(BaseModel):
    """
    None model

    """

    links : List[Link] = Field(alias="links" )

    id : str = Field(alias="id" )

    disabledReason : Optional[str] = Field(alias="disabledReason" , default = None )

    invalidReason : Optional[str] = Field(alias="invalidReason" , default = None )

    x-ro-invalidReason : Optional[str] = Field(alias="x-ro-invalidReason" , default = None ) ### invalid variable name

    memberType : Optional[Any] = Field(alias="memberType" , default = None )

    parameters : Optional[Dict[str, Any]] = Field(alias="parameters" , default = None )

    name : Optional[str] = Field(alias="name" , default = None )

    title : Optional[str] = Field(alias="title" , default = None )

adding | replace("-","_") to the models.jinja can solve the issue:

before: {{ property.name | replace("@","") }} : {{ property.type.converted_type | safe }} = Field(alias="{{ property.name }}" {% if not property.required %}, default = {{ property.default }} {% endif %}) after: {{ property.name | replace("@","") | replace("-","_") }} : {{ property.type.converted_type | safe }} = Field(alias="{{ property.name }}" {% if not property.required %}, default = {{ property.default }} {% endif %})