swagger-api / swagger-api/swagger-codegen

[Python] Discriminator_Value_Class_Map not getting populated with oneOf

Open
#10,962 1 comment 2 reactions 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

Python Client generated from yaml spec is not creating the Polymorphic links between the generated classes. This means the client generated cannot convert the response data to the correct object type, making it unusable.

I see the discriminator is assigned in the base class, but the discriminator_value_class_map is empty e.g.

discriminator_value_class_map = {
          }

I was expecting something like:


#inline_response200.py
class InlineResponse200(object):
    
    ....

    discriminator_value_class_map = {
        'Object': Object
    }

    def __init__(self):  # noqa: E501
        """InlineResponse200 - a model defined in Swagger"""  # noqa: E501
        self.discriminator = 'object_type'

#object.py

class Object(object):

    swagger_types = {
    }

    attribute_map = {
    }

    discriminator_value_class_map = {
        'SimpleObject': SimpleObject,
        'ComplexObject': ComplexObject
    }

    def __init__(self):  # noqa: E501
        """Object - a model defined in Swagger"""  # noqa: E501
        self.discriminator = 'object_type'

Im referencing the swagger docs for this under Discriminator

Swagger-codegen version

Version = 3.0.25

Swagger declaration file content or url
openapi: "3.0.3"
info:
  title: "Sample"
  version: "1.0"

components:
  schemas:
    Object:
      oneOf:
        - required:
          - objectType
          - data
          properties:
            objectType:
              type: string
            data:
              type: string
          title: "SimpleObject"
        - required:
          - objectType
          - data
          properties:
            objectType:
              type: string
            data:
              type: string
          title: "ComplexObject"
      discriminator:
        propertyName: objectType
    
  responses:
    sampleObjectResponse:
      description: ""
      content:
        application/json:
          schema:
            oneOf:
              - $ref: '#/components/schemas/Object'
            discriminator:
              propertyName: objectType

paths:
  /api/v1/object:
    get:
      responses:
        "200":
          $ref: "#/components/responses/sampleObjectResponse"
Command line used for generation

swagger-codegen generate -i demo.yaml -l python -o demo_out

Steps to reproduce
  1. Place the above yaml code into a file
  2. Run the command in above section
  3. Go to swagger_client/models/InlineResponse200.py and swagger_client/models/Object.py
  4. Observe missing fields
Related issues/PRs

I couldn't find anything

Suggest a fix/enhancement

There are a number of things going wrong here that need to be fixed:

  1. The model name should match what is in the response section of the yaml. Instead of InlineRepsonse200 the class name should be SampleObjectResponse
  2. OneOfInlineResponse200 does not look to be used or generated incorrectly
  3. SimpleObject and ComplexObject do not get created as classes at all
  4. OneOfobject does look to be used or is generated incorrectly
  5. discriminator_value_class_map should get populated correctly e.g.
discriminator_value_class_map = {
    "SimpleObject": SimpleObject,
    "ComplexObject": ComplexObject
              }
  1. The right model object should be created and return to the user when the api is used

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

Reproduce the issue with the supplied demo.yaml using the shown swagger-codegen command, then inspect swagger_client/models/InlineResponse200.py and Object.py. Trace how oneOf schemas, generated model classes, and discriminator_value_class_map are produced. Done means the named models and discriminator mappings are generated and the API response is converted to the correct object type.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.