Skip to content

Commit e172a00

Browse files
authored
Merge pull request #1336 from swagger-api/issue1335
fix for issue #1335 - OAS3: Resolved YAML doesn't include Example components from domains
2 parents 8eca893 + cad2fd6 commit e172a00

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,42 @@ private void processRefContent(Map<String, MediaType> content, String $ref) {
721721
if(mediaType.getSchema() != null) {
722722
processRefSchemaObject(mediaType.getSchema(), $ref);
723723
}
724+
if(mediaType.getExamples() != null) {
725+
processRefExamples(mediaType.getExamples(), $ref);
726+
}
727+
}
728+
}
729+
730+
private void processRefExamples(Map<String, Example> examples, String $ref) {
731+
String file = $ref.split("#/")[0];
732+
for(Example example : examples.values()) {
733+
if (example.get$ref() != null) {
734+
RefFormat ref = computeRefFormat(example.get$ref());
735+
if (isAnExternalRefFormat(ref)) {
736+
processRefExample(example, $ref);
737+
} else {
738+
processRefToExternalExample(file + example.get$ref(), RefFormat.RELATIVE);
739+
}
740+
}
741+
}
742+
}
743+
744+
private void processRefExample(Example example, String externalFile) {
745+
RefFormat format = computeRefFormat(example.get$ref());
746+
747+
if (!isAnExternalRefFormat(format)) {
748+
example.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + example.get$ref(), RefFormat.RELATIVE));
749+
return;
750+
}
751+
String $ref = example.get$ref();
752+
String subRefExternalPath = getExternalPath(example.get$ref())
753+
.orElse(null);
754+
755+
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
756+
$ref = join(externalFile, example.get$ref());
757+
example.set$ref($ref);
758+
}else {
759+
processRefToExternalExample($ref, format);
724760
}
725761
}
726762

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
@@ -2261,6 +2261,16 @@ public void testParseOptionsSkipMatchesTrue() {
22612261
assertEquals(6, openAPI.getComponents().getSchemas().size());
22622262
}
22632263

2264+
@Test
2265+
public void testIssue1335() {
2266+
final ParseOptions options = new ParseOptions();
2267+
options.setResolve(true);
2268+
2269+
SwaggerParseResult result = new OpenAPIV3Parser()
2270+
.readLocation("src/test/resources/issue1335.yaml", null, options);
2271+
assertNotNull(result.getOpenAPI().getComponents().getExamples().get("ex1"));
2272+
}
2273+
22642274
@Test
22652275
public void testRegressionIssue1236() {
22662276
final ParseOptions options = new ParseOptions();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
components:
2+
responses:
3+
200ok:
4+
description: ok
5+
content:
6+
application/json:
7+
schema:
8+
type: string
9+
examples:
10+
ex1:
11+
$ref: '#/components/examples/ex1'
12+
13+
examples:
14+
ex1:
15+
value: hello
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
openapi: 3.0.2
2+
info:
3+
title: Example references
4+
version: 1.0.0
5+
paths:
6+
/foo:
7+
get:
8+
responses:
9+
'200':
10+
$ref: './domain-issue-1335.yaml#/components/responses/200ok'

0 commit comments

Comments
 (0)