swagger-api / swagger-api/swagger-codegen

[Python] Cannot resolve model correctly when $ref file name collide with primitive data type name

Open
#7,030 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When I try to define an object named array.yaml and reference it in another object, the reference won't be correctly resolved. Swagger-code-gen would think I'm referencing just an native array, thus it'll be translated to list type for python instead of my own array class.

Swagger-codegen version

2.2.3

Swagger declaration file content or url

Below is a sample yaml

type: object
allOf:
  - $ref: ./models/object-response.yaml
  - properties:
      items:
         type: array
         items:
           $ref: './models/array.yaml'
         description: a list of array objects

In the generated python code for this class, it has below field,

    swagger_types = {
        'pagination_info': 'PaginationInfo',
        'items': 'list[list]'
    }

which I would like it to be

    swagger_types = {
        'pagination_info': 'PaginationInfo',
        'items': 'list[array]'
    }

while array is the object I defined in array.yaml.

Suggest a fix/enhancement

The reason for this to happen is that when parsing the yaml spec, the $ref: './models/array.yaml' line will be parsed as #/definitions/array, which to me indicate a user defined object instead of primitive type like array, so I think code-gen should be able to resolve it correctly. One fix I can think of is to change io.swagger.codegen.languages.PythonClientCodegen#getTypeDeclaration to resolve ref type property before primitive properties. If this sounds ok I can work on a pull request.

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 in io.swagger.codegen.languages.PythonClientCodegen#getTypeDeclaration and reproduce the issue with the YAML declaration using ./models/array.yaml. Trace how the $ref becomes #/definitions/array, then verify that generated swagger_types uses list[array] rather than list[list].

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.