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") #7119

Closed
5 of 6 tasks
rmerceragari opened this issue Aug 3, 2020 · 7 comments
Closed
5 of 6 tasks

[BUG] Python generator generating reserved word ("from") #7119

rmerceragari opened this issue Aug 3, 2020 · 7 comments

Comments

@rmerceragari
Copy link

rmerceragari commented Aug 3, 2020

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used? v4.3.1 (implementation-version: 4.3.1)
  • Have you search for related issues/PRs? Yes
  • What's the actual output vs expected output? See below
  • [Optional] Bounty to sponsor the fix (example)
Description

When using the python generator (not the experimental one) the provided spec generates a python test that uses a reserved word as a variable name. This causes python to blow up.

The spec has to have this property: A python reserved word is used as a nested property in a schema (FailureStats -> FailureStat -> from). From is a reserved word in python but is not escaped because it's nested. In the provided sample it ends up being generated in a test file:

 def make_instance(self, include_optional):
        """Test FailureStats
            include_option is a boolean, when False only required
            params are included, when True both required and
            optional params are included """
        # model = openapi_client.models.failure_stats.FailureStats()  # noqa: E501
        if include_optional :
            return FailureStats(
                version = 56, 
                status = '0', 
                code = 56, 
                offset = 56, 
                count = 56, 
                failure_stats = [
                    openapi_client.models.failure_stat.FailureStat(
                        samples = 56, 
                        subject = '0', 
                        from = '0',  # HERE!
                        domain = '0', 
                        first_occurrence = datetime.datetime.strptime('2013-10-20 19:20:30.00', '%Y-%m-%d %H:%M:%S.%f'), 
                        last_occurrence = datetime.datetime.strptime('2013-10-20 19:20:30.00', '%Y-%m-%d %H:%M:%S.%f'), 
                        ip_count = 56, 
                        links = [
                            '0'
                            ], )
                    ]
            )
        else :
            return FailureStats(
        )
openapi-generator version

v4.3.1 (implementation-version: 4.3.1). Does not appear to be a regression that I can see.

OpenAPI declaration file content or url
Command line used for generation

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i spec3.yaml -g python -o ./output/

Steps to reproduce

Download openapi-generator-cli.jar from github.
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i spec3.yaml -g python -o ./output/
pytest ./output/test/

Related issues/PRs

I couldnt find any that seemed to mimic this issue for python.

Suggest a fix

In PythonClientCodegen.java. On line 872. Just add the following:

if (isReservedWord(propname)) {
     propname = escapeReservedWord(propname);
}
@ryan-beisner
Copy link

I have a nearly identical scenario, where the "del" reserved operator is generated and used as an assignment inside the make_instance method, and it reproduces on 4.2.3 and 5.0.3b.

@0xDAFE
Copy link

0xDAFE commented Dec 8, 2020

Having the same problem with a nested "self" unfortunately.

TypeError: wrapped_init() got multiple values for argument 'self'.

Tested with v5.0.0-beta3 and 4.3.1.

@indydcarr
Copy link

Having the same problem with a nested "self" unfortunately.

TypeError: wrapped_init() got multiple values for argument 'self'.

Tested with v5.0.0-beta3 and 4.3.1.

I have the same exact issue, were you able to find a past version that this was resolved in?

@Kylmakalle
Copy link
Contributor

Experiencing this problem with _self attribute of AppStoreConnect API on 5.2.0-SNAPSHOT generator.

Works pretty fine on 5.1.1

Looks like this diff 5.2.0 vs 5.1.1 changed the behaviour
image

@mugx-fc
Copy link

mugx-fc commented Jul 15, 2021

I had a similar issue while using the keyword self (trying to be respectful of the HAL schema definition: https://datatracker.ietf.org/doc/html/draft-kelly-json-hal#section-8.1), so I found a solution using the following flag

--reserved-words-mappings self=self

hope that can help you.

@Lnk2past
Copy link

I am hitting this error (specifically with from). Trying --reserved-words-mappings from=from didn't seem to have any effect on the generated code. Am I using this flag incorrectly, or is there another workaround? I am attempting to use a spec (that I do not control) and it uses reserved words from Python as a part of various schemas/components.

@spacether
Copy link
Contributor

spacether commented Oct 5, 2022

This is working in v6.2.0 and onward in the python client if the invalidly named property has an inline schema.
If the property is a refed schema, then this is working in commit 57f5cc4 which will land in 6.2.1.

One can see a passing test at:

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

No branches or pull requests

8 participants