Skip to content

Bug - python client deserialization when attribute named self#9006

Merged
spacether merged 10 commits intoOpenAPITools:masterfrom
gbmarc1:bug-python-client-self-attribute
Apr 11, 2021
Merged

Bug - python client deserialization when attribute named self#9006
spacether merged 10 commits intoOpenAPITools:masterfrom
gbmarc1:bug-python-client-self-attribute

Conversation

@gbmarc1
Copy link
Copy Markdown
Contributor

@gbmarc1 gbmarc1 commented Mar 18, 2021

@taxpon @frol @mbohlool @cbornet @kenjones-cisco @tomplus @Jyhess @arun-nalla @spacether

When an attribute is named self in model __init__ fails because self is in kwargs. Generation maps self to _self properly. However, when calling the endpoint the deserialization fails.

openapi: 3.0.2
info:
  title: bug report
  description:  Bug report
  version: 0.60.1
servers:
  - url: /inference/v1
paths:
  /datasets:
    get:
      responses:
        '200':
          description: description
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/datasets'

components:
  schemas:
    datasets:
        type: object
        properties:
          self:
            type: string
    import open_api
    from open_api.api import datasets_api

    # Defining the host is optional and defaults to http://localhost/inference/v1
    # See configuration.py for a list of all supported configuration parameters.
    configuration = open_api.Configuration(
        host="/inference/v1",
    )

    # Enter a context with an instance of the API client
    with open_api.ApiClient(configuration) as api_client:
        # Create an instance of the API class
        api_instance = datasets_api.DatasetsApi(api_client)

        api_instance.v1_inference_datasets_get()  # FAILS HERE

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Mar 19, 2021

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@gbmarc1 gbmarc1 force-pushed the bug-python-client-self-attribute branch from b8b716a to 0c292d3 Compare March 19, 2021 16:00
@gbmarc1
Copy link
Copy Markdown
Contributor Author

gbmarc1 commented Mar 19, 2021

@wing328 , it should be ok now. Thank you

Comment thread modules/openapi-generator/src/main/resources/python/model_utils.mustache Outdated
Copy link
Copy Markdown
Contributor

@spacether spacether left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gbmarc1 Thank you for your PR. One goal with the python generator is to not treat any variable as special. If a name collision can happen, I have been adding an underscore in on the python side to prevent property name collisions.
So we don't use nullable as a class property, we use _nullable to allow a model to have a property named nullable.

Can you rename the self parameter in the model init signature to _self here?
That way our existing code will work and this variable is not handled in a special way outside of our model mustache templates.

Also, can you add a new schema that demonstrates this problem to our sample spec here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml#L2320
And can you add a model test like we have here?
If we don't have a test of this and someone changes the code in the future we will not be able to detect when this feature breaks.

Comment thread modules/openapi-generator/src/main/resources/python/model_utils.mustache Outdated
call.respond(HttpStatusCode.Unauthorized)
} else {
call.respond(HttpStatusCode.NotImplemented)
}
Copy link
Copy Markdown
Contributor

@spacether spacether Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gbmarc1 Adding this ^M looks incorrect
Are you on unix?
It could be from this issue: https://stackoverflow.com/questions/5843495/what-does-m-character-mean-in-vim
Looks like a platform specific newline is being used.

Copy link
Copy Markdown
Contributor Author

@gbmarc1 gbmarc1 Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I will revert that changes for those files

This reverts commit 6fc9712.
@gbmarc1
Copy link
Copy Markdown
Contributor Author

gbmarc1 commented Mar 29, 2021

@spacether
Could you just regenerate the file for me?
Generation fails on my side. I don't know why

[INFO] Reactor Summary for openapi-generator-project 5.1.0-SNAPSHOT: [INFO] [INFO] openapi-generator-project .......................... SUCCESS [ 5.004 s] [INFO] openapi-generator-core ............................. SUCCESS [01:25 min] [INFO] openapi-generator (core library) ................... FAILURE [31:35 min] [INFO] openapi-generator (executable) ..................... SKIPPED [INFO] openapi-generator (maven-plugin) ................... SKIPPED [INFO] openapi-generator-gradle-plugin (maven wrapper) .... SKIPPED [INFO] openapi-generator-online ........................... SKIPPED

@gbmarc1
Copy link
Copy Markdown
Contributor Author

gbmarc1 commented Apr 7, 2021

Any idea why CI\Circleci build fails? Error seems unrelated to this PR

@spacether
Copy link
Copy Markdown
Contributor

spacether commented Apr 9, 2021

Any idea why CI\Circleci build fails? Error seems unrelated to this PR

That error is unrelated to this PR.
Re-running that CI now because it blocked other tests.

@spacether spacether added this to the 5.1.1 milestone Apr 9, 2021
Copy link
Copy Markdown
Contributor

@spacether spacether left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR and adding the test case. This looks good.

@spacether spacether merged commit 8e0955f into OpenAPITools:master Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants