APIDevTools / swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser/validator
https://apitools.dev/swagger-parser
MIT License
1.08k stars 153 forks source link

Bundle with examples used several times #209

Open gigaga opened 2 years ago

gigaga commented 2 years ago

Hi,

By using swagger-parser v10.0.3, I tried to bundle my file that contains several external references from $ref into one by using bundle function.

My spec file is


info:
  version: 1.0.0-SNAPSHOT
  title: petstore-several

paths:
  /pets:
    get:
      tags:
        - pets
      responses:
        '200':
          description: Pet
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pet"
              examples:
                $ref: ./examples.yaml#/test
    delete:
      tags:
        - pets
      responses:
        '200':
          description: Pet
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pet"
              examples:
                $ref: ./examples.yaml#/test

components:
  schemas:
    Pet:
        type: object
        required:
        - pet_type
        properties:
          pet_type:
            type: string

And my js code is :

import SwaggerParser from 'swagger-parser';

async function run() {
    let api = await SwaggerParser.bundle("./examples/specs/petstore/petstore-several-files/petstore-several-files.yaml");
    console.log(JSON.stringify(api, null, 2));
}

run();

The execution sdtout is :

{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0-SNAPSHOT",
    "title": "petstore-several"
  },
  "paths": {
    "/pets": {
      "get": {
        "tags": [
          "pets"
        ],
        "responses": {
          "200": {
            "description": "Pet",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pet"
                },
                "examples": {
                  "jedi": {
                    "value": {
                      "pet_type": "tata"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "pets"
        ],
        "responses": {
          "200": {
            "description": "Pet",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pet"
                },
                "examples": {
                  "$ref": "#/paths/~1pets/get/responses/200/content/application~1json/examples"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "required": [
          "pet_type"
        ],
        "properties": {
          "pet_type": {
            "type": "string"
          }
        }
      }
    }
  }
}

The ref of the second example still is present in bundled spec :(. Redoc, swagger-ui and openapi-generator are not able to manage correctly this second example :(. As workaround, we have to duplicate examples.yaml file (even if it is exactly the same content) but it is very dirty :(

Thanks.