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

External reference in query parameter content does not get resolved

Open jphorx opened this issue 7 years ago • 1 comments

Issue found with swagger-parser version 2.0.12. This may also be related to https://github.com/swagger-api/swagger-ui/issues/4442, as it seems content is not well supported in some areas.

Given a common.yaml:

openapi: "3.0.0"
info:
  version: 15.3.0
  title: "Common Data Types"
paths: {}  
components:
  schemas:
    PlmnId:
      type: object
      properties:
        mcc:
          type: string
        mnc:
          type: string

and a simple test1.yaml that references it from a content parameter:

openapi: "3.0.0"
info:
  version: 15.3.0
  title: test
paths:
  /my-app:
    get:
      parameters:
      - name: target-plmn-list
        in: query
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: 'common.yaml#/components/schemas/PlmnId'
                minItems: 1
      responses:
        '200':
          description: Expected response to a valid request
          schema:
            type: string

The following test code shows the external reference is not resolved

         ParseOptions options = new ParseOptions();
         options.setResolve(true);
         
         OpenAPIV3Parser parser = new OpenAPIV3Parser();
         SwaggerParseResult result = parser.readLocation("test1.yaml", Collections.emptyList(), options);
         OpenAPI test1 = result.getOpenAPI();
   
         ArraySchema schema = (ArraySchema)test1.getPaths().get("/my-app").getGet().getParameters().get(0).getContent().get("application/json").getSchema();
         System.out.println("items/$ref=" + schema.getItems().get$ref());
         System.out.println("components=" + test1.getComponents());

Output:

items/$ref=./common.yaml#/components/schemas/PlmnId
components=null

If content is not used (see test2.yaml below) with slightly tweaked test code, the external reference can be resolved:

openapi: "3.0.0"
info:
  version: 15.3.0
  title: test
paths:
  /my-app:
    get:
      parameters:
      - name: target-plmn-list
        in: query
        schema:
          type: array
          items:
            $ref: 'common.yaml#/components/schemas/PlmnId'
            minItems: 1
      responses:
        '200':
          description: Expected response to a valid request
          schema:
            type: string

Test code:

         ParseOptions options = new ParseOptions();
         options.setResolve(true);
         
         OpenAPIV3Parser parser = new OpenAPIV3Parser();
         SwaggerParseResult result = parser.readLocation("test2.yaml", Collections.emptyList(), options);
         OpenAPI test2 = result.getOpenAPI();
         
         ArraySchema schema = (ArraySchema)test2.getPaths().get("/my-app").getGet().getParameters().get(0).getSchema();
         System.out.println("items/$ref=" + schema.getItems().get$ref());
         System.out.println("components=" + test2.getComponents().getSchemas().keySet());

Output:

items/$ref=#/components/schemas/PlmnId
components=[PlmnId]

A potential fix is to add support for Content in io.swagger.v3.parser.processors.ParameterProcessor.processParameters(List<Parameter> parameters). By adding the new code block for Content, then the original test code gives the expected output.

        for (Parameter parameter : processedPathLevelParameters) {
            Schema schema = parameter.getSchema();
            if(schema != null){
                schemaProcessor.processSchema(schema);
            }
            Content content = parameter.getContent();
            if (content != null) {
               for (MediaType mediaType : content.values()) {
                  schema = mediaType.getSchema();
                  schemaProcessor.processSchema(schema);
               }
            }
        }

jphorx avatar May 06 '19 18:05 jphorx

Hi @jphorx thanks for this spec, it was faster to test and fix, you can check the solution in PR #1109. Please let us know if this issue keeps happening.

gracekarina avatar May 29 '19 22:05 gracekarina