Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specs with vendor extensions might lead to invalid Python code #323

Open
hajs opened this issue Feb 13, 2023 · 0 comments
Open

Specs with vendor extensions might lead to invalid Python code #323

hajs opened this issue Feb 13, 2023 · 0 comments

Comments

@hajs
Copy link

hajs commented Feb 13, 2023

Users can add additional information with x-fields. But Python does not accept identifiers with dashses.

Here are some common vendor extensions documented: https://github.com/Redocly/redoc/blob/main/docs/redoc-vendor-extensions.md#x-logo

Here is example spec:

openapi: 3.0.0
info:
  title: Example
  version: 1.0.0
  x-audience: company-internal
  x-logo:
    url: https://www.example.com/company-logo.png

The generated code looks like this

app = FastAPI(
    title='Example',
    version='1.0.0',
    x - audience='company-internal',
    x - logo={'url': 'https://www.example.com/company-logo.png'},
)

I fixed it this way:

--- fastapi_code_generator/template/main.jinja2.orig    2023-02-13 16:11:00.677867572 +0100
+++ fastapi_code_generator/template/main.jinja2 2023-02-13 16:37:28.944897684 +0100
@@ -8,7 +8,9 @@
     {% if info %}
     {% for key,value in info.items() %}
     {% set info_value= value.__repr__() %}
+    {%- if not key.startswith("x-") -%} 
     {{ key }} = {{info_value}},
+    {%- endif -%}
     {% endfor %}
     {% endif %}
     )

First I tried to add all fields as a dict passed as kwargs:

app = FastAPI(**{
    'title': 'Example',
    'version':  '1.0.0',
    'x-audience': 'company-internal',
    'x-logo': {'url': 'https://www.example.com/company-logo.png'},
})

But ignoring these fields seems sufficient because FastAPI ignores them anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant