Skip to content

Commit 7ad16bc

Browse files
committed
Issue swagger-api#2116. Add unit test to check that params ref starting with #/paths are properly processed
1 parent 51ac8c2 commit 7ad16bc

File tree

1 file changed

+161
-145
lines changed

1 file changed

+161
-145
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.v3.parser.processors;
22

3+
import static org.testng.Assert.assertEquals;
34

45
import io.swagger.v3.oas.models.OpenAPI;
56
import io.swagger.v3.oas.models.media.Content;
@@ -13,154 +14,169 @@
1314
import io.swagger.v3.oas.models.parameters.RequestBody;
1415
import io.swagger.v3.parser.ResolverCache;
1516
import io.swagger.v3.parser.models.RefFormat;
16-
import mockit.*;
17-
import org.testng.annotations.Test;
18-
19-
2017
import java.util.Arrays;
2118
import java.util.List;
22-
23-
import static org.testng.Assert.assertEquals;
24-
19+
import mockit.Expectations;
20+
import mockit.FullVerifications;
21+
import mockit.Injectable;
22+
import mockit.Mocked;
23+
import org.testng.annotations.Test;
2524

2625
public class ParameterProcessorTest {
2726

28-
29-
@Injectable
30-
ResolverCache cache;
31-
32-
@Injectable
33-
OpenAPI openAPI;
34-
35-
@Mocked
36-
SchemaProcessor modelProcessor;
37-
38-
@Injectable
39-
boolean openapi31;
40-
41-
@Injectable
42-
HeaderParameter headerParameter;
43-
44-
@Injectable
45-
QueryParameter queryParameter;
46-
47-
@Injectable
48-
CookieParameter cookieParameter;
49-
50-
@Injectable
51-
PathParameter pathParameter;
52-
53-
@Injectable
54-
HeaderParameter resolvedHeaderParam;
55-
56-
@Injectable
57-
Schema bodyParamSchema;
58-
59-
@Test
60-
public void testProcessParameters_TypesThatAreNotRefOrBody() throws Exception {
61-
expectedModelProcessorCreation();
62-
new Expectations() {
63-
{
64-
headerParameter.getSchema();
65-
result = null;
66-
headerParameter.getContent();
67-
result = null;
68-
queryParameter.getSchema();
69-
result = null;
70-
queryParameter.getContent();
71-
result = null;
72-
cookieParameter.getSchema();
73-
result = null;
74-
cookieParameter.getContent();
75-
result = null;
76-
pathParameter.getSchema();
77-
result = null;
78-
pathParameter.getContent();
79-
result = null;
80-
}
81-
};
82-
final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI, openapi31)
83-
.processParameters(Arrays.asList(headerParameter,
84-
queryParameter,
85-
cookieParameter,
86-
pathParameter));
87-
88-
new FullVerifications() {{
89-
headerParameter.get$ref();
90-
times = 1;
91-
queryParameter.get$ref();
92-
times = 1;
93-
cookieParameter.get$ref();
94-
times = 1;
95-
pathParameter.get$ref();
96-
times = 1;
97-
}};
98-
99-
assertEquals(processedParameters.size(), 4);
100-
assertEquals(processedParameters.get(0), headerParameter);
101-
assertEquals(processedParameters.get(1), queryParameter);
102-
assertEquals(processedParameters.get(2), cookieParameter);
103-
assertEquals(processedParameters.get(3), pathParameter);
104-
}
105-
106-
@Test
107-
public void testProcessParameters_RefToHeader() throws Exception {
108-
expectedModelProcessorCreation();
109-
110-
final String ref = "#/components/parameters/foo";
111-
Parameter refParameter = new Parameter().$ref(ref);
112-
113-
expectLoadingRefFromCache(ref, RefFormat.INTERNAL, resolvedHeaderParam);
114-
new Expectations() {
115-
{
116-
resolvedHeaderParam.getSchema();
117-
result = null;
118-
resolvedHeaderParam.getContent();
119-
result = null;
120-
}
121-
};
122-
123-
final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI, openapi31).processParameters(Arrays.asList(refParameter));
124-
125-
new FullVerifications(){{}};
126-
127-
assertEquals(processedParameters.size(), 1);
128-
assertEquals(processedParameters.get(0), resolvedHeaderParam);
129-
}
130-
131-
private void expectLoadingRefFromCache(final String ref, final RefFormat refFormat,
132-
final Parameter resolvedParam) {
133-
new Expectations() {{
134-
cache.loadRef(ref, refFormat, Parameter.class);
135-
times = 1;
136-
result = resolvedParam;
137-
}};
138-
}
139-
140-
@Test
141-
public void testProcessParameters_BodyParameter() throws Exception {
142-
final SchemaProcessor[] schemaProcessor1 = {new SchemaProcessor(cache, openAPI, openapi31)};
143-
new Expectations() {{
144-
schemaProcessor1[0] = new SchemaProcessor(cache, openAPI, openapi31);
145-
times = 1;
146-
147-
}};
148-
149-
RequestBody bodyParameter = new RequestBody().content(new Content().addMediaType("*/*",new MediaType().schema(bodyParamSchema)));
150-
151-
new Expectations(){{
152-
schemaProcessor1[0].processSchema(bodyParamSchema); times=1;
153-
}};
154-
155-
new RequestBodyProcessor(cache, openAPI, openapi31).processRequestBody(bodyParameter);
156-
157-
new FullVerifications(){{}};
158-
}
159-
160-
private void expectedModelProcessorCreation() {
161-
new Expectations() {{
162-
new SchemaProcessor(cache, openAPI, openapi31);
163-
times = 1;
164-
}};
165-
}
27+
@Injectable
28+
ResolverCache cache;
29+
30+
@Injectable
31+
OpenAPI openAPI;
32+
33+
@Mocked
34+
SchemaProcessor modelProcessor;
35+
36+
@Injectable
37+
boolean openapi31;
38+
39+
@Injectable
40+
HeaderParameter headerParameter;
41+
42+
@Injectable
43+
QueryParameter queryParameter;
44+
45+
@Injectable
46+
CookieParameter cookieParameter;
47+
48+
@Injectable
49+
PathParameter pathParameter;
50+
51+
@Injectable
52+
HeaderParameter resolvedHeaderParam;
53+
54+
@Injectable
55+
Schema bodyParamSchema;
56+
57+
@Test
58+
public void testProcessParameters_TypesThatAreNotRefOrBody() throws Exception {
59+
expectedModelProcessorCreation();
60+
new Expectations() {
61+
{
62+
headerParameter.getSchema();
63+
result = null;
64+
headerParameter.getContent();
65+
result = null;
66+
queryParameter.getSchema();
67+
result = null;
68+
queryParameter.getContent();
69+
result = null;
70+
cookieParameter.getSchema();
71+
result = null;
72+
cookieParameter.getContent();
73+
result = null;
74+
pathParameter.getSchema();
75+
result = null;
76+
pathParameter.getContent();
77+
result = null;
78+
}
79+
};
80+
final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI, openapi31)
81+
.processParameters(Arrays.asList(
82+
headerParameter,
83+
queryParameter,
84+
cookieParameter,
85+
pathParameter
86+
));
87+
88+
new FullVerifications() {{
89+
headerParameter.get$ref();
90+
times = 1;
91+
queryParameter.get$ref();
92+
times = 1;
93+
cookieParameter.get$ref();
94+
times = 1;
95+
pathParameter.get$ref();
96+
times = 1;
97+
}};
98+
99+
assertEquals(processedParameters.size(), 4);
100+
assertEquals(processedParameters.get(0), headerParameter);
101+
assertEquals(processedParameters.get(1), queryParameter);
102+
assertEquals(processedParameters.get(2), cookieParameter);
103+
assertEquals(processedParameters.get(3), pathParameter);
104+
}
105+
106+
@Test
107+
public void testProcessParameters_RefToHeader() throws Exception {
108+
expectedModelProcessorCreation();
109+
110+
final String ref = "#/components/parameters/foo";
111+
Parameter refParameter = new Parameter().$ref(ref);
112+
113+
expectLoadingRefFromCache(ref, RefFormat.INTERNAL, resolvedHeaderParam);
114+
new Expectations() {
115+
{
116+
resolvedHeaderParam.getSchema();
117+
result = null;
118+
resolvedHeaderParam.getContent();
119+
result = null;
120+
}
121+
};
122+
123+
final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI, openapi31).processParameters(Arrays.asList(refParameter));
124+
125+
new FullVerifications() {{
126+
}};
127+
128+
assertEquals(processedParameters.size(), 1);
129+
assertEquals(processedParameters.get(0), resolvedHeaderParam);
130+
}
131+
132+
private void expectLoadingRefFromCache(
133+
final String ref, final RefFormat refFormat,
134+
final Parameter resolvedParam
135+
) {
136+
new Expectations() {{
137+
cache.loadRef(ref, refFormat, Parameter.class);
138+
times = 1;
139+
result = resolvedParam;
140+
}};
141+
}
142+
143+
@Test
144+
public void testProcessParameters_BodyParameter() throws Exception {
145+
final SchemaProcessor[] schemaProcessor1 = {new SchemaProcessor(cache, openAPI, openapi31)};
146+
new Expectations() {{
147+
schemaProcessor1[0] = new SchemaProcessor(cache, openAPI, openapi31);
148+
times = 1;
149+
150+
}};
151+
152+
RequestBody bodyParameter = new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(bodyParamSchema)));
153+
154+
new Expectations() {{
155+
schemaProcessor1[0].processSchema(bodyParamSchema);
156+
times = 1;
157+
}};
158+
159+
new RequestBodyProcessor(cache, openAPI, openapi31).processRequestBody(bodyParameter);
160+
161+
new FullVerifications() {{
162+
}};
163+
}
164+
165+
@Test
166+
public void testProcessParameters_PreservePathRef() throws Exception {
167+
ParameterProcessor processor = new ParameterProcessor(cache, openAPI, openapi31);
168+
List<Parameter> parameters =
169+
Arrays.asList(new PathParameter().$ref("#/paths/~1test/get/parameters/0"), new PathParameter().$ref("#/no-paths/~1test/get/parameters/0"));
170+
List<Parameter> processedParameters = processor.processParameters(parameters);
171+
172+
assertEquals(processedParameters.size(), 1);
173+
assertEquals(processedParameters.get(0).get$ref(), "#/paths/~1test/get/parameters/0");
174+
}
175+
176+
private void expectedModelProcessorCreation() {
177+
new Expectations() {{
178+
new SchemaProcessor(cache, openAPI, openapi31);
179+
times = 1;
180+
}};
181+
}
166182
}

0 commit comments

Comments
 (0)