swagger-api / swagger-parser

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

References to responses defined in an external file aren't parsed #1561

Closed martininigo closed 3 years ago

martininigo commented 3 years ago

I have a main yaml file with the specification of the service and an other yaml with schemas and responses. When I use OpenAPIParser ().readContents method, the schemes are correctly parsed and added to openAPI.components.shcemas map but the responses aren't added to openAPI.components.responses map.

My main yml is:

openapi: 3.0.0
info:
  description: The Trazabilidad API offers the functionalities of the new trace regardless of the channel in which it is in invoked.
  version: 1.0.2
  title: ApiTrazabilidad

tags:
  - name: Trazabilidad
    description: Trazabilidad API
paths:
  '/operationalServices/tracks/scopes/{scopeId}/processes/{processId}/petitions':
    post:
      tags:
        - createPetition
      summary: Creates a new instance in the traceability operation.
      operationId: createPetition
      description: Creates a new instance in the traceability operation.
      parameters:
        - in: path
          name: processId
          description: instance process ID
          required: true
          schema:
            type: integer
      requestBody:
        $ref: '#/components/requestBodies/TrazabilidadBodyRequest'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BodyOutResponse'
        '400':
          $ref: >-
            /exceptions.yml#/components/responses/400
        '404':
          $ref: >-
            /exceptions.yml#/components/responses/404
        '500':
          $ref: >-
            /exceptions.yml#/components/responses/500

components:
  requestBodies:
    TrazabilidadBodyRequest:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/TrazabilidadBodyRequest'
      description: body request
      required: true
  schemas:
    BodyOutResponse:
      type: object
      properties:
        petitionId:
          type: string
          description: The ID of the request that has been created
          example: "971205125921"
    TrazabilidadBodyRequest:
      type: object
      properties:
        action:
          type: string
          description: Action to be performed. values -- alta , mod , fin , baja
          example: "alta"          
          example: "2021-01-25"
...

exceptions.yml

components:
  schemas:
    Exception:
      type: object
    Rfc7807Exception:
      description: The Rfc7807 specification for exceptions.
      allOf:
        - $ref: '#/components/schemas/Exception'
        - type: object
          properties:
            type:
              type: string
              description: A URI reference that identifies the problem type.
          required: 
          - type
          - title
          - status
          - detail
    AbsisExceptionType:
      type: string
      enum: [VALIDATION, BUSINESS, RUNTIME, CHALLENGE, NOTFOUND, UNAUTHORIZED, AUTHENTICATION]
      description: The possible types of an AbsisException.
    AbsisFieldError:
      type: object
      description: The representation of a validation error.
      properties:
        code:
          type: string
          description: The error code.
      example:
        code: request
        parameters:
        - codes:
          - aCode
          - otherCode
        value: NotNull
    AbsisException:
      description: Representation of an exception.
      allOf:
        - $ref: '#/components/schemas/Rfc7807Exception'
        - type: object
          properties:
            _class:
              type: string
              description: The canonical name of the class AbsisException.
            cause:
              $ref: '#/components/schemas/AbsisException'
              description: The AbsisException wrapping the exception that originated this exception.
  responses:
    '400':
      description: Error when validating input parameters or request body.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AbsisException'
    '404':
      description: The endpoint does not exists or the attempted resource does not exist.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AbsisException'
    '500':
      description: Unexpected error occurred at one of the layers of the microservice, controller, business service, repository etc.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AbsisException' 

If I copy the responses from the external file and add them to the main file it works fine. So apparently the problem is precisely when the responses are defined in an external file. The strange thing is that the rest of the components defined in the external file processes them correctly.

gracekarina commented 3 years ago

Hi @martininigo I tested the specs, and I think the issue is that the ref /exceptions.yml#/components/responses/... has a slash at the begining, I removed ir or place a .before the slash and the resolving was ok. Please let us know if this helped solved your issue. Please review the test I commited, so we are sure we are talking of the same scenario.


'400':
          $ref: >-
            exceptions.yml#/components/responses/400
        '404':
          $ref: >-
            exceptions.yml#/components/responses/404
        '500':
          $ref: >-
            exceptions.yml#/components/responses/500
gracekarina commented 3 years ago

Hi @martininigo you can find now in the components section the responses that were in the relative file. thanks for reporting this issue.