Skip to content

Commit 79ced9d

Browse files
committed
[spring] Resolve regression on RequestParam for non-objects
1 parent dd152ee commit 79ced9d

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,8 +2303,8 @@ public CodegenProperty fromProperty(String name, Schema p) {
23032303
// property.baseType = getSimpleRef(p.get$ref());
23042304
//}
23052305
// --END of revision
2306-
property.isModel = ModelUtils.isModel(p);
23072306
setNonArrayMapProperty(property, type);
2307+
property.isModel = ModelUtils.isObjectSchema(p) && ModelUtils.isModel(p);
23082308
}
23092309

23102310
LOGGER.debug("debugging from property return: " + property);

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,75 @@ public void doNotGenerateRequestParamForObjectQueryParam() throws IOException {
167167
checkFileNotContains(generator, outputPath + "/src/main/java/org/openapitools/api/PonyApi.java", "@RequestParam");
168168
}
169169

170+
@Test
171+
public void shouldGenerateRequestParamForRefParams_3248_Regression() throws IOException {
172+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
173+
output.deleteOnExit();
174+
String outputPath = output.getAbsolutePath().replace('\\', '/');
175+
176+
OpenAPI openAPI = new OpenAPIParser()
177+
.readLocation("src/test/resources/3_0/3248-regression.yaml", null, new ParseOptions()).getOpenAPI();
178+
179+
SpringCodegen codegen = new SpringCodegen();
180+
codegen.setOutputDir(output.getAbsolutePath());
181+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
182+
183+
ClientOptInput input = new ClientOptInput();
184+
input.setOpenAPI(openAPI);
185+
input.setConfig(codegen);
186+
187+
MockDefaultGenerator generator = new MockDefaultGenerator();
188+
generator.opts(input).generate();
189+
190+
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java",
191+
"@RequestParam(value = \"format\"",
192+
"@RequestParam(value = \"query\"");
193+
}
194+
195+
@Test
196+
public void shouldGenerateRequestParamForRefParams_3248_RegressionDates() throws IOException {
197+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
198+
output.deleteOnExit();
199+
String outputPath = output.getAbsolutePath().replace('\\', '/');
200+
201+
OpenAPI openAPI = new OpenAPIParser()
202+
.readLocation("src/test/resources/3_0/3248-regression-dates.yaml", null, new ParseOptions()).getOpenAPI();
203+
204+
SpringCodegen codegen = new SpringCodegen();
205+
codegen.setOutputDir(output.getAbsolutePath());
206+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
207+
208+
ClientOptInput input = new ClientOptInput();
209+
input.setOpenAPI(openAPI);
210+
input.setConfig(codegen);
211+
212+
MockDefaultGenerator generator = new MockDefaultGenerator();
213+
generator.opts(input).generate();
214+
215+
checkFileContains(generator, outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java",
216+
"@RequestParam(value = \"start\"");
217+
}
218+
170219
private void checkFileNotContains(MockDefaultGenerator generator, String path, String... lines) {
171220
String file = generator.getFiles().get(path);
172221
assertNotNull(file);
173222
for (String line : lines)
174223
assertFalse(file.contains(line));
175224
}
176225

226+
private void checkFileContains(MockDefaultGenerator generator, String path, String... lines) {
227+
String file = generator.getFiles().get(path);
228+
assertNotNull(file);
229+
int expectedCount = lines.length;
230+
int actualCount = 0;
231+
for (String line : lines) {
232+
if (file.contains(line)) {
233+
actualCount++;
234+
}
235+
}
236+
assertEquals(actualCount, expectedCount, "File is missing " + (expectedCount - actualCount) + " expected lines.");
237+
}
238+
177239
@Test
178240
public void clientOptsUnicity() {
179241
SpringCodegen codegen = new SpringCodegen();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
openapi: 3.0.2
2+
info:
3+
title: info
4+
description: info
5+
version: 0.1.0
6+
7+
paths:
8+
/example/api:
9+
get:
10+
summary: summary
11+
description: description
12+
parameters:
13+
- name: start
14+
in: query
15+
schema:
16+
type: string
17+
format: date-time
18+
required: true
19+
description: The start time.
20+
responses:
21+
200:
22+
description: response
23+
content:
24+
application/json:
25+
schema:
26+
type: string
27+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
openapi: 3.0.2
2+
info:
3+
title: info
4+
description: info
5+
version: 0.1.0
6+
7+
paths:
8+
/example/api:
9+
get:
10+
summary: summary
11+
description: description
12+
parameters:
13+
- $ref: '#/components/parameters/requiredQueryParam'
14+
- $ref: '#/components/parameters/formatParam'
15+
responses:
16+
200:
17+
description: response
18+
content:
19+
application/json:
20+
schema:
21+
type: string
22+
23+
components:
24+
parameters:
25+
requiredQueryParam:
26+
description: set query
27+
in: query
28+
name: query
29+
required: true
30+
schema:
31+
type: string
32+
formatParam:
33+
description: set format
34+
in: query
35+
name: format
36+
required: false
37+
schema:
38+
$ref: '#/components/schemas/format'
39+
40+
schemas:
41+
format:
42+
default: json
43+
description: response format
44+
enum:
45+
- json
46+
- csv
47+
type: string

0 commit comments

Comments
 (0)