[BUG][JAVA][SPRING] Collection request-body validation skipped with useSpringBuiltInValidation=true after 7.25.0

未关闭
#25,734 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Inf1mum 已经在做这个了。

开始于 2026年11月18日。

  • #25741 来自 @Inf1mum —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
java, spring
领域
api, backend

调研方向

Start with modules/openapi-generator/src/main/resources/JavaSpring/bodyParams.mustache and compare the v7.25.0 and v7.26.0 versions linked in the issue. Then inspect the Spring generator tests that cover request body validation and reproduce the described collection request with useSpringBuiltInValidation=true. Done means the generated interface preserves validation and the invalid item returns HTTP 400 before the controller runs; coordinate with the already open PR #25741.

由索引模型根据 Issue 内容生成。

描述

Issue: Bug

Description

After upgrading openapi-generator-maven-plugin from 7.25.0 to 7.27.0, an existing Spring MVC endpoint accepting a list of DTOs accepts invalid collection elements instead of returning HTTP 400.

We use useSpringBuiltInValidation=true, so the generated interface does not have class-level @Validated.

The generated request parameter changes from:

@Valid @RequestBody List<@Valid Item> items

to:

@RequestBody List<@Valid Item> items

The DTO constraints are unchanged.

Expected: Invalid collection elements are rejected with HTTP 400 before the controller implementation executes.

Actual: Invalid collection elements reach the controller, and the endpoint returns HTTP 204.

With 7.25.0, the request is rejected with HandlerMethodValidationException.

Versions

  • Previously working: 7.25.0
  • Runtime failure observed: 7.27.0
  • The relevant template change is already present in 7.26.0, but that version has not been separately runtime-tested.
  • Spring Boot: 3.5.16
  • Java: 21

Template comparison:

https://github.com/OpenAPITools/openapi-generator/blob/v7.25.0/modules/openapi-generator/src/main/resources/JavaSpring/bodyParams.mustache

https://github.com/OpenAPITools/openapi-generator/blob/v7.26.0/modules/openapi-generator/src/main/resources/JavaSpring/bodyParams.mustache

Generator configuration

<generatorName>spring</generatorName>
<library>spring-boot</library>
<generateApis>true</generateApis>
<configOptions>
  <useSpringBoot3>true</useSpringBoot3>
  <useJakartaEe>true</useJakartaEe>
  <interfaceOnly>true</interfaceOnly>
  <skipDefaultInterface>true</skipDefaultInterface>
  <useResponseEntity>false</useResponseEntity>
  <useSpringBuiltInValidation>true</useSpringBuiltInValidation>
</configOptions>

Illustrative OpenAPI specification

The regression was observed in an existing application. This generic specification illustrates the affected request shape; this standalone example has not yet been executed.

openapi: 3.0.3
info:
  title: Collection validation example
  version: 1.0.0
paths:
  /items:
    post:
      operationId: updateItems
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/Item'
      responses:
        '204':
          description: Accepted
components:
  schemas:
    Item:
      type: object
      properties:
        name:
          type: string
          maxLength: 3

Implement the generated interface in a @RestController, without adding class-level @Validated, and submit:

POST /items
Content-Type: application/json

[{"name":"too long"}]

The invalid name should cause HTTP 400 rather than reaching the controller.

Related upstream change

This appears related to the removal of container-level @Valid in:

https://github.com/OpenAPITools/openapi-generator/pull/24176

The author's runtime verification uses a controller annotated with class-level @Validated:

https://github.com/OpenAPITools/openapi-generator/pull/24176#issuecomment-4859610681

However, useSpringBuiltInValidation=true intentionally suppresses that annotation and uses Spring MVC's built-in validation. Therefore, that verification does not cover this configuration.

Could the generator preserve collection request-body validation with useSpringBuiltInValidation=true, or document a supported migration for this configuration?

主要语言
Java
星标
26.9k
派生
7.8k
平均合并
2 天 23 分钟
30 天内合并 PR
177

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenAPITools/openapi-generator 的其他 Issue

查看 OpenAPITools/openapi-generator 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。