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

[BUG][FLASK] enum name issue #16557

Closed
4 of 5 tasks
fleutot opened this issue Sep 11, 2023 · 7 comments · Fixed by #16576
Closed
4 of 5 tasks

[BUG][FLASK] enum name issue #16557

fleutot opened this issue Sep 11, 2023 · 7 comments · Fixed by #16576

Comments

@fleutot
Copy link

fleutot commented Sep 11, 2023

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
    -> Nope, but tested with the docker image, which I think is at most 17 days old.
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

Generated flask server code has bad Python syntax for enum.

openapi-generator version

7.0.1-SNAPSHOT (got it from Docker)

OpenAPI declaration file content or url

https://github.com/TALQ-consortium/TALQ_specification/tree/main/oas

Steps to reproduce

I'm trying to generate a TALQ gateway in flask, from their OpenAPI specs.

I've download these OpenAPI specs into my local folder, and tried to generate a server as per the Docker instructions.

docker run --rm -v $PWD:/local openapitools/openapi-generator-cli generate -i /local/talq-api-gateway-2-5-1.json -g python-flask -o /local/out/flask

Generation gave a wall of text, a lot of INFO, some WARN, but the code was generated. I couldn't find anything special about the offending identifiers in this text. Return value in CLI is OK.

Then I followed the instructions in out/flask/README.md:

# building the image
docker build -t openapi_server .

# starting up a container
docker run -p 8080:8080 openapi_server

This results in an error in openapi_server/models/event_type.py, the generated python code is invalid:

class EventType(Model):
    """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

    Do not edit the class manually.
    """

    """
    allowed enum values
    """
    NUMBER_'abnormalNoiseDetected' = 'abnormalNoiseDetected'
    NUMBER_'absoluteLampPowerTooHigh' = 'absoluteLampPowerTooHigh'
    NUMBER_'absoluteLampPowerTooLow' = 'absoluteLampPowerTooLow'
...

NUMBER_' is probably wrong, &#39 is an apostrophe that did not translate correctly. But I'm not sure what the expected result should be.

Related issues/PRs

Related issue: #13689

@wing328
Copy link
Member

wing328 commented Sep 11, 2023

Thanks for reporting the issue.

To fix it, one needs to use {{{...}}} mustache tag (original value) instead of {{...}} mustache tag (escaped value).

Is that something you can help file a PR with?

@fleutot
Copy link
Author

fleutot commented Sep 11, 2023

Thanks for the help! I've tried to touch on the file src/main/resources/python-flask/model.mustache and turn some {{name}} that seemed relevant into {{{name}}}, but my generated openapi_server/models/event_type.py does not change (I regenerate, docker build with no cache, then run).

I'd be happy to try more, but I think I need some more guidance.

@wing328 wing328 changed the title [BUG][FLASK] Description [BUG][FLASK] enum name issue Sep 12, 2023
@fleutot
Copy link
Author

fleutot commented Sep 12, 2023

@wing328
This is what I've tried:

diff --git a/modules/openapi-generator/src/main/resources/python-flask/model.mustache b/modules/openapi-generator/src/main/resources/python-flask/model.mustache
index b9603345..a0790fb2 100644
--- a/modules/openapi-generator/src/main/resources/python-flask/model.mustache
+++ b/modules/openapi-generator/src/main/resources/python-flask/model.mustache
@@ -29,12 +29,12 @@ class {{classname}}(Model):
     allowed enum values
     """
 {{#enumVars}}
-    {{name}} = {{{value}}}{{^-last}}
+    {{{name}}} = {{{value}}}{{^-last}}
 {{/-last}}
 {{/enumVars}}
 {{/allowableValues}}
 
-    def __init__(self{{#vars}}, {{name}}={{{defaultValue}}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}):  # noqa: E501
+    def __init__(self{{#vars}}, {{{name}}}={{{defaultValue}}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}):  # noqa: E501
         """{{classname}} - a model defined in OpenAPI
 
         {{#vars}}
@@ -160,7 +160,7 @@ class {{classname}}(Model):
 {{/hasValidation}}
 {{/isEnum}}
 
-        self._{{name}} = {{name}}{{^-last}}
+        self._{{{name}}} = {{{name}}}{{^-last}}
 
 {{/-last}}
 {{/vars}}

@wing328
Copy link
Member

wing328 commented Sep 13, 2023

I got the following after updating the template:

class EnumModel(Model):
    """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

    Do not edit the class manually.
    """

    """
    allowed enum values
    """
    NUMBER_'available>' = 'available>'
    NUMBER_'pending<' = 'pending<'
    NUMBER_'sold' = 'sold'
    NUMBER_'1' = '1'
    NUMBER_'2' = '2'

Looks like we need to fix the issue in the python flask generator (java class) as well.

@wing328
Copy link
Member

wing328 commented Sep 13, 2023

UPDATE: filed #16576 to fix the issue.

@fleutot
Copy link
Author

fleutot commented Sep 13, 2023

Result after the PR:

class EventType(Model):
    """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

    Do not edit the class manually.
    """

    """
    allowed enum values
    """
    ABNORMALNOISEDETECTED = 'abnormalNoiseDetected'
    ABSOLUTELAMPPOWERTOOHIGH = 'absoluteLampPowerTooHigh'
    ABSOLUTELAMPPOWERTOOLOW = 'absoluteLampPowerTooLow'

, which seems correct, thank you!

I had to manually move the previously generated event_type.py to see the change, maybe I missed a required clean command, or there is something broken in build dependencies.

Very grateful, thanks!

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

Successfully merging a pull request may close this issue.

2 participants