OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python-server] Typings not working correctly

Open
#2,978 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

PYTHON

AbstractCodegenGenerator for Connexion WebServer is gernerating invalid code.
In a specific use case.
If a class is referencing its type in the init function without single quotes.
https://stackoverflow.com/questions/35617608/type-hinting-for-objects-of-type-thats-being-defined

NameError: name 'TreeGroup' is not defined
openapi-generator version
  • 4.0.0
OpenAPI declaration file content or url
components
  schemas:
    TreeGroup:
      properties:
        treeGroupId:
          readOnly: true
          type: string
        name:
          type: string
        groups:
          type: array
          items:
            $ref: "#/components/schemas/TreeGroup"
      required:
        - name
        - groups
      type: object
Command line used for generation
java -DsupportingFiles=util.py,__init__.py,base_model_.py -Dmodels -DskipFormModel=true -jar openapi-generator-cli-4.0.0.jar generate -i openapi-v3.yaml -g python-aiohttp -o ./out/python --skip-validate-spec -c python-config.json --model-package dataobjects
Steps to reproduce

Generate a YAML with an components.schemas as above and generate the models.
This is a self reference.
Try to import them into a python shell.

from openapi_server.dataobjects import TreeGroup
Related issues/PRs
  • <->
Suggest a fix

This approach fixed the bug.

https://stackoverflow.com/questions/35617608/type-hinting-for-objects-of-type-thats-being-defined

class TreeGroup(Model):
    def __init__(self, tree_group_id: str=None, name: str=None, groups: List[TreeGroup]=None):

class TreeGroup(Model):
    def __init__(self, tree_group_id: str=None, name: str=None, groups: List['TreeGroup']=None):

Think this line is responsible for the output but don't know it for sure.

https://github.com/OpenAPITools/openapi-generator/blob/63e1e0451c61d7eb5f191c54d5e68f5fb02f5994/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java#L293-L303

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 at modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java#L293-L303 and reproduce the issue with the supplied OpenAPI schema and generation command. Confirm the generated self-referential TreeGroup model can be imported successfully and uses the expected quoted type reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.