Skip to content

Commit 03ff021

Browse files
authored
Merge pull request #919 from ymohdriz/branch_issue_915
Fix for issue 918
2 parents b3e3908 + 4959327 commit 03ff021

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

Lines changed: 35 additions & 1 deletion
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.links.Link;
910
import io.swagger.v3.oas.models.media.ArraySchema;
1011
import io.swagger.v3.oas.models.media.ComposedSchema;
@@ -50,6 +51,7 @@ public ResolverFully(boolean aggregateCombinators) {
5051
private Map<String, Example> examples;
5152
private Map<String, Parameter> parameters;
5253
private Map<String, RequestBody> requestBodies;
54+
private Map<String, Header> headers;
5355
private Map<String, Link> links;
5456

5557
public void resolveFully(OpenAPI openAPI) {
@@ -74,6 +76,12 @@ public void resolveFully(OpenAPI openAPI) {
7476
}
7577
}
7678

79+
if (openAPI.getComponents() != null && openAPI.getComponents().getHeaders() != null) {
80+
headers = openAPI.getComponents().getHeaders();
81+
if (headers == null) {
82+
headers = new HashMap<>();
83+
}
84+
}
7785

7886
if (openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
7987
parameters = openAPI.getComponents().getParameters();
@@ -85,7 +93,6 @@ public void resolveFully(OpenAPI openAPI) {
8593
links = openAPI.getComponents().getLinks();
8694
if (links == null) {
8795
links = new HashMap<>();
88-
8996
}
9097
}
9198

@@ -174,6 +181,21 @@ public void resolvePath(PathItem pathItem){
174181
}
175182
}
176183
}
184+
185+
Map<String, Header> headers = response.getHeaders();
186+
if (headers != null) {
187+
for (Map.Entry<String, Header> header : headers.entrySet()) {
188+
Header value = header.getValue();
189+
Header resolvedValue = value.get$ref() != null ? resolveHeader(value) : value;
190+
Map<String, Example> examples = resolvedValue.getExamples();
191+
if(examples != null) {
192+
Map<String,Example> resolved = resolveExample(examples);
193+
resolvedValue.setExamples(resolved);
194+
}
195+
header.setValue(resolvedValue);
196+
}
197+
}
198+
177199
Map<String, Link> links = response.getLinks();
178200
if (links != null) {
179201
for (Map.Entry<String, Link> link : links.entrySet()) {
@@ -187,6 +209,18 @@ public void resolvePath(PathItem pathItem){
187209
}
188210
}
189211

212+
public Header resolveHeader(Header header){
213+
RefFormat refFormat = computeRefFormat(header.get$ref());
214+
String $ref = header.get$ref();
215+
if (!isAnExternalRefFormat(refFormat)){
216+
if (headers != null && !headers.isEmpty()) {
217+
String referenceKey = computeDefinitionName($ref);
218+
return headers.getOrDefault(referenceKey, header);
219+
}
220+
}
221+
return header;
222+
}
223+
190224
public Link resolveLink(Link link){
191225
RefFormat refFormat = computeRefFormat(link.get$ref());
192226
String $ref = link.get$ref();

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

Lines changed: 10 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.links.Link;
1617
import io.swagger.v3.oas.models.media.*;
1718
import io.swagger.v3.oas.models.examples.Example;
@@ -1539,6 +1540,15 @@ public void shouldParseRequestBody() {
15391540
}
15401541

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