[JAVA-SPRING] - 22859 - spring http interface library should support 'useBeanValidation'#22891
Conversation
…-must-support-useBeanValidation2 # Conflicts: # modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java # samples/client/petstore/spring-http-interface-noResponseEntity/src/main/java/org/openapitools/api/AnotherFakeApi.java # samples/client/petstore/spring-http-interface-noResponseEntity/src/main/java/org/openapitools/api/FakeApi.java # samples/client/petstore/spring-http-interface-noResponseEntity/src/main/java/org/openapitools/api/FakeClassnameTags123Api.java # samples/client/petstore/spring-http-interface-noResponseEntity/src/main/java/org/openapitools/api/PetApi.java # samples/client/petstore/spring-http-interface-noResponseEntity/src/main/java/org/openapitools/api/StoreApi.java # samples/client/petstore/spring-http-interface-noResponseEntity/src/main/java/org/openapitools/api/UserApi.java # samples/client/petstore/spring-http-interface-reactive-noResponseEntity/src/main/java/org/openapitools/api/AnotherFakeApi.java # samples/client/petstore/spring-http-interface-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApi.java # samples/client/petstore/spring-http-interface-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeClassnameTags123Api.java # samples/client/petstore/spring-http-interface-reactive-noResponseEntity/src/main/java/org/openapitools/api/PetApi.java # samples/client/petstore/spring-http-interface-reactive-noResponseEntity/src/main/java/org/openapitools/api/StoreApi.java # samples/client/petstore/spring-http-interface-reactive-noResponseEntity/src/main/java/org/openapitools/api/UserApi.java # samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/AnotherFakeApi.java # samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/FakeApi.java # samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/FakeClassnameTags123Api.java # samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/PetApi.java # samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/StoreApi.java # samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/UserApi.java # samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/AnotherFakeApi.java # samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/FakeApi.java # samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/FakeClassnameTags123Api.java # samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/PetApi.java # samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/StoreApi.java # samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/UserApi.java
There was a problem hiding this comment.
5 issues found across 217 files
Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/pom-sb4.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/pom-sb4.mustache:101">
P2: `spring-boot-starter-validation` is only added for `useBeanValidation`, but `performBeanValidation` can be enabled independently for spring-http-interface. When `performBeanValidation=true` and `useBeanValidation=false`, generated code can require Bean Validation at runtime without pulling the validation implementation, leading to missing validator beans.</violation>
</file>
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/NullableMapPropertyDto.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/NullableMapPropertyDto.java:43">
P1: `putLanguageValuesItem` dereferences a null map when `languageValues` is explicitly set to null via `JsonNullable.of(null)`.</violation>
</file>
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/api/FakeApi.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/api/FakeApi.java:219">
P2: @Pattern uses Java regex syntax; `/[a-z]/i` is treated literally (slashes and `i` included), so case-insensitive matching won’t work and normal alphabetic input will fail validation.</violation>
</file>
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/PetDto.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/PetDto.java:41">
P3: @Valid is applied to both the field and getter for tags, which causes cascaded validation to run twice per Bean Validation spec and can produce duplicate violations/overhead. Use a single access strategy (field or property) for @Valid.</violation>
</file>
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/CatDto.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/CatDto.java:36">
P2: `CatDto` is missing explicit `@JsonTypeName("Cat")`, risking discriminator `className` mismatch under Jackson `Id.NAME`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
…-must-support-useBeanValidation2 # Conflicts: # bin/configs/spring-http-interface-reactive.yaml # bin/configs/spring-http-interface.yaml # modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java # samples/client/petstore/spring-http-interface-springboot-4/src/main/java/org/openapitools/model/ArrayTestDto.java # samples/client/petstore/spring-http-interface-springboot-4/src/main/java/org/openapitools/model/FileSchemaTestClassDto.java # samples/client/petstore/spring-http-interface-springboot-4/src/main/java/org/openapitools/model/PetDto.java
…-must-support-useBeanValidation2
…n performBeanValidation flag
There was a problem hiding this comment.
4 issues found across 645 files
Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/FormatTestDto.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/FormatTestDto.java:205">
P1: Bean Validation regex is emitted in JavaScript `/.../i` form instead of Java regex/flags, causing incorrect `@Pattern` validation behavior.</violation>
</file>
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/ContainerDefaultValueDto.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/ContainerDefaultValueDto.java:35">
P2: Required properties are pre-initialized to non-null values, so `@NotNull` cannot catch missing JSON fields and required-field validation can be bypassed.</violation>
<violation number="2" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/ContainerDefaultValueDto.java:52">
P2: `add...Item` methods can throw `NullPointerException` when the field is `JsonNullable.of(null)` (present but null).</violation>
</file>
<file name="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/NullableMapPropertyDto.java">
<violation number="1" location="samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/NullableMapPropertyDto.java:42">
P2: `putLanguageValuesItem` can throw `NullPointerException` when `languageValues` is `JsonNullable.of(null)` (present wrapper with null value).</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| * Get string | ||
| * @return string | ||
| */ | ||
| @Pattern(regexp = "/[a-z]/i") |
There was a problem hiding this comment.
P1: Bean Validation regex is emitted in JavaScript /.../i form instead of Java regex/flags, causing incorrect @Pattern validation behavior.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/FormatTestDto.java, line 205:
<comment>Bean Validation regex is emitted in JavaScript `/.../i` form instead of Java regex/flags, causing incorrect `@Pattern` validation behavior.</comment>
<file context>
@@ -0,0 +1,416 @@
+ * Get string
+ * @return string
+ */
+ @Pattern(regexp = "/[a-z]/i")
+ @JsonProperty("string")
+ public @Nullable String getString() {
</file context>
|
|
||
| private JsonNullable<List<String>> nullableRequiredArray = JsonNullable.<List<String>>undefined(); | ||
|
|
||
| private List<String> requiredArray = new ArrayList<>(); |
There was a problem hiding this comment.
P2: Required properties are pre-initialized to non-null values, so @NotNull cannot catch missing JSON fields and required-field validation can be bypassed.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/spring-http-interface-bean-validation/src/main/java/org/openapitools/model/ContainerDefaultValueDto.java, line 35:
<comment>Required properties are pre-initialized to non-null values, so `@NotNull` cannot catch missing JSON fields and required-field validation can be bypassed.</comment>
<file context>
@@ -0,0 +1,204 @@
+
+ private JsonNullable<List<String>> nullableRequiredArray = JsonNullable.<List<String>>undefined();
+
+ private List<String> requiredArray = new ArrayList<>();
+
+ private JsonNullable<List<String>> nullableArrayWithDefault = JsonNullable.<List<String>>undefined();
</file context>
Spring declares that Http Interface annotations can also be used server-side for controller instantiation: https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-service-client
On the server side, an @controller class can implement the same interface to handle requests with @HttpExchange controller methods. In such case it makes sense to allow the bean validation option and thus annotate both the model and the api methods with
jakarta.validationannotations.I also added unit tests
closes #22859
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)Summary by cubic
Adds Bean Validation support to the Spring HTTP Interface library (reactive and non‑reactive) and fixes Jackson polymorphic typing with discriminator mappings. The generator now honors
useBeanValidation/performBeanValidation(default false), emitsjakarta.validationand@Validatedwhen enabled, addsspring-boot-starter-validationto SB3/SB4 POMs, updates samples/tests, and avoids NPEs in nullable containers. Fixes #22859.New Features
useBeanValidationandperformBeanValidationonly when provided; default both to false for thespring-http-interfacelibrary.spring-boot-starter-validationwhenuseBeanValidationorperformBeanValidationis true; add reactive and non-reactive bean-validation samples and include them in the JDK 17 samples workflow with unit tests.Refactors
@Validon container fields in POJOs; update samples accordingly.x-discriminator-valueto child models and emit@JsonTypeNameaccordingly to align with@JsonSubTypesand ensure correct Jackson polymorphic typing.Written for commit f77d733. Summary will update on new commits.