swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

Paths defined as $ref wont add parameters to each operation

Open frankbille opened this issue 2 years ago • 1 comments

Problem

When organizing path definitions in different files like the example below, the parameters will not be passed from the top-level /ref/test/{id}/toplevelparam definition to the GET operation.

api.yaml:

openapi: 3.0.0

paths:
  /ref/test/{id}/toplevelparam:
    $ref: "./test-endpoints.yaml#/paths/~1ref~1test~1{id}~1toplevelparam"

test-endpoints.yaml:

openapi: 3.0.0

paths:
  /ref/test/{id}/toplevelparam:
    parameters:
      - in: path
        name: id
        required: true
        schema:
          type: string
          enum:
            - one
            - two
    get:
      summary: Test of path params defined on top level
      responses:
        200:
          description: OK
      tags:
        - Tests

The issue is that the passing of parameters to the operations is done before parsing of the $refs.

Solution

By calling addParametersToEachOperation after all the $refs has been processed, the parameters can be correctly added.

I have included simple tests of PathProcessor for this specific case. I am not sure if I have structured the tests correctly or if they make more sense to include in OpenAPIResolverTest

frankbille avatar May 24 '22 08:05 frankbille

There are two tests that fail as a consequence of the change to PathProcessor, because they expect there to be parameters on top level. When PathProcessor.addParametersToEachOperation is called it ends by setting the top level parameters to null.

I think the test needs to be changed because it would never have passed if the schema was all internal.

frankbille avatar May 24 '22 08:05 frankbille

thanks for this PR!

gracekarina avatar Nov 03 '22 12:11 gracekarina