Skip to content

Commit e847cbe

Browse files
committed
fix(Spring Boot): adds test for validation of body params of forms requests
1 parent 2ce1267 commit e847cbe

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,42 @@ public void useBeanValidationGenerateAnnotationsForRequestBody_issue13932() thro
27432743
.containsWithNameAndAttributes("Min", ImmutableMap.of("value", "2"));
27442744
}
27452745

2746+
@Test
2747+
public void useBeanValidationGenerateAnnotationsForFormsRequestBody() throws IOException {
2748+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2749+
output.deleteOnExit();
2750+
2751+
OpenAPI openAPI = new OpenAPIParser()
2752+
.readLocation("src/test/resources/3_0/spring/form-requestbody-params-with-constraints.yaml", null, new ParseOptions()).getOpenAPI();
2753+
SpringCodegen codegen = new SpringCodegen();
2754+
codegen.setLibrary(SPRING_BOOT);
2755+
codegen.setOutputDir(output.getAbsolutePath());
2756+
codegen.additionalProperties().put(SpringCodegen.INTERFACE_ONLY, "true");
2757+
codegen.additionalProperties().put(SpringCodegen.USE_BEANVALIDATION, "true");
2758+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xyz.model");
2759+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xyz.controller");
2760+
2761+
ClientOptInput input = new ClientOptInput()
2762+
.openAPI(openAPI)
2763+
.config(codegen);
2764+
2765+
DefaultGenerator generator = new DefaultGenerator();
2766+
generator.setGenerateMetadata(false);
2767+
Map<String, File> files = generator.opts(input).generate().stream()
2768+
.collect(Collectors.toMap(File::getName, Function.identity()));
2769+
2770+
JavaFileAssert.assertThat(files.get("AddApi.java"))
2771+
.assertMethod("addPost")
2772+
.assertParameter("name")
2773+
.assertParameterAnnotations()
2774+
.containsWithNameAndAttributes("Pattern", ImmutableMap.of("regexp", "\"^[[:print:]]+$\""))
2775+
.toParameter()
2776+
.toMethod()
2777+
.assertParameter("quantity")
2778+
.assertParameterAnnotations()
2779+
.containsWithNameAndAttributes("Min", ImmutableMap.of("value", "1"));
2780+
}
2781+
27462782
@Test
27472783
public void shouldHandleSeparatelyInterfaceAndModelAdditionalAnnotations() throws IOException {
27482784
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test form
4+
version: 1.0.0
5+
servers:
6+
- url: https://where.am.i
7+
paths:
8+
/add:
9+
post:
10+
requestBody:
11+
required: true
12+
content:
13+
application/x-www-form-urlencoded:
14+
schema:
15+
type: object
16+
required:
17+
- id
18+
- quantity
19+
properties:
20+
name:
21+
type: string
22+
pattern: '^[[:print:]]+$'
23+
quantity:
24+
type: integer
25+
minimum: 1
26+
responses:
27+
'200':
28+
description: OK
29+
content:
30+
application/json:
31+
schema:
32+
type: boolean

0 commit comments

Comments
 (0)