Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit d05d320

Browse files
author
Darrell May
committed
Adds support for content vs schema when describing more complex parameter definitionsa
1 parent b06bd06 commit d05d320

5 files changed

Lines changed: 171 additions & 3 deletions

File tree

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
@@ -143,8 +143,25 @@ public List<Parameter> processParameters(List<Parameter> parameters) {
143143
if(schema != null){
144144
schemaProcessor.processSchema(schema);
145145
}
146+
else if(parameter.getContent() != null){
147+
Map<String,MediaType> content = parameter.getContent();
148+
for( String mediaName : content.keySet()) {
149+
MediaType mediaType = content.get(mediaName);
150+
if(mediaType.getSchema()!= null) {
151+
schema = mediaType.getSchema();
152+
if (schema != null) {
153+
schemaProcessor.processSchema(schema);
154+
}
155+
}
156+
if(mediaType.getExamples() != null) {
157+
for(Example ex: mediaType.getExamples().values()){
158+
exampleProcessor.processExample(ex);
159+
}
160+
}
161+
}
162+
}
146163
}
147164

148165
return processedPathLevelParameters;
149166
}
150-
}
167+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ 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;
48+
headerParameter.getContent();
49+
result = null;
4850
queryParameter.getSchema();
4951
result = null;
52+
queryParameter.getContent();
53+
result = null;
5054
cookieParameter.getSchema();
5155
result = null;
56+
cookieParameter.getContent();
57+
result = null;
5258
pathParameter.getSchema();
5359
result = null;
60+
pathParameter.getContent();
61+
result = null;
5462
}
5563
};
5664
final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI)
@@ -90,6 +98,8 @@ public void testProcessParameters_RefToHeader(
9098
{
9199
resolvedHeaderParam.getSchema();
92100
result = null;
101+
resolvedHeaderParam.getContent();
102+
result = null;
93103
}
94104
};
95105

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

Lines changed: 66 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;
@@ -1869,7 +1870,71 @@ public void shouldParseApiWithMultipleParameterReferences() {
18691870

18701871
}
18711872

1873+
@Test
1874+
public void shouldParseApiWithParametersUsingContentvsSchema() {
1875+
// Tests that the content method of specifying the format of a parameter
1876+
// gets resolved.
1877+
// Test checks if an API's single parameter of array type gets fully resolved to
1878+
// referenced definitions.
1879+
String location = "src/test/resources/issue-1078/api.yaml";
1880+
ParseOptions options = new ParseOptions();
1881+
options.setResolve(true);
1882+
// This test uses an Array in the parameters, test if it get's fully resolved.
1883+
options.setResolveFully(true);
1884+
OpenAPIV3Parser tested = new OpenAPIV3Parser();
1885+
1886+
// Parse yaml
1887+
SwaggerParseResult result = tested.readLocation(location, emptyList(), options);
1888+
1889+
OpenAPI api = result.getOpenAPI();
1890+
Paths paths = api.getPaths();
1891+
1892+
// First ensure all schemas were resolved, this is important when this library
1893+
// is used to generate code
1894+
Components components = api.getComponents();
1895+
assertNotNull(components);
1896+
assertThat(components.getSchemas().size(), equalTo(4));
1897+
assertNotNull(components.getSchemas().get("LocationType"));
1898+
assertNotNull(components.getSchemas().get("Lat"));
1899+
assertNotNull(components.getSchemas().get("Long"));
1900+
assertNotNull(components.getSchemas().get("SearchResult"));
1901+
1902+
PathItem apiEndpoint = paths.get("/api-endpoint-1");
1903+
List<Parameter> parameters = apiEndpoint.getGet().getParameters();
1904+
1905+
// Ensure there's only one parameter in this test
1906+
assertThat(parameters.size(), equalTo(1));
1907+
1908+
// We are testing content for a parameter so make sure its there.
1909+
Content content = parameters.get(0).getContent();
1910+
assertNotNull(content);
1911+
// spec says only one content is permitted in 3.x
1912+
assertThat( content.size(), equalTo(1));
1913+
1914+
// Ensure there's a media type
1915+
MediaType mediaType = content.entrySet().iterator().next().getValue();
1916+
assertNotNull(mediaType);
1917+
1918+
// This test has a single parameter of type array
1919+
Schema parameterSchema = mediaType.getSchema();
1920+
Assert.assertTrue(parameterSchema instanceof ArraySchema);
1921+
ArraySchema arraySchema = (ArraySchema)parameterSchema;
1922+
1923+
// Test if the item schema was resolved properly
1924+
Schema itemSchema = arraySchema.getItems();
1925+
assertNotNull(itemSchema);
1926+
Assert.assertTrue(itemSchema instanceof ObjectSchema);
1927+
1928+
// Ensure the referenced item's schema has been resolved.
1929+
ObjectSchema objSchema = (ObjectSchema)itemSchema;
1930+
Map<String, Schema> objectItemSchemas = objSchema.getProperties();
1931+
assertThat( objectItemSchemas.size(), equalTo(2));
1932+
Assert.assertTrue(objectItemSchemas.get("lat") instanceof IntegerSchema);
1933+
Assert.assertTrue(objectItemSchemas.get("long") instanceof IntegerSchema);
1934+
1935+
}
1936+
18721937
private static int getDynamicPort() {
18731938
return new Random().ints(10000, 20000).findFirst().getAsInt();
18741939
}
1875-
}
1940+
}
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)