[BUG][KOTLIN] jvm-retrofit2: DELETE with requestBody fails with "Non-body HTTP method cannot contain @Body"
Maintainer thường phản hồi trong vòng 1 ngày
Một pull request liên quan đã được merge.
- #25469 của @cpruijsen — đã merge
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 80/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend-api-design, tooling
Hướng nghiên cứu
Bắt đầu với kotlin-client/libraries/jvm-retrofit2/api.mustache, nơi issue cho biết các chú thích Retrofit được tạo ra, rồi kiểm tra các bài kiểm thử template gần đó cho thư viện này. Tái hiện bằng đặc tả tối thiểu được liên kết và xác nhận rằng một thao tác DELETE có phần thân yêu cầu tạo ra một phương thức client có thể gọi, gửi phần thân JSON. Issue cũng chỉ ra Java/libraries/retrofit2/api.mustache là template liên quan cần kiểm tra.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
[!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.
- Ngôn ngữ chính
- Java
- Star
- 26.8k
- Fork
- 7.7k
- Merge trung bình
- 1 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 176
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của OpenAPITools/openapi-generator
-
[BUG][Java][Spring] Make HTTP header matching case insensitiveCó thể đã có người làm @vinirib đã nhận hôm nay. Đang mởIssue: Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
OpenAPITools/openapi-generator#25475 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[REQ][csharp][restsharp] Option to construct RestClient with useClientFactory: trueCó thể đã có người làm @MrD4rkne đã nhận hôm nay. Đang mởEnhancement: Feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
OpenAPITools/openapi-generator#25425 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[REQ][JAVA][resteasy] Support for Jackson 3 / useJackson3Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởEnhancement: Feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OpenAPITools/openapi-generator#25364 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUST] chrono date-time path/query/header params are not serialized as RFC 3339Có thể đã có người làm @teovillanueva đã nhận 13 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
OpenAPITools/openapi-generator#25297 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUST] Reqwest - encoding of spaces in path parametersCó thể đã có người làm @GlenWright đã nhận 16 ngày trước. Đang mởIssue: Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
OpenAPITools/openapi-generator#25257 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của OpenAPITools/openapi-generator
Issue tương tự
-
breaking change bug needs human review not ready priority medium size S
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
martin-francois/symphony-trello#1193 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Mend: dependency security vulnerability
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
status: waiting-for-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
spring-projects/spring-framework#37614 ·
Maintainer thường phản hồi trong vòng 1 ngày