Skip to content

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

Open
@zhengl7

Description

@zhengl7
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions