Skip to content

[JAVA-SPRING] - 22859 - spring http interface library should support 'useBeanValidation'#22891

Draft
Picazsoo wants to merge 20 commits intoOpenAPITools:masterfrom
Picazsoo:bugfix/22859-Spring-HTTP-Interface-library-must-support-useBeanValidation2
Draft

[JAVA-SPRING] - 22859 - spring http interface library should support 'useBeanValidation'#22891
Picazsoo wants to merge 20 commits intoOpenAPITools:masterfrom
Picazsoo:bugfix/22859-Spring-HTTP-Interface-library-must-support-useBeanValidation2

Conversation

@Picazsoo
Copy link
Copy Markdown
Contributor

@Picazsoo Picazsoo commented Feb 3, 2026

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.validation annotations.

I also added unit tests

closes #22859

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request. - tagging @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

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), emits jakarta.validation and @Validated when enabled, adds spring-boot-starter-validation to SB3/SB4 POMs, updates samples/tests, and avoids NPEs in nullable containers. Fixes #22859.

  • New Features

    • Honor useBeanValidation and performBeanValidation only when provided; default both to false for the spring-http-interface library.
    • Add spring-boot-starter-validation when useBeanValidation or performBeanValidation is true; add reactive and non-reactive bean-validation samples and include them in the JDK 17 samples workflow with unit tests.
  • Refactors

    • Stop emitting redundant @Valid on container fields in POJOs; update samples accordingly.
    • Propagate discriminator mapping keys as x-discriminator-value to child models and emit @JsonTypeName accordingly to align with @JsonSubTypes and ensure correct Jackson polymorphic typing.

Written for commit f77d733. Summary will update on new commits.

@Picazsoo Picazsoo changed the title [java-spring] - 22859 - spring http interface library must support use bean validation [JAVA-SPRING] - 22859 - spring http interface library should support 'useBeanValidation' Feb 17, 2026
Picazsoo added 4 commits March 5, 2026 21:19
…-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
@Picazsoo Picazsoo marked this pull request as ready for review March 5, 2026 21:56
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic


private JsonNullable<List<String>> nullableRequiredArray = JsonNullable.<List<String>>undefined();

private List<String> requiredArray = new ArrayList<>();
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

@Picazsoo Picazsoo marked this pull request as draft April 16, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JavaSpring] Spring HTTP Interface library must support useBeanValidation

1 participant