Skip to content

Commit d0b2935

Browse files
jorgerodfrantuma
authored andcommitted
Fix resolve $refs in headers
1 parent 5c6b141 commit d0b2935

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) {
313313
if (response.getLinks() != null) {
314314
processRefLinks(response.getLinks(), $ref);
315315
}
316+
if (response.getHeaders() != null) {
317+
processRefHeaders(response.getHeaders(), $ref);
318+
}
316319
}
317320
}
318321
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3476,4 +3476,16 @@ public void testIssue1902_component_example_not_parse_ordinal_json() {
34763476
Example expectedExample = openAPI.getComponents().getExamples().get("Things");
34773477
assertNotNull(expectedExample);
34783478
}
3479+
3480+
@Test
3481+
public void testIssue_1746_headers_relative_paths() {
3482+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
3483+
ParseOptions options = new ParseOptions();
3484+
options.setResolve(true);
3485+
SwaggerParseResult parseResult = openApiParser.readLocation("issue-1746/petstore.yml", null, options);
3486+
OpenAPI openAPI = parseResult.getOpenAPI();
3487+
3488+
3489+
assertEquals(openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getHeaders().get("x-next").get$ref(), "#/components/headers/LocationInHeaders");
3490+
}
34793491
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
components:
2+
schemas:
3+
Pet:
4+
type: object
5+
required:
6+
- id
7+
- name
8+
properties:
9+
id:
10+
type: integer
11+
format: int64
12+
name:
13+
type: string
14+
tag:
15+
type: string
16+
Pets:
17+
type: array
18+
items:
19+
$ref: "#/components/schemas/Pet"
20+
Error:
21+
type: object
22+
required:
23+
- code
24+
- message
25+
properties:
26+
code:
27+
type: integer
28+
format: int32
29+
message:
30+
type: string
31+
32+
links:
33+
userRepository:
34+
operationId: getRepository
35+
requestBody: '$response.body#/slug'
36+
headers:
37+
X-Rate-Limit:
38+
description: The number of allowed requests in the current period
39+
schema:
40+
type: integer
41+
headers:
42+
LocationInHeaders:
43+
description: A link to the next page of responses
44+
schema:
45+
type: string
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
get:
2+
summary: List all pets
3+
operationId: listPets
4+
tags:
5+
- pets
6+
parameters:
7+
- name: myParam
8+
in: query
9+
description: Param
10+
required: true
11+
schema:
12+
$ref: 'def.yml#/components/schemas/Pet'
13+
- name: myParam2
14+
in: query
15+
description: Param2
16+
required: true
17+
schema:
18+
$ref: './def.yml#/components/schemas/Pet'
19+
- name: myParam3
20+
in: query
21+
description: Param3
22+
required: true
23+
schema:
24+
$ref: '../pets/def.yml#/components/schemas/Pet'
25+
responses:
26+
"200":
27+
description: A paged array of pets
28+
headers:
29+
x-next:
30+
$ref: "./def.yml#/components/headers/LocationInHeaders"
31+
content:
32+
application/json:
33+
schema:
34+
$ref: "./def.yml#/components/schemas/Pets"
35+
default:
36+
description: unexpected error
37+
content:
38+
application/json:
39+
schema:
40+
$ref: "./def.yml#/components/schemas/Error"
41+
post:
42+
summary: Create a pet
43+
operationId: createPets
44+
tags:
45+
- pets
46+
responses:
47+
'201':
48+
description: New guest added to event list
49+
content:
50+
application/json:
51+
schema:
52+
properties:
53+
test:
54+
type: string
55+
examples:
56+
testExamples:
57+
$ref: "../petstore.yml#/components/schemas/DateWithExample"
58+
links:
59+
userRepository:
60+
$ref: 'def.yml#/components/links/userRepository'
61+
default:
62+
description: unexpected error
63+
content:
64+
application/json:
65+
schema:
66+
$ref: "./def.yml#/components/schemas/Error"
67+
example:
68+
$ref: "../petstore.yml#/components/schemas/DateWithExample"
69+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
$ref: "./pets/pets.yml"
12+
13+
components:
14+
schemas:
15+
Date:
16+
type: string
17+
format: date
18+
DateWithExample:
19+
$ref: '#/components/schemas/Date'
20+
description: Date schema extended with a `default` value... Or not?
21+
default: 2000-01-01

0 commit comments

Comments
 (0)