Skip to content

[BUG] openapi-yaml generator produces invalid output with nested references #24528

Description

@github-mass
Description

When using the structure described below as input to the openapi-yaml generator, the output is incorrect. The generation does not fail per se, but the output is malformed. There are multiple error messages

Failed to get the schema name

and

Error obtaining the datatype from ref

in the log, pointing to the cause of the problem.

openapi-generator version

Tested using the openapi-generator-cli Docker v7.24.0.
This does not appear to constitute a regression.

Expected vs. Actual

The difference is on the schemata for ArrayOfComplexTypes and NullableComplexType.

Actual:

openapi: 3.0.4
info:
  title: Open API Ref Test
  version: "1.0"
servers:
- url: /
paths:
  /simple:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SimpleType"
          description: Simple return
  /arr:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ArrayOfComplexTypes"
          description: description
  /null:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/NullableComplexType"
          description: description
components:
  schemas:
    ComplexType:
      properties:
        prop1:
          $ref: "#/components/schemas/SimpleType"
        prop2:
          items:
            $ref: "#/components/schemas/SimpleType"
          type: array
        prop3:
          allOf:
          - $ref: "#/components/schemas/SimpleType"
          nullable: true
      type: object
    SimpleType:
      example:
        value: value
      properties:
        value:
          type: string
      type: object
    ArrayOfComplexTypes:
      example:
        complexArray:
        - "{}"
        - "{}"
      properties:
        complexArray:
          items:
            $ref: ./swagger.yml#/components/schemas/ComplexType
          type: array
      type: object
    NullableComplexType:
      example:
        complexOrNull: ""
      properties:
        complexOrNull:
          allOf:
          - $ref: ./swagger.yml#/components/schemas/ComplexType
          nullable: true
      type: object

Expected:

openapi: 3.0.4
info:
  title: Open API Ref Test
  version: "1.0"
servers:
- url: /
paths:
  /simple:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SimpleType"
          description: Simple return
  /arr:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ArrayOfComplexTypes"
          description: description
  /null:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/NullableComplexType"
          description: description
components:
  schemas:
    ComplexType:
      properties:
        prop1:
          $ref: "#/components/schemas/SimpleType"
        prop2:
          items:
            $ref: "#/components/schemas/SimpleType"
          type: array
        prop3:
          allOf:
          - $ref: "#/components/schemas/SimpleType"
          nullable: true
      type: object
    SimpleType:
      example:
        value: value
      properties:
        value:
          type: string
      type: object
    ArrayOfComplexTypes:
      example:
        complexArray:
        - "{}"
        - "{}"
      properties:
        complexArray:
          items:
            $ref: "#/components/schemas/ComplexType"
          type: array
      type: object
    NullableComplexType:
      example:
        complexOrNull: ""
      properties:
        complexOrNull:
          allOf:
          - $ref: "#/components/schemas/ComplexType"
          nullable: true
      type: object
OpenAPI declaration file content or url

DIrectory structure:

Image

swagger.yml:

openapi: 3.0.4
info:
  version: "1.0"
  title: Open API Ref Test
paths:
  '/simple':
    get:
      responses:
        '200':
          description: Simple return
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SimpleType'
  '/arr':
    get:
      responses:
        '200':
          description: description
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ArrayOfComplexTypes'
  '/null':
    get:
      responses:
        '200':
          description: description
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/NullableComplexType'

components:
  schemas:
    ComplexType:
      $ref: "./schemas/ComplexType.yml"
    SimpleType:
      $ref: "./schemas/SimpleType.yml"
    ArrayOfComplexTypes:
      $ref: "./schemas/ArrayOfComplexTypes.yml"
    NullableComplexType:
      $ref: "./schemas/NullableComplexType.yml"

schemas/SimpleType.yml

type: object
properties:
  value:
    type: string

schemas/ComplexType.yml:

type: object
properties:
  prop1:
    $ref: '../swagger.yml#/components/schemas/SimpleType'
  prop2:
    type: array
    items:
      $ref: '../swagger.yml#/components/schemas/SimpleType'
  prop3:
    nullable: true
    anyOf:
      - $ref: '../swagger.yml#/components/schemas/SimpleType'

ArrayOfComplexTypes.yml:

type: object
properties:
  complexArray:
    type: array
    items:
      $ref: '../swagger.yml#/components/schemas/ComplexType'

NullableComplexType.yml:

type: object
properties:
  complexOrNull:
    nullable: true
    anyOf:
      - $ref: '../swagger.yml#/components/schemas/ComplexType'
Generation Details
Steps to reproduce

Using the input described above, execute
docker run --rm -it --user $(id -u):$(id -g) --volume "${PWD}:/local" openapitools/openapi-generator-cli:v7.24.0 generate -g openapi-yaml -i local/swagger.yml -o local/out

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions