swagger-api / swagger-api/swagger-codegen

[PYTHON] ref properties do not propagate

Open
#10,912 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Generated python code does not propagate the ref properties. I have checked other languages and they look right, for Go the propagated values are a part of the struct definition, and for Java and Typescript, the class definitions inherit the from a parent class with the propagated properties. As far as I can tell Python does neither.

Swagger-codegen version

using swagger-codegen-cli-2.4.18.jar and editor.swagger.io

Swagger declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
  title: Inheritance test
paths:
  /User:
    get:
      summary: Get User
      responses:
        '200':
          description: The User
          schema:
            $ref: '#/definitions/UserModel'

definitions:
  ModelBase:
    type: object
    additionalProperties: false
    required:
      - id
    properties:
      id:
        type: string
        format: guid

  UserModel:
    type: object
    additionalProperties: false
    allOf:
      - $ref: '#/definitions/ModelBase'
    required:
      - username
    properties:
      username:
        type: string

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i test.yaml -l python -o test_api --additional-properties packageName=test_api --additional-properties packageVersion=1.0

This results in

class UserModel(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
        'username': 'str'
    }

    attribute_map = {
        'username': 'username'
    }

    def __init__(self, username=None):  # noqa: E501
        """UserModel - a model defined in Swagger"""  # noqa: E501

        self._username = None
        self.discriminator = None

        self.username = username

the self.id field is missing and the UserModel does not inherit from ModelBase.

Steps to reproduce

The above yaml file into either editor.swagger.io or codegen v2.4.18

Related issues/PRs

I found a number of past issues

Suggest a fix/enhancement

I'm not sure if python should be doing the object inheritance like Java/Typscript or just copy the properties in like Go. My preference would probably more along the lines of the Go implementation.

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 supplied Swagger YAML and generation command, then inspect how the Python generator handles the UserModel allOf reference to ModelBase. Regenerate the example and compare UserModel with the expected propagated id property and inheritance behavior; done means the generated Python model preserves the referenced properties in a consistent, documented way.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
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.