swagger-api / swagger-api/swagger-client

Deep object model render impacted by definitions + paths ordering in v2 doc

Open
#1,425 1 comment 0 reactions 1 assignee View on GitHub

@tim-lai is already working on this.

Since May 23, 2020.

cat: resolver cat: specmap P1
Dominant language
JavaScript
Stars
2.7k
Forks
765
Avg merge
1d 1h
Merged PRs (30d)
6

Description

Q&A (please complete the following information)
  • OS: Windows 10
  • Environment: Chrome 72.0.3626.109
  • Method of installation: npm
  • Swagger-Client version: 3.8.23
  • Swagger/OpenAPI version: Swagger 2.0
  • Swagger-UI version: 3.20.7
Content & configuration

Swagger/OpenAPI definition:
Doc with 'definitions' before 'paths' renders incorrectly:

{
  "swagger": "2.0",
  "info": {
    "title": "Bug Repro",
    "version": "1.0.0",
    "description": ""
  },
  "host": "localhost",
  "schemes": [
    "https"
  ],
  "definitions": {
    "SampleDefinitionA": {
      "type": "object",
      "properties": {
        "someProperty": {
          "$ref": "#/definitions/SampleDefinitionB"
        }
      }
    },
    "SampleDefinitionB": {
      "type": "object",
      "properties": {
        "parentOfAllOf": {
          "allOf": [
            {
              "$ref": "#/definitions/SampleEnum"
            }
          ],
          "description": "Some description of the enum"
        }
      }
    },
    "SampleEnum": {
      "type": "string",
      "enum": [
        "ValueA",
        "ValueB"
      ]
    }
  },
  "paths": {
    "/sample": {
      "get": {
        "operationId": "Sample",
        "summary": "Sample",
        "description": "Sample definition",
        "responses": {
          "200": {
            "description": "Some description",
            "schema": {
              "$ref": "#/definitions/SampleDefinitionA"
            }
          }
        }
      }
    }
  }
}

Same doc with 'paths' before 'definitions' renders correctly:

{
  "swagger": "2.0",
  "info": {
    "title": "Bug Repro",
    "version": "1.0.0",
    "description": ""
  },
  "host": "localhost",
  "schemes": [
    "https"
  ],
  "paths": {
    "/sample": {
      "get": {
        "operationId": "Sample",
        "summary": "Sample",
        "description": "Sample definition",
        "responses": {
          "200": {
            "description": "Some description",
            "schema": {
              "$ref": "#/definitions/SampleDefinitionA"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "SampleDefinitionA": {
      "type": "object",
      "properties": {
        "someProperty": {
          "$ref": "#/definitions/SampleDefinitionB"
        }
      }
    },
    "SampleDefinitionB": {
      "type": "object",
      "properties": {
        "parentOfAllOf": {
          "allOf": [
            {
              "$ref": "#/definitions/SampleEnum"
            }
          ],
          "description": "Some description of the enum"
        }
      }
    },
    "SampleEnum": {
      "type": "string",
      "enum": [
        "ValueA",
        "ValueB"
      ]
    }
  }
}

Swagger-Client usage:
As used in the Swagger UI v3.20.7

Describe the bug you're encountering

A v2 document with complex nested models and 'definitions' before 'paths' does not correctly render the fully-expanded model in the Swagger UI. This is because the resolved subtree for the model is not correct in this case. While the 'normal' order is certainly 'paths' before 'definitions', per the spec, order should not matter.

To reproduce...

Steps to reproduce the behavior:

  1. View the given document with 'definitions' before 'paths' in the Swagger UI.
  2. Expand the 200 response model all the way, as in the attached screenshot. Note that the model is not rendered correctly.
Expected behavior

The 'allOf' should be correctly expanded like in the document with 'paths' before 'definitions'. See the attached screenshot. The Swagger UI renders incorrectly because the resolved subtree for the response is incorrect:

            responses: {
              '200': {
                description: 'Some description',
                schema: {
                  type: 'object',
                  properties: {
                    someProperty: {
                      type: 'object',
                      properties: {
                        parentOfAllOf: {
                          allOf: [
                            {
                              type: 'string',
                              'enum': [
                                'ValueA',
                                'ValueB'
                              ],
                              $$ref: '#/definitions/SampleEnum'
                            }
                          ],
                          description: 'Some description of the enum'
                        }
                      },
                      $$ref: '#/definitions/SampleDefinitionB'
                    }
                  },
                  $$ref: '#/definitions/SampleDefinitionA'
                }
              }
            }

The correct resolved subtree (from the document with 'paths' before 'definitions'):

responses: {
              '200': {
                description: 'Some description',
                schema: {
                  type: 'object',
                  properties: {
                    someProperty: {
                      type: 'object',
                      properties: {
                        parentOfAllOf: {
                          type: 'string',
                          'enum': [
                            'ValueA',
                            'ValueB'
                          ],
                          description: 'Some description of the enum'
                        }
                      },
                      $$ref: '#/definitions/SampleDefinitionB'
                    }
                  },
                  $$ref: '#/definitions/SampleDefinitionA'
                }
              }
            }
Screenshots

Correct rendering with 'definitions' before 'paths':
image

Incorrect rendering with 'paths' before 'definitions':
image

Additional context or thoughts

Seems like this bug may be related to https://github.com/swagger-api/swagger-js/issues/1394.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.