Skip to content

Commit 478f392

Browse files
committed
fix: resolve nested example references in parameter definitions
1 parent ef2cf16 commit 478f392

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,16 @@ protected void updateRefs(Parameter param, String pathRef) {
199199
if (param.get$ref() != null){
200200
param.set$ref(computeRef(param.get$ref(), pathRef));
201201
}
202-
if(param.getSchema() != null) {
202+
if (param.getSchema() != null) {
203203
updateRefs(param.getSchema(), pathRef);
204204
}
205-
if(param.getContent() != null) {
205+
if (param.getExamples() != null) {
206+
Map<String, Example> examples = param.getExamples();
207+
for (Example example : examples.values()) {
208+
updateRefs(example, pathRef);
209+
}
210+
}
211+
if (param.getContent() != null) {
206212
Map<String, MediaType> content = param.getContent();
207213
for (String key: content.keySet()) {
208214
MediaType mediaType = content.get(key);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,24 @@ public void testIssue2081() {
33133313
assertEquals(openAPI.getComponents().getSchemas().get("PetCreate").getProperties().size(), 2);
33143314
}
33153315

3316+
@Test(description = "should resolve nested relative references in examples")
3317+
public void testIssue2229() {
3318+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
3319+
ParseOptions options = new ParseOptions();
3320+
options.setResolve(true);
3321+
SwaggerParseResult parseResult = openApiParser.readLocation("issue-2229/openapi-rest.yml", null, options);
3322+
OpenAPI openAPI = parseResult.getOpenAPI();
3323+
3324+
Map<String, Example> parameterExamples = openAPI.getPaths().get("/api/v1/rules").getGet().getParameters().get(0).getExamples();
3325+
assertEquals(parameterExamples.get("default").get$ref(), "#/components/examples/StatusQueryParameter");
3326+
3327+
assertNotNull(openAPI.getComponents(), "should have resolved the components");
3328+
assertNotNull(openAPI.getComponents().getExamples(), "should have resolved the examples");
3329+
Set<String> exampleNames = new HashSet<>();
3330+
exampleNames.add("StatusQueryParameter");
3331+
assertEquals(openAPI.getComponents().getExamples().keySet(), exampleNames);
3332+
}
3333+
33163334
@Test(description = "responses should be inline")
33173335
public void testFullyResolveResponses() {
33183336
ParseOptions options = new ParseOptions();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
StatusQueryParameter:
2+
value: 'ACTIVE'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Rules
4+
contact:
5+
name: myname
6+
7+
description: API desgined for creating, updating, retrieving, and deleting rules and reference entities (reasons, processes).
8+
version: 1.0.2
9+
security:
10+
- itxBearerAuth: []
11+
servers:
12+
- description: The localhost API server.
13+
url: '{protocol}://localhost:{port}'
14+
variables:
15+
protocol:
16+
default: 'http'
17+
port:
18+
default: "8080"
19+
tags:
20+
- name: Rule
21+
description: A rule defines when a product should be restricted from certain business processes based on specific conditions.
22+
paths:
23+
/api/v1/rules:
24+
$ref: './paths/rules/rules.yml'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
get:
2+
tags:
3+
- Rule
4+
summary: Search rules (paginated)
5+
description: My description
6+
operationId: searchRulesPaginated
7+
parameters:
8+
- name: status
9+
in: query
10+
description: Filter by rule status (ACTIVE, INACTIVE, ARCHIVED).
11+
required: false
12+
schema:
13+
type: string
14+
enum: [ ACTIVE, INACTIVE, ARCHIVED ]
15+
description: "Current lifecycle status of the rule."
16+
examples:
17+
default:
18+
$ref: '../../examples/rule/rule-parameters.yml#/StatusQueryParameter'
19+
responses:
20+
'200':
21+
description: OK - Paginated list of rules successfully retrieved.
22+
content:
23+
application/json:
24+
schema:
25+
type: object

0 commit comments

Comments
 (0)