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):
OS: mac osx
Python Version: 3.10.0
version 0.5.0
Additional context
i put a quick patch into openapi_python_generator/generate_data.py which allowed it to create the file and continue:
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
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:
this is the model it and error on. notice the invalid variable name
x-ro-invalidReason
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 %})