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

Headers in responses do not get their refs resolved in the same way as other headers do

Open AndySpaven opened this issue 3 years ago • 0 comments

The function updateRefs(apiResponse, pathRef) does not update refs for headers in the response whilst it does this correctly for examples and content. As a result relative references in this section cause errors turning the OpenApi spec file into a model as these then get mangled when joined with the base url.

For example:

application.yaml
commonHeaders.yaml
endpoints/
  list.yaml

list.yaml would then reference common headers

    "200":
      description: some text here
      headers:
        X-CorrelationId:
          $ref: '../commonHeaders.yaml#/components/parameters/correlationId'
       ... <more response definition>

The final url to resolve commonHeaders.yaml looses the last part of the {baseUrl}/../commonHeaders.yaml rather than having {baseUrl}/endpoints/../commonHeaders.yaml

Looking at the code for updateRefs locally we can fix it by changing/adding the following code (and all existing tests pass) but this may not be the correct or ideal way to address this (not knowing the whole codebase).

        if (response.get$ref() != null){
        	response.set$ref(computeRef(response.get$ref(), pathRef));
        }
        if(response.getContent() != null) {
            Map<String, MediaType> content = response.getContent();
            for (String key: content.keySet()) {
                MediaType mediaType = content.get(key);
                if (mediaType.getSchema() != null) {
                    updateRefs(mediaType.getSchema(), pathRef);
                }
                Map<String, Example> examples = content.get(key).getExamples();
                if (examples != null) {
                    for( Example example:examples.values()){
                        updateRefs(example, pathRef);
                    }
                }
            }
        }

+        Map<String, Header> headers = response.getHeaders();
+        if(headers != null) {
+            for( Header header : headers.values()) {
+                updateRefs(header, pathRef);
+            }
+        }
    }

+    protected void updateRefs(Header header, String pathRef) {
+        if (header.get$ref() != null) {
+            header.set$ref(computeRef(header.get$ref(), pathRef));
+        }
+    }

We believe that the OpenAPI specification supports the way that our specification files are structured.

AndySpaven avatar Jun 13 '22 12:06 AndySpaven