Skip to content

Commit 4ff3854

Browse files
committed
Fix for issue 918
Headers Internal Ref Not Resolved
1 parent a566237 commit 4ff3854

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.v3.oas.models.PathItem;
66
import io.swagger.v3.oas.models.callbacks.Callback;
77
import io.swagger.v3.oas.models.examples.Example;
8+
import io.swagger.v3.oas.models.headers.Header;
89
import io.swagger.v3.oas.models.media.ArraySchema;
910
import io.swagger.v3.oas.models.media.ComposedSchema;
1011
import io.swagger.v3.oas.models.media.MediaType;
@@ -13,6 +14,7 @@
1314
import io.swagger.v3.oas.models.parameters.Parameter;
1415
import io.swagger.v3.oas.models.parameters.RequestBody;
1516
import io.swagger.v3.oas.models.responses.ApiResponse;
17+
import io.swagger.v3.oas.models.responses.ApiResponses;
1618
import io.swagger.v3.parser.models.RefFormat;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
@@ -48,6 +50,7 @@ public ResolverFully(boolean aggregateCombinators) {
4850
private Map<String, Schema> resolvedModels = new HashMap<>();
4951
private Map<String, Example> examples;
5052
private Map<String, RequestBody> requestBodies;
53+
private Map<String, Header> headers;
5154

5255

5356
public void resolveFully(OpenAPI openAPI) {
@@ -72,6 +75,13 @@ public void resolveFully(OpenAPI openAPI) {
7275
}
7376
}
7477

78+
if (openAPI.getComponents() != null && openAPI.getComponents().getHeaders() != null) {
79+
headers = openAPI.getComponents().getHeaders();
80+
if (headers == null) {
81+
headers = new HashMap<>();
82+
}
83+
}
84+
7585
if(openAPI.getPaths() != null) {
7686
for (String pathname : openAPI.getPaths().keySet()) {
7787
PathItem pathItem = openAPI.getPaths().get(pathname);
@@ -138,9 +148,10 @@ public void resolvePath(PathItem pathItem){
138148
}
139149
}
140150
// responses
141-
if(op.getResponses() != null) {
142-
for(String code : op.getResponses().keySet()) {
143-
ApiResponse response = op.getResponses().get(code);
151+
ApiResponses responses = op.getResponses();
152+
if(responses != null) {
153+
for(String code : responses.keySet()) {
154+
ApiResponse response = responses.get(code);
144155
if (response.getContent() != null) {
145156
Map<String, MediaType> content = response.getContent();
146157
for(String mediaType: content.keySet()){
@@ -155,11 +166,31 @@ public void resolvePath(PathItem pathItem){
155166
}
156167
}
157168
}
169+
Map<String, Header> headers = response.getHeaders();
170+
if (headers != null) {
171+
for (Map.Entry<String, Header> header : headers.entrySet()) {
172+
Header value = header.getValue();
173+
Header resolvedValue = value.get$ref() != null ? resolveHeader(value) : value;
174+
header.setValue(resolvedValue);
175+
}
176+
}
158177
}
159178
}
160179
}
161180
}
162181

182+
public Header resolveHeader(Header header){
183+
RefFormat refFormat = computeRefFormat(header.get$ref());
184+
String $ref = header.get$ref();
185+
if (!isAnExternalRefFormat(refFormat)){
186+
if (headers != null && !headers.isEmpty()) {
187+
String referenceKey = computeDefinitionName($ref);
188+
return headers.getOrDefault(referenceKey, header);
189+
}
190+
}
191+
return header;
192+
}
193+
163194
public RequestBody resolveRequestBody(RequestBody requestBody){
164195
RefFormat refFormat = computeRefFormat(requestBody.get$ref());
165196
String $ref = requestBody.get$ref();

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.swagger.v3.oas.models.OpenAPI;
1313
import io.swagger.v3.oas.models.Operation;
1414
import io.swagger.v3.oas.models.PathItem;
15+
import io.swagger.v3.oas.models.headers.Header;
1516
import io.swagger.v3.oas.models.media.*;
1617
import io.swagger.v3.oas.models.examples.Example;
1718
import io.swagger.v3.oas.models.media.ArraySchema;
@@ -1537,6 +1538,16 @@ public void shouldParseRequestBody() {
15371538
assertEquals(actualPathContent, actualComponentContent);
15381539
}
15391540

1541+
@Test
1542+
public void testIssue915() {
1543+
ParseOptions parseOptions = new ParseOptions();
1544+
parseOptions.setResolveFully(true);
1545+
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/issue_918.yaml", null, parseOptions);
1546+
Map<String, Header> headers = openAPI.getPaths().get("/2.0/users/").getGet().getResponses().get("200").getHeaders();
1547+
String description = headers.get("X-Rate-Limit").getDescription();
1548+
assertEquals(description, "The number of allowed requests in the current period");
1549+
}
1550+
15401551

15411552
private static int getDynamicPort() {
15421553
return new Random().ints(10000, 20000).findFirst().getAsInt();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
openapi: 3.0.0
2+
info:
3+
title: headers in components
4+
version: 1.0.0
5+
paths:
6+
/2.0/users/:
7+
get:
8+
operationId: getUserByName
9+
responses:
10+
'200':
11+
description: The User
12+
headers:
13+
X-Rate-Limit:
14+
$ref: "#/components/headers/X-Rate-Limit-Ref"
15+
components:
16+
headers:
17+
X-Rate-Limit-Ref:
18+
description: The number of allowed requests in the current period
19+
schema:
20+
type: integer

0 commit comments

Comments
 (0)