OpenAPITools / OpenAPITools/openapi-generator

[BUG] [python-legacy] Generated tests include invalid Python code when a member has the name of a keyword

Open
#10,236 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?
    • No, but I have run it against v5.2.1 and verified that there are no relevant changes that would address it since then.
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating a schema with a message includes a sub-message with a reserved word (e.g. Wrapper has key whatever, which in turn has key dict), the generated test for Wrapper will include del = None in its whatever's parameter list.

openapi-generator version

v5.2.1

OpenAPI declaration file content or url
swagger: "2.0"
info:
  description: Bug
  title: Bug
  version: 0.0.0

produces:
- application/json
consumes:
- application/json

schemes:
- http
- https

basePath: /a-bug

definitions:
  Wrapper:
    type: object
    properties:
      whatever:
        type: object
        properties:
          del:
            type: boolean


paths:
  /test:
    get:
      description: i'm makeing a bug
      produces:
        - application/json
      consumes:
        - application/json
      responses:
        200:
          description: this is bad and wrong
          schema:
            $ref: "#/definitions/Wrapper"
Generation Details
java -jar openapi-generator-cli-5.2.1.jar generate -g python-legacy -o output-5.2.1-legacy -i bug.yaml
Steps to reproduce
  1. Write above contents to bug.yaml.
  2. Run:
    java -jar openapi-generator-cli-5.2.1.jar generate -g python-legacy -o output-5.2.1-legacy -i bug.yaml
    

Actual results (test/test_wrapper.py):

# coding: utf-8

"""
    Bug

    Bug  # noqa: E501

    The version of the OpenAPI document: 0.0.0
    Generated by: https://openapi-generator.tech
"""


from __future__ import absolute_import

import unittest
import datetime

import openapi_client
from openapi_client.models.wrapper import Wrapper  # noqa: E501
from openapi_client.rest import ApiException

class TestWrapper(unittest.TestCase):
    """Wrapper unit test stubs"""

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def make_instance(self, include_optional):
        """Test Wrapper
            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.wrapper.Wrapper()  # noqa: E501
        if include_optional :
            return Wrapper(
                whatever = openapi_client.models.wrapper_whatever.Wrapper_whatever(
                    del = True, )
            )
        else :
            return Wrapper(
        )

    def testWrapper(self):
        """Test Wrapper"""
        inst_req_only = self.make_instance(include_optional=False)
        inst_req_and_optional = self.make_instance(include_optional=True)

if __name__ == '__main__':
    unittest.main()

Expected results:

# coding: utf-8

"""
    Bug

    Bug  # noqa: E501

    The version of the OpenAPI document: 0.0.0
    Generated by: https://openapi-generator.tech
"""


from __future__ import absolute_import

import unittest
import datetime

import openapi_client
from openapi_client.models.wrapper import Wrapper  # noqa: E501
from openapi_client.rest import ApiException

class TestWrapper(unittest.TestCase):
    """Wrapper unit test stubs"""

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def make_instance(self, include_optional):
        """Test Wrapper
            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.wrapper.Wrapper()  # noqa: E501
        if include_optional :
            return Wrapper(
                whatever = openapi_client.models.wrapper_whatever.Wrapper_whatever(
####################################
                    _del = True, ) # UNDERSCORE ADDED HERE
####################################
            )
        else :
            return Wrapper(
        )

    def testWrapper(self):
        """Test Wrapper"""
        inst_req_only = self.make_instance(include_optional=False)
        inst_req_and_optional = self.make_instance(include_optional=True)

if __name__ == '__main__':
    unittest.main()
Suggest a fix

It seems that the examples here:
https://github.com/OpenAPITools/openapi-generator/blob/158649aff9a87b13b448880d7ca6a4ac35fadcc2/modules/openapi-generator/src/main/resources/python-legacy/model_test.mustache#L35
do not have their name parameters properly escaped.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced model_test.mustache template around line 35 and reproduce the issue using bug.yaml and the documented java -jar generation command. Check the generated test/test_wrapper.py for a reserved member name such as del; the work is done when the generated Python test uses the expected valid form instead of invalid syntax.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.