swagger-api / swagger-parser

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

enum default value changes when defined in referenced file #1958

Open martin-mfg opened 1 year ago

martin-mfg commented 1 year ago

The default value of an enum changes depending on whether it is defined in the main input file or in a referenced file. Although I'm new to swagger-parser, this seems like a bug to me. Using the flatten and resolveFully options doesn't make a difference.

Example 1

input1.yaml:

openapi: 3.0.3
info:
  version: 1.0.0
  title: Issue Demo
  description: none

paths:
  /test:
    $ref: 'input1Child.yaml#/paths/~1test'

input1Child.yaml:

components:
  parameters:
    pets:
      name: pets
      in: query
      schema:
        type: array
        items:
          $ref: '#/components/schemas/Pet'
        default: [DOG, FISH]

  schemas:
    Pet:
      type: string
      enum:
        - DOG
        - CAT
        - FISH

paths:
  /test:
    get:
      description: Returns ad for a given ad ID.
      operationId: getAd
      parameters:
        - $ref: '#/components/parameters/pets'
      responses:
        '200':
          description: OK
          content:
            text/plain:
              schema:
                type: string
                example: pong```

Here the pets default value is parsed to be [DOG, FISH].

Example 2

input2.yaml:

openapi: 3.0.3
info:
  version: 1.0.0
  title: Issue Demo
  description: none

components:
  parameters:
    pets:
      name: pets
      in: query
      schema:
        type: array
        items:
          $ref: '#/components/schemas/Pet'
        default: [DOG, FISH]

  schemas:
    Pet:
      type: string
      enum:
        - DOG
        - CAT
        - FISH

paths:
  /test:
    get:
      description: Returns ad for a given ad ID.
      operationId: getAd
      parameters:
        - $ref: '#/components/parameters/pets'
      responses:
        '200':
          description: OK
          content:
            text/plain:
              schema:
                type: string
                example: pong

Here the pets default value is parsed to be ["DOG","FISH"].


I used the latest swagger-parser version, 2.1.16, for these examples. And I created a minimal executable example at https://github.com/martin-mfg/swagger-parser-demo that demos the issue.