[BUG][KOTLIN] jvm-retrofit2: DELETE with requestBody fails with "Non-body HTTP method cannot contain @Body"
维护者通常 1 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 80/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
调研方向
从 kotlin-client/libraries/jvm-retrofit2/api.mustache 开始,issue 指出 Retrofit 注解在此生成,然后检查该库附近的模板测试。使用链接中的最小规格复现,并确认带有请求体的 DELETE 操作会生成一个可调用的客户端方法,该方法会发送 JSON 请求体。issue 还指出 Java/libraries/retrofit2/api.mustache 是需要检查的相关模板。
由索引模型根据 Issue 内容生成。
描述
[!NOTE]
🤖 Claude Opus 5.5 opening this issue on behalf of Will
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
Description
With generatorName = kotlin and library = jvm-retrofit2, a DELETE operation that has a requestBody generates @DELETE("path") plus an @Body parameter. It compiles, but Retrofit rejects the method on the first call:
java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
for method DefaultApi.deleteItems
at retrofit2.Utils.methodError(Utils.java:56)
at retrofit2.Utils.methodError(Utils.java:44)
at retrofit2.RequestFactory$Builder.build(RequestFactory.java:220)
at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:235)
at retrofit2.Retrofit$1.invoke(Retrofit.java:177)
OpenAPI 3 allows a body on DELETE, so the generated client can't call these operations at all. We hit it on every call to a DELETE endpoint with a required JSON body in our app.
Minimal repro: https://github.com/ceolinwill/openapi-kotlin-retrofit-delete-body-repro
openapi-generator version
7.26.0 (latest release), Gradle plugin. Not a regression as far as I can tell: the template line is the same on master.
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Repro
version: 1.0.0
paths:
/items:
delete:
operationId: deleteItems
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/DeleteItemsRequest"
responses:
"204":
description: Deleted
components:
schemas:
DeleteItemsRequest:
type: object
required: [ids]
properties:
ids:
type: array
items:
type: string
openApiValidate reports "Spec is valid."
Generation Details
openApiGenerate {
generatorName = "kotlin"
library = "jvm-retrofit2"
inputSpec = layout.projectDirectory.file("openapi.yaml")
outputDir = layout.buildDirectory.dir("generated")
packageName = "repro.client"
configOptions = mapOf(
"serializationLibrary" to "kotlinx_serialization",
"useCoroutines" to "true",
)
}
Generated:
@DELETE("items")
suspend fun deleteItems(@Body deleteItemsRequest: DeleteItemsRequest): Response<Unit>
Retrofit 3.0.0, OkHttp 5.5.1, Kotlin 2.4.21, JDK 17.
Steps to reproduce
git clone https://github.com/ceolinwill/openapi-kotlin-retrofit-delete-body-repro && cd openapi-kotlin-retrofit-delete-body-repro./gradlew run -q: callsdeleteItemsagainst MockWebServer and throws the exception above../gradlew run -q -PwithFix: same code generated with the template change below. PrintsDELETE /items {"ids":["a","b"]}.
Expected: the request is sent as DELETE /items with the JSON body.
Actual: IllegalArgumentException before any request is made.
Related issues/PRs
I found none for Kotlin or Retrofit. #7557 / #7558 were the same kind of problem in typescript-angular.
Suggest a fix
kotlin-client/libraries/jvm-retrofit2/api.mustache always emits @{{httpMethod}}("{{{path}}}") (line 158). Retrofit's documented way to send a body with a method that has no body by default is @HTTP(method = ..., path = ..., hasBody = true). Changing that line to this fixes it in the repro, and leaves operations without a body unchanged:
{{#bodyParam}}@HTTP(method = "{{httpMethod}}", path = "{{{path}}}", hasBody = true){{/bodyParam}}{{^bodyParam}}@{{httpMethod}}("{{{path}}}"){{/bodyParam}}
retrofit2.http.* is already imported. Java/libraries/retrofit2/api.mustache has the same @{{httpMethod}}("{{{path}}}") line, so the Java retrofit2 client likely has the same problem; I haven't run it.
- 主要语言
- Java
- 星标
- 26.8k
- 派生
- 7.7k
- 平均合并
- 1 天 14 小时
- 30 天内合并 PR
- 185
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
OpenAPITools/openapi-generator 的其他 Issue
-
Issue: Bug
难度 2/5 1-3 小时 新手友好度 82/100
OpenAPITools/openapi-generator#25475 ·
维护者通常 1 天内回复
-
Enhancement: Feature
难度 2/5 1-3 小时 新手友好度 82/100
OpenAPITools/openapi-generator#25425 ·
维护者通常 1 天内回复
-
[REQ][JAVA][resteasy] Support for Jackson 3 / useJackson3可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭Enhancement: Feature
难度 2/5 1-3 小时 新手友好度 78/100
OpenAPITools/openapi-generator#25364 ·
维护者通常 1 天内回复
-
[BUG][RUST] chrono date-time path/query/header params are not serialized as RFC 3339可能已有人在做 @teovillanueva 于 12 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
OpenAPITools/openapi-generator#25297 ·
维护者通常 1 天内回复
-
[BUG][RUST] Reqwest - encoding of spaces in path parameters可能已有人在做 @GlenWright 于 15 天前认领。 未关闭Issue: Bug
难度 2/5 1-3 小时 新手友好度 68/100
OpenAPITools/openapi-generator#25257 · 3 条评论 ·
维护者通常 1 天内回复
查看 OpenAPITools/openapi-generator 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
oracle/graalvm-reachability-metadata#11472 ·
维护者通常 1 天内回复
-
OkHttp versions 5.4.0+ no longer include baseline-prof.txt可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug
难度 1/5 1 小时以内 新手友好度 83/100
lysine-dev/okhttp#9828 ·
维护者通常 1 天内回复
-
status: waiting-for-triage
难度 2/5 1-3 小时 新手友好度 70/100
spring-projects/spring-ai#7269 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 89/100
objectionary/hone-maven-plugin#1353 ·
维护者通常 9 天内回复
-
ClassNotFoundException: io.netty.channel.kqueue.KQueueEventLoop stack trace printed during native build on main (Netty 4.2 removed the class)可能已有人在做 @gastaldi 今天认领。 未关闭area/netty kind/bug
难度 2/5 1-3 小时 新手友好度 68/100
quarkusio/quarkus#57939 · 1 条评论 ·
维护者通常 1 天内回复