Skip to content

Commit 1b3afd7

Browse files
committed
add support for useBeanValidation in spring http interface
1 parent aaa3500 commit 1b3afd7

9 files changed

Lines changed: 66 additions & 16 deletions

File tree

.github/workflows/samples-jdk17.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ on:
77
- samples/client/petstore/java-helidon-client/v3/mp/**
88
- samples/client/petstore/java-helidon-client/v3/se/**
99
- samples/client/petstore/spring-http-interface-reactive/**
10+
- samples/client/petstore/spring-http-interface-reactive-bean-validation/**
11+
- samples/client/petstore/spring-http-interface-bean-validation/**
1012
- samples/client/petstore/spring-http-interface/**
1113
- samples/client/petstore/spring-http-interface-reactive-noResponseEntity/**
1214
- samples/client/petstore/spring-http-interface-noResponseEntity/**
@@ -24,6 +26,8 @@ on:
2426
- samples/client/petstore/java-helidon-client/v3/mp/**
2527
- samples/client/petstore/java-helidon-client/v3/se/**
2628
- samples/client/petstore/spring-http-interface-reactive/**
29+
- samples/client/petstore/spring-http-interface-reactive-bean-validation/**
30+
- samples/client/petstore/spring-http-interface-bean-validation/**
2731
- samples/client/petstore/spring-http-interface/**
2832
- samples/client/petstore/spring-http-interface-reactive-noResponseEntity/**
2933
- samples/client/petstore/spring-http-interface-noResponseEntity/**
@@ -47,6 +51,8 @@ jobs:
4751
- samples/client/petstore/java-helidon-client/v3/mp/
4852
- samples/client/petstore/java-helidon-client/v3/se
4953
- samples/client/petstore/spring-http-interface-reactive
54+
- samples/client/petstore/spring-http-interface-reactive-bean-validation
55+
- samples/client/petstore/spring-http-interface-bean-validation
5056
- samples/client/petstore/spring-http-interface
5157
- samples/client/petstore/spring-http-interface-reactive-noResponseEntity
5258
- samples/client/petstore/spring-http-interface-noResponseEntity
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
generatorName: spring
2+
library: spring-http-interface
3+
outputDir: samples/client/petstore/spring-http-interface-validation
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
additionalProperties:
7+
artifactId: spring-http-interface
8+
snapshotVersion: "true"
9+
hideGenerationTimestamp: "true"
10+
modelNameSuffix: 'Dto'
11+
generatedConstructorWithRequiredArgs: "false"
12+
# validation should be respected
13+
useBeanValidation: "true"
14+
# documentation provider should be ignored
15+
documentationProvider: "springdoc"
16+
# annotation provider should be ignored
17+
annotationLibrary: "swagger2"

bin/configs/spring-http-interface-noResponseEntity.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ additionalProperties:
1313
documentationProvider: "springdoc"
1414
# annotation provider should be ignored
1515
annotationLibrary: "swagger2"
16-
# validation should be ignored
17-
useBeanValidation: "true"
18-
performBeanValidation: "true"
16+
# useBeanValidation should default to "false" when not specified
17+
# performBeanValidation should default to "false" when not specified
1918
useResponseEntity: "false"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
generatorName: spring
2+
library: spring-http-interface
3+
outputDir: samples/client/petstore/spring-http-interface-reactive-bean-validation
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
additionalProperties:
7+
artifactId: spring-http-interface-reactive
8+
snapshotVersion: "true"
9+
hideGenerationTimestamp: "true"
10+
reactive: "true"
11+
# validation should be respected
12+
useBeanValidation: "true"
13+
# documentation provider should be ignored
14+
documentationProvider: "springfox"
15+
# annotation provider should be ignored
16+
annotationLibrary: "swagger1"
17+

bin/configs/spring-http-interface-reactive-noResponseEntity.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ additionalProperties:
1212
documentationProvider: "springfox"
1313
# annotation provider should be ignored
1414
annotationLibrary: "swagger1"
15-
# validation should be ignored
16-
useBeanValidation: "true"
17-
performBeanValidation: "true"
15+
# useBeanValidation should default to "false" when not specified
16+
# performBeanValidation should default to "false" when not specified
1817
useResponseEntity: "false"
1918

bin/configs/spring-http-interface-reactive.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ additionalProperties:
1212
documentationProvider: "springfox"
1313
# annotation provider should be ignored
1414
annotationLibrary: "swagger1"
15-
# validation should be ignored
16-
useBeanValidation: "true"
17-
performBeanValidation: "true"
18-
15+
# useBeanValidation should default to "false" when not specified
16+
# performBeanValidation should default to "false" when not specified

bin/configs/spring-http-interface.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ additionalProperties:
1313
documentationProvider: "springdoc"
1414
# annotation provider should be ignored
1515
annotationLibrary: "swagger2"
16-
# validation should be ignored
17-
useBeanValidation: "true"
18-
performBeanValidation: "true"
16+
# useBeanValidation should default to "false" when not specified
17+
# performBeanValidation should default to "false" when not specified

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,18 @@ public void processOpts() {
384384
documentationProvider = DocumentationProvider.NONE;
385385
annotationLibrary = AnnotationLibrary.NONE;
386386
useJakartaEe = true;
387-
useBeanValidation = false;
388-
performBeanValidation = false;
387+
if(additionalProperties.containsKey(USE_BEANVALIDATION)) {
388+
useBeanValidation = Boolean.parseBoolean(additionalProperties.get(USE_BEANVALIDATION).toString());
389+
} else {
390+
//default to false if not specified
391+
useBeanValidation = false;
392+
}
393+
if(additionalProperties.containsKey(PERFORM_BEANVALIDATION)) {
394+
performBeanValidation = Boolean.parseBoolean(additionalProperties.get(PERFORM_BEANVALIDATION).toString());
395+
} else {
396+
//default to false if not specified
397+
performBeanValidation = false;
398+
}
389399

390400
additionalProperties.put(USE_JAKARTA_EE, useJakartaEe);
391401
additionalProperties.put(USE_BEANVALIDATION, useBeanValidation);

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/api.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import org.springframework.web.bind.annotation.*;
1717
import org.springframework.web.service.annotation.*;
1818
import org.springframework.web.multipart.MultipartFile;
1919
{{#reactive}}
20+
{{#useBeanValidation}}
21+
import {{javaxPackage}}.validation.Valid;
22+
import {{javaxPackage}}.validation.constraints.*;
23+
{{^useSpringBuiltInValidation}}import org.springframework.validation.annotation.Validated;{{/useSpringBuiltInValidation}}
24+
{{/useBeanValidation}}
2025

2126
import org.springframework.http.codec.multipart.Part;
2227
import reactor.core.publisher.Flux;
@@ -30,7 +35,7 @@ import {{javaxPackage}}.annotation.Generated;
3035

3136

3237
{{>generatedAnnotation}}
33-
38+
{{#useBeanValidation}}{{^useSpringBuiltInValidation}}@Validated{{/useSpringBuiltInValidation}}{{/useBeanValidation}}
3439
{{#operations}}
3540
public interface {{classname}} {
3641
{{#operation}}

0 commit comments

Comments
 (0)