Skip to content

$ref in external file in parameter with resolve option true #749

Closed

Description

@jmini

When $ref references an external yaml file, Swagger-Parser can merge everything and produce an instance of OpenAPI where the references are local.
The key point is to use setResolve(true).
See more: https://j2r2b.github.io/2018/05/26/external-ref-openapi.html

It was reported that this does not work with $ref in parameter. It works well in request body
and response.


Test case, in the same folder, put two files:

Main OAS3 file: main.yaml

openapi: 3.0.1
info:
  title: ping test
  version: '1.0'
servers:
  - url: 'http://localhost:8000/'
paths:
  /some/ping:
    get:
      operationId: pingGet
      parameters:
        - name: i
          in: query
          description: Test
          required: true
          schema:
            $ref: './external.yaml#/SomeId'
      responses:
        '201':
          description: OK
components:
  schemas: {}

External file: other.yaml

SomeId:
  type: integer
  format: int32
  description: My value 123
  example: 123

Run this code:

OpenAPIParser openApiParser = new OpenAPIParser();
ParseOptions options = new ParseOptions();
options.setResolve(true);

OpenAPI openAPI = openApiParser.readLocation(inputSpec, null, options).getOpenAPI();
String string = Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);

Current value:

openapi: 3.0.1
info:
  title: ping test
  version: "1.0"
servers:
- url: http://localhost:8000/
paths:
  /some/ping:
    get:
      operationId: pingGet
      parameters:
      - name: i
        in: query
        description: Test
        required: true
        style: form
        explode: true
        schema:
          $ref: ./external.yaml#/SomeId
      responses:
        201:
          description: OK
components:
  schemas: {}

Expected value:

openapi: 3.0.1
info:
  title: ping test
  version: "1.0"
servers:
- url: http://localhost:8000/
paths:
  /some/ping:
    get:
      operationId: pingGet
      parameters:
      - name: i
        in: query
        description: Test
        required: true
        style: form
        explode: true
        schema:
          $ref:  '#/components/schemas/SomeId'
      responses:
        201:
          description: OK
components:
  schemas:
    SomeId:
      type: integer
      description: My value 123
      format: int32
      example: 123

Tested with version 2.0.1

Activity

  1. ymohdriz commented on Jul 6, 2018

    @ymohdriz
    Contributor

    Hi team,

    I have raised a PR (#752) to fix this issue.

    Thanks,
    Mohammed

  2. added a commit that references this issue on Jul 10, 2018
  3. gracekarina commented on Aug 10, 2018

    @gracekarina
    Contributor

    Fixed by #752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions