Skip to content

Commit 1f56ef3

Browse files
authored
Merge pull request #1079 from darrellmay/master
Issue #1078
2 parents b83b171 + 3b00f29 commit 1f56ef3

File tree

5 files changed

+159
-3
lines changed

5 files changed

+159
-3
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,25 @@ public List<Parameter> processParameters(List<Parameter> parameters) {
161161
}
162162

163163
}
164+
else if(parameter.getContent() != null){
165+
Map<String,MediaType> content = parameter.getContent();
166+
for( String mediaName : content.keySet()) {
167+
MediaType mediaType = content.get(mediaName);
168+
if(mediaType.getSchema()!= null) {
169+
schema = mediaType.getSchema();
170+
if (schema != null) {
171+
schemaProcessor.processSchema(schema);
172+
}
173+
}
174+
if(mediaType.getExamples() != null) {
175+
for(Example ex: mediaType.getExamples().values()){
176+
exampleProcessor.processExample(ex);
177+
}
178+
}
179+
}
180+
}
164181
}
165182

166183
return processedPathLevelParameters;
167184
}
168-
}
185+
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ParameterProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testProcessParameters_TypesThatAreNotRefOrBody(@Injectable final Hea
4242
@Injectable final PathParameter pathParameter) throws Exception {
4343
expectedModelProcessorCreation();
4444
new Expectations() {
45-
{
45+
{
4646
headerParameter.getSchema();
4747
result = null;
4848
headerParameter.getContent();

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.swagger.v3.oas.models.Components;
1212
import io.swagger.v3.oas.models.OpenAPI;
1313
import io.swagger.v3.oas.models.Operation;
14+
import io.swagger.v3.oas.models.Paths;
1415
import io.swagger.v3.oas.models.PathItem;
1516
import io.swagger.v3.oas.models.headers.Header;
1617
import io.swagger.v3.oas.models.links.Link;
@@ -1912,6 +1913,68 @@ public void shouldParseApiWithMultipleParameterReferences() {
19121913
}
19131914

19141915
@Test
1916+
public void shouldParseApiWithParametersUsingContentvsSchema() {
1917+
// Tests that the content method of specifying the format of a parameter
1918+
// gets resolved.
1919+
// Test checks if an API's single parameter of array type gets fully resolved to
1920+
// referenced definitions.
1921+
String location = "src/test/resources/issue-1078/api.yaml";
1922+
ParseOptions options = new ParseOptions();
1923+
options.setResolve(true);
1924+
// This test uses an Array in the parameters, test if it get's fully resolved.
1925+
options.setResolveFully(true);
1926+
OpenAPIV3Parser tested = new OpenAPIV3Parser();
1927+
1928+
// Parse yaml
1929+
SwaggerParseResult result = tested.readLocation(location, emptyList(), options);
1930+
1931+
OpenAPI api = result.getOpenAPI();
1932+
Paths paths = api.getPaths();
1933+
1934+
// First ensure all schemas were resolved, this is important when this library
1935+
// is used to generate code
1936+
Components components = api.getComponents();
1937+
assertNotNull(components);
1938+
assertThat(components.getSchemas().size(), equalTo(4));
1939+
assertNotNull(components.getSchemas().get("LocationType"));
1940+
assertNotNull(components.getSchemas().get("Lat"));
1941+
assertNotNull(components.getSchemas().get("Long"));
1942+
assertNotNull(components.getSchemas().get("SearchResult"));
1943+
1944+
PathItem apiEndpoint = paths.get("/api-endpoint-1");
1945+
List<Parameter> parameters = apiEndpoint.getGet().getParameters();
1946+
1947+
// Ensure there's only one parameter in this test
1948+
assertThat(parameters.size(), equalTo(1));
1949+
1950+
// We are testing content for a parameter so make sure its there.
1951+
Content content = parameters.get(0).getContent();
1952+
assertNotNull(content);
1953+
// spec says only one content is permitted in 3.x
1954+
assertThat( content.size(), equalTo(1));
1955+
1956+
// Ensure there's a media type
1957+
MediaType mediaType = content.entrySet().iterator().next().getValue();
1958+
assertNotNull(mediaType);
1959+
1960+
// This test has a single parameter of type array
1961+
Schema parameterSchema = mediaType.getSchema();
1962+
Assert.assertTrue(parameterSchema instanceof ArraySchema);
1963+
ArraySchema arraySchema = (ArraySchema)parameterSchema;
1964+
1965+
// Test if the item schema was resolved properly
1966+
Schema itemSchema = arraySchema.getItems();
1967+
assertNotNull(itemSchema);
1968+
Assert.assertTrue(itemSchema instanceof ObjectSchema);
1969+
1970+
// Ensure the referenced item's schema has been resolved.
1971+
ObjectSchema objSchema = (ObjectSchema)itemSchema;
1972+
Map<String, Schema> objectItemSchemas = objSchema.getProperties();
1973+
assertThat( objectItemSchemas.size(), equalTo(2));
1974+
Assert.assertTrue(objectItemSchemas.get("lat") instanceof IntegerSchema);
1975+
Assert.assertTrue(objectItemSchemas.get("long") instanceof IntegerSchema);
1976+
1977+
}
19151978
public void testIssue1063() {
19161979
// given
19171980
String location = "src/test/resources/issue-1063/openapi.yaml";
@@ -1949,4 +2012,4 @@ public void testResolveFullyMap() {
19492012
private static int getDynamicPort() {
19502013
return new Random().ints(10000, 20000).findFirst().getAsInt();
19512014
}
1952-
}
2015+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
version: '1.0.0'
4+
title: 'Simple Test'
5+
description: 'Test of schema vs content for Query Parameters'
6+
7+
paths:
8+
/api-endpoint-1:
9+
get:
10+
summary: Test Service API Endpoint 1
11+
operationId: SimpleTestEndpoint1
12+
tags:
13+
- SimpleTestEndpoint(1)
14+
parameters:
15+
- name: target-locations-list
16+
in: query
17+
description: list of locations to search near
18+
content:
19+
application/json:
20+
schema:
21+
type: array
22+
items:
23+
$ref: './common.yaml#/components/schemas/LocationType'
24+
minItems: 1
25+
responses:
26+
'200':
27+
description: Expected response to a valid request
28+
content:
29+
application/json:
30+
schema:
31+
$ref: './common.yaml#/components/schemas/SearchResult'
32+
'404':
33+
$ref: './common.yaml#/components/responses/404'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
openapi: 3.0.0
2+
info:
3+
version: '1.0.0'
4+
title: 'Common Data Types'
5+
description: 'Common Data Types for Test'
6+
7+
paths: {}
8+
components:
9+
schemas:
10+
11+
LocationType:
12+
type: object
13+
properties:
14+
lat:
15+
$ref: '#/components/schemas/Lat'
16+
long:
17+
$ref: '#/components/schemas/Long'
18+
required:
19+
- lat
20+
- long
21+
22+
Lat:
23+
type: integer
24+
Long:
25+
type: integer
26+
27+
SearchResult:
28+
type: object
29+
required:
30+
- simpleTestResults
31+
properties:
32+
resultId:
33+
type: integer
34+
simpleTestResults:
35+
type: array
36+
items:
37+
type: string
38+
39+
40+
responses:
41+
'404':
42+
description: Not Found
43+
type: string

0 commit comments

Comments
 (0)