Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #10856
Closed
Open
Issue created Nov 15, 2021 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][Python] model template __copy__ refers to new_cls (undefined)

Created by: tuanchien

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Python generator template's __copy__ function refers to new_cls, which is not defined in the function.

See: https://github.com/OpenAPITools/openapi-generator/blob/9464999d9c799ff9eb723886206ea98b7222032d/modules/openapi-generator/src/main/resources/python/model_templates/methods_shared.mustache#L22

This propagates into the generated model_utils.py file. It will result in failed operations if you try to copy() an OpenApiModel object.

openapi-generator version

This is present in the master branch but also 5.3.0

OpenAPI declaration file content or url

This is reproducible using the petstore sample already present in the repo.

Generation Details

None. Use petstore sample.

Steps to reproduce

From the openapi-generator/samples/client/petstore/python directory, launch Python and run

import petstore_api
from petstore_api.model.class_model import ClassModel
import copy

a = ClassModel()
b = copy.deepcopy(a)  # OK
c = copy.copy(a)  # Raises an error

It produces an error like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/copy.py", line 84, in copy
    return copier(x)
  File "/home/user/openapi-generator/samples/client/petstore/python/petstore_api/model_utils.py", line 195, in __copy__
    return new_cls.__new__(cls, **self.__dict__)
NameError: name 'new_cls' is not defined
Related issues/PRs
Suggest a fix

Change https://github.com/OpenAPITools/openapi-generator/blob/9464999d9c799ff9eb723886206ea98b7222032d/modules/openapi-generator/src/main/resources/python/model_templates/methods_shared.mustache#L22 to

            return cls.__new__(cls, **self.__dict__)
Assignee
Assign to
Time tracking