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][python] Generator generating reserved word ("from") #22

Closed
5 tasks done
Lnk2past opened this issue Sep 30, 2022 · 3 comments · Fixed by #26
Closed
5 tasks done

[BUG][python] Generator generating reserved word ("from") #22

Lnk2past opened this issue Sep 30, 2022 · 3 comments · Fixed by #26
Labels
bug Something isn't working
Milestone

Comments

@Lnk2past
Copy link

Lnk2past commented Sep 30, 2022

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?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

I am attempting to use a spec that I have no control over (I cannot just change the spec). A "step" structure is implemented with from and to properties; however, when the structure is nested, the from keyword does not get escaped. This results in invalid Python code being produced.

Upon importing the models, I am instantly hit with a syntax error:

>>> import openapi_client.models
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/openapi_client/models/__init__.py", line 14, in <module>
    from openapi_client.model.my_response import MyResponse
  File "/usr/local/lib/python3.8/dist-packages/openapi_client/model/my_response.py", line 45
    def from() -> typing.Type['MyResponseData']:
        ^
SyntaxError: invalid syntax

The associated code looks like (shortened slightly for brevity):

class MyResponse(
    schemas.DictSchema
):
    """NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech

    Do not edit the class manually.
    """


    class MetaOapg:
        required = {
            "from",
            "to",
        }
        
        class properties:
        
            @staticmethod
            def from() -> typing.Type['MyResponseData']:
                return MyResponseData
        
            @staticmethod
            def to() -> typing.Type['MyResponseData']:
                return MyResponseData
            __annotations__ = {
                "from": _from,
                "to": to,
            }
    
    to: 'MyResponseData'
    
    @typing.overload
    def __getitem__(self, name: typing_extensions.Literal["from"]) -> 'MyResponseData': ...
    
    @typing.overload
    def __getitem__(self, name: typing_extensions.Literal["to"]) -> 'MyResponseData': ...
    
    @typing.overload
    def __getitem__(self, name: str) -> schemas.UnsetAnyTypeSchema: ...
    
    def __getitem__(self, name: typing.Union[typing_extensions.Literal["from", "to", ], str]):
        # dict_instance[name] accessor
        return super().__getitem__(name)
    
    
    @typing.overload
    def get_item_oapg(self, name: typing_extensions.Literal["from"]) -> 'MyResponseData': ...
    
    @typing.overload
    def get_item_oapg(self, name: typing_extensions.Literal["to"]) -> 'MyResponseData': ...
    
    @typing.overload
    def get_item_oapg(self, name: str) -> typing.Union[schemas.UnsetAnyTypeSchema, schemas.Unset]: ...
    
    def get_item_oapg(self, name: typing.Union[typing_extensions.Literal["from", "to", ], str]):
        return super().get_item_oapg(name)
    

    def __new__(
        cls,
        *args: typing.Union[dict, frozendict.frozendict, ],
        to: 'MyResponseData',
        _configuration: typing.Optional[schemas.Configuration] = None,
        **kwargs: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, None, list, tuple, bytes],
    ) -> 'MyResponse':
        return super().__new__(
            cls,
            *args,
            to=to,
            _configuration=_configuration,
            **kwargs,
        )

I do not know what the entirety of this code should look like, but I do know that the specific code producing the syntax error should at least look like this:

            @staticmethod
            def _from() -> typing.Type['MyResponseData']:
                return MyResponseData
openapi-json-schema-generator version

master branch; this is not a regression as the issue was reported here (and it is still an issue): OpenAPITools/openapi-generator#7119

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  title: Sample
  version: "1.0.0"
paths:
  /test_one:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/my_response"
components:
  schemas:
    my_response:
      type: object
      properties:
        from:
          $ref: "#/components/schemas/my_response_data"
        to:
          $ref: "#/components/schemas/my_response_data"
      required:
        - from
        - to
    my_response_data:
      type: object
      properties:
        data:
          type: string
        id:
          type: integer
Generation Details
java -jar modules/openapi-json-schema-generator-cli/target/openapi-json-schema-generator-cli.jar generate \
    -i /TEST.yaml \
    -g python \
    -t modules/openapi-json-schema-generator/src/main/resources/python \
    -o TEST
Steps to reproduce

Built a Docker image from the following Dockerfile:

FROM maven:3.6.3-jdk-11-openj9

WORKDIR /

RUN apt update && apt install -y git curl unzip python3 python3-pip

RUN curl -L https://github.com/OpenAPITools/openapi-json-schema-generator/archive/refs/heads/master.zip -o master.zip \
    && unzip master.zip

WORKDIR /openapi-json-schema-generator-master

RUN mvn clean install

Then built and ran with:

docker build -t openapi-test -f Dockerfile.openapi .
docker run -it --rm openapi-test bash

Then from within docker

java -jar modules/openapi-json-schema-generator-cli/target/openapi-json-schema-generator-cli.jar generate \
    -i /TEST.yaml \
    -g python \
    -t modules/openapi-json-schema-generator/src/main/resources/python \
    -o TEST

And lastly, tried using the generated code:

cd TEST
python3 -m pip install .
python3 -c "import openapi_client.models"

HOWEVER THIS SPEC WORKS (no nesting)

openapi: "3.0.0"
info:
  title: Sample
  version: "1.0.0"
paths:
  /test_one:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/my_response"
components:
  schemas:
    my_response:
      type: object
      properties:
        from:
          type: string
        to:
          type: string
      required:
        - from
        - to
Related issues/PRs

OpenAPITools/openapi-generator#7119

Suggest a fix

I am not sure how to fix this, but it seems like the escapeReservedWord function is not being called properly?

Edits
  • fixed some code formatting
  • fixed typo
@Lnk2past
Copy link
Author

Also, adding that --reserved-words-mappings from=from did not help

@spacether spacether linked a pull request Sep 30, 2022 that will close this issue
3 tasks
@spacether spacether added this to the 1.0.0 milestone Sep 30, 2022
@spacether spacether added the bug Something isn't working label Sep 30, 2022
@Lnk2past
Copy link
Author

Lnk2past commented Oct 1, 2022

Thanks for the super fast turnaround on this, confirmed working on my end!

@spacether
Copy link
Contributor

Thank you for filling the issue. You're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants