Skip to content

Commit 3ee7b0c

Browse files
authored
Merge pull request #897 from joeljons/allOf-example-resolving
ResolveFully creates array example for allOf
2 parents 383f0d6 + 5425872 commit 3ee7b0c

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.slf4j.LoggerFactory;
2121

2222
import java.util.ArrayList;
23-
import java.util.Arrays;
2423
import java.util.HashMap;
2524
import java.util.HashSet;
2625
import java.util.LinkedHashMap;
@@ -406,7 +405,11 @@ public Schema resolveSchema(Schema schema) {
406405
}
407406
}
408407
}
409-
model.setExample(examples);
408+
if (schema.getExample() != null) {
409+
model.setExample(schema.getExample());
410+
} else if (!examples.isEmpty()) {
411+
model.setExample(examples);
412+
}
410413
return model;
411414
} else {
412415
// User don't want to aggregate composed schema, we only solve refs

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.swagger.v3.parser.test;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
67
import com.github.tomakehurst.wiremock.WireMockServer;
78
import com.github.tomakehurst.wiremock.client.WireMock;
89
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
10+
import io.swagger.v3.core.util.Json;
911
import io.swagger.v3.oas.models.Components;
1012
import io.swagger.v3.oas.models.OpenAPI;
1113
import io.swagger.v3.oas.models.Operation;
@@ -740,6 +742,20 @@ public void testComposedSchemaAdjacentWithExamples(@Injectable final List<Author
740742
Assert.assertTrue(examples.size() == 2);
741743
}
742744

745+
@Test
746+
public void allOfExampleGeneration(@Injectable final List<AuthorizationValue> auths) throws JsonProcessingException {
747+
ParseOptions options = new ParseOptions();
748+
options.setResolve(true);
749+
options.setResolveFully(true);
750+
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/simpleAllOf.yaml", null, options);
751+
752+
Assert.assertNotNull(openAPI);
753+
Object withoutExample = openAPI.getPaths().get("/foo").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getExample();
754+
Assert.assertNull(withoutExample);
755+
756+
Object withExample = openAPI.getPaths().get("/bar").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getExample();
757+
Assert.assertEquals("{\"someProperty\":\"ABC\",\"someOtherProperty\":42}", Json.mapper().writeValueAsString(withExample));
758+
}
743759

744760
@Test
745761
public void testSwaggerResolver(@Injectable final OpenAPI swagger,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
openapi: 3.0.0
2+
info:
3+
version: "1.0.0"
4+
paths:
5+
/foo:
6+
get:
7+
responses:
8+
'200':
9+
content:
10+
application/json:
11+
schema:
12+
allOf:
13+
- $ref: '#/components/schemas/FirstType'
14+
- $ref: '#/components/schemas/SecondType'
15+
/bar:
16+
get:
17+
responses:
18+
'200':
19+
content:
20+
application/json:
21+
schema:
22+
allOf:
23+
- $ref: '#/components/schemas/FirstType'
24+
- $ref: '#/components/schemas/SecondType'
25+
example:
26+
someProperty: ABC
27+
someOtherProperty: 42
28+
components:
29+
schemas:
30+
FirstType:
31+
type: object
32+
properties:
33+
someProperty:
34+
type: string
35+
example: abc
36+
SecondType:
37+
type: object
38+
properties:
39+
someOtherProperty:
40+
type: integer
41+
example: 10

0 commit comments

Comments
 (0)