swagger-api / swagger-api/swagger-codegen

python-flask model incorrect for multi-level $ref

Open
#12,541 0 comments 0 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

Overview

Testing using v3.0.68, when a schema references another schema which is itself a reference, the resulting python-flask model does not contain all of the expected items.

Example

The following OAD is used to demonstrate the problem:

openapi: 3.0.4
info:
  title: Demo of ref bug
  description: |-
    This demonstrates the multi-ref bug
  version: 1.0.0
  
paths:
  /blobs:
    get:
      summary: Return a list of blobs
      description: Return a list of blob (or sub-class) objects
      operationId: getBlobs
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: "array"
                items:
                  anyOf:
                    - $ref: '#/components/schemas/BlobA'
                    - $ref: '#/components/schemas/BlobB'
                    - $ref: '#/components/schemas/BlobC'
                    - $ref: '#/components/schemas/BlobD'
components:
  schemas:
    BlobA:
      description: A class blob
      type: object
      required: [a_item]
      properties:
        a_item:
          description: Item at A class level
          type: string
    BlobB:
      description: B class blob
      allOf:
        - $ref: "#/components/schemas/BlobA"
        - type: object
          properties:
            b_item:
              description: Item at B class level
              type: string
    BlobC:
      description: C class blob
      allOf:
        - $ref: "#/components/schemas/BlobB"
        - type: object
          properties:
            c_item:
              description: Item at C class level
              type: string
    BlobD:
      description: D class blob
      allOf:
        - $ref: "#/components/schemas/BlobC"
        - type: object
          properties:
            d_item:
              description: Item at D class level
              type: string

Expectation

It is expected that BlobA contains only A class items, BlobB contains A and B class items, BlobC contains A, B, and C class items, and BlobD contains, A, B, C, and D class items.

This expectation works in SwaggerEditor, where the schema matches the above.

Result

The resulting python-flask server stub only seems to contain one parent class worth of items.

For instance BlobD model is as follows:

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

    Do not edit the class manually.
    """
    def __init__(self, d_item: str=None, c_item: str=None):  # noqa: E501
        """BlobD - a model defined in Swagger

        :param d_item: The d_item of this BlobD.  # noqa: E501
        :type d_item: str
        :param c_item: The c_item of this BlobD.  # noqa: E501
        :type c_item: str
        """
        self.swagger_types = {
            'd_item': str,
            'c_item': str
        }

        self.attribute_map = {
            'd_item': 'd_item',
            'c_item': 'c_item'
        }
        self._d_item = d_item
        self._c_item = c_item

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 OpenAPI document and inspect the generated python-flask BlobB, BlobC, and BlobD models. Compare inherited properties with the stated expectation; done means each model contains the fields from all referenced parent levels. The issue names no source file or test to begin with.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, openapi, python
Domain
backend, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.