LucyBot-Inc / api-spec-converter

Convert API descriptions between popular formats such as OpenAPI(fka Swagger), RAML, API Blueprint, WADL, etc.
https://lucybot-inc.github.io/api-spec-converter/
MIT License
1.11k stars 181 forks source link

Only one ref is resolved by openapi3 to swagger2. #248

Open chris-ep opened 4 years ago

chris-ep commented 4 years ago

Hello,

i have a issue by the convert of a openapi3 to swagger2 api documentation. For me its look like that only one #ref is resolved if one component is referenced more then once.

For example i have create a simple file:

openapi: 3.0.0
info:
  title: test
  description: |-
   test
  version: "2"
paths:
  /testPathOnlyOneRefIsResolved:
    get:
      summary: test
      description: |-
        test
      responses:
        '200':
          description: |-
                ok
      requestBody:
        $ref: '#/components/requestBodies/Value'
  /testPathRefIsNotResolvedASecondTime:
    get:
      summary: test
      description: |-
        test
      responses:
        '200':
          description: |-
                ok
      requestBody:
        $ref: '#/components/requestBodies/Value'
servers:
- url: https://somewhere.test
  description: above the clouds
components:
  requestBodies:
    Value:
      content:
        application/json:
          schema:
            type: object
            example: {}
      description: |-
        Some content
      required: true

If i try to convert this file the result is:

{
  "swagger": "2.0",
  "info": {
    "description": "test",
    "title": "test",
    "version": "2"
  },
  "host": "somewhere.test",
  "basePath": "/",
  "schemes": [
    "https"
  ],
  "paths": {
    "/testPathOnlyOneRefIsResolved": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "description": "Some content",
            "in": "body",
            "name": "body",
            "required": true,
            "schema": {
              "example": {},
              "type": "object"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "ok"
          }
        },
        "description": "test",
        "summary": "test"
      }
    },
    "/testPathRefIsNotResolvedASecondTime": {
      "get": {
        "parameters": [],
        "responses": {
          "200": {
            "description": "ok"
          }
        },
        "description": "test",
        "summary": "test"
      }
    }
  },
  "x-components": {
    "requestBodies": {
      "Value": {
        "description": "Some content",
        "in": "body",
        "name": "body",
        "required": true,
        "schema": {
          "example": {},
          "type": "object"
        }
      }
    }
  }
}

Only at the first endpoint the body is present on the second endpoint the body is missing.

sytolk commented 4 years ago

This not work for me too: https://gist.githubusercontent.com/sytolk/e7ee6715e9d3949569b4a76749d3848c/raw/d1c97e5f1fc74fc8616668fb6eb77c6bd58296ad/gistfile1.txt

thenorthernpole commented 4 years ago

I would be happy to get an update - this makes the converter functionality useless in practical terms.

rbren commented 4 years ago

This seems like a legitimate bug - thanks for providing a test case!

Unfortunately I don't have much time to devote to api-spec-converter these days, but would be happy to review a PR here.

thenorthernpole commented 4 years ago

Thanks for the update @rbren. I am willing to give it a try, will you be kind to give me a few pointers?

Thanks!

rbren commented 4 years ago

👍 My guess is the issue is somewhere in this function: https://github.com/LucyBot-Inc/api-spec-converter/blob/master/lib/converters/openapi3_to_swagger2.js#L64

Definitely in that file.

thenorthernpole commented 4 years ago

Will give it a try! Thanks!

thenorthernpole commented 4 years ago

@sytolk and @chris-ep - I fixed the issue. Please check the fix :-)