swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
782 stars 528 forks source link

Schema refs of array parameters are not resolved for renamed schema components #1955

Closed loplex closed 1 year ago

loplex commented 1 year ago
Description

Schema ref of array-type operation parameter is not resolved if:

Example

merged_spec12.yaml:

openapi: 3.0.1
info:
  title: merged spec
  version: 1.0.0
servers:
  - url: http://localhost:8080
paths:
  /getEmptyOne:
    $ref: "./spec1.yaml#/paths/~1getEmptyOne"
  /getEmptyTwo:
    $ref: "./spec2.yaml#/paths/~1getEmptyTwo"

spec1.yaml:

openapi: 3.0.1
info:
  title: spec 1
  version: 1.0.0
servers:
  - url: http://localhost:8080
paths:
  /getEmptyOne:
    get:
      operationId: getEmptyOne
      parameters:
        - name: elements
          in: query
          schema:
            type: array
            items:
              $ref: '#/components/schemas/myType'
      responses:
        "204":
          description: empty
components:
  schemas:
    myType:
      type: string

spec2.yaml:

openapi: 3.0.1
info:
  title: spec 2
  version: 1.0.0
servers:
  - url: http://localhost:8080
paths:
  /getEmptyTwo:
    get:
      operationId: getEmptyTwo
      parameters:
        - name: elements
          in: query
          schema:
            type: array
            items:
              $ref: '#/components/schemas/myType'
      responses:
        "204":
          description: empty
components:
  schemas:
    myType:
      type: integer

code:

ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
new OpenAPIV3Parser().read("merged_spec12.yaml", null, parseOptions);
Current behavior

It is parsed with refs containing reference to original file, but with renamed schema component name ($ref: ./spec1.yaml#/components/schemas/myType_1 at .paths[].get.parameters[].schema.items):

openapi: 3.0.1
info:
  description: merged spec
  title: merged spec
  version: 1.0.0
servers:
- url: http://localhost:8080
paths:
  /getEmptyTwo:
    get:
      operationId: getEmptyTwo
      parameters:
      - in: query
        name: elements
        schema:
          items:
            $ref: '#/components/schemas/myType'
          type: array
      responses:
        "204":
          description: empty
  /getEmptyOne:
    get:
      operationId: getEmptyOne
      parameters:
      - in: query
        name: elements
        schema:
          items:
            $ref: ./spec1.yaml#/components/schemas/myType_1
          type: array
      responses:
        "204":
          description: empty
components:
  schemas:
    myType:
      type: integer
    myType_1:
      type: string
Expected behavior

It is parsed with refs containing internal reference to renamed schema component in the same file. ($ref: '#/components/schemas/myType_1' at .paths[].get.parameters[].schema.items):

openapi: 3.0.1
info:
  description: merged spec
  title: merged spec
  version: 1.0.0
servers:
- url: http://localhost:8080
paths:
  /getEmptyTwo:
    get:
      operationId: getEmptyTwo
      parameters:
      - in: query
        name: elements
        schema:
          items:
            $ref: '#/components/schemas/myType'
          type: array
      responses:
        "204":
          description: empty
  /getEmptyOne:
    get:
      operationId: getEmptyOne
      parameters:
      - in: query
        name: elements
        schema:
          items:
            $ref: '#/components/schemas/myType_1'
          type: array
      responses:
        "204":
          description: empty
components:
  schemas:
    myType:
      type: integer
    myType_1:
      type: string
Proposed fix

I tried to fix the issue, I'll make pull request.