[BUG][KOTLIN] multiplatform/jvm-ktor: `DELETE` without `requestBody` sends `"Content-Type: application/json"` with an empty body
Maintainer thường phản hồi trong vòng 1 ngày
Đá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
- 78/100
Hướng nghiên cứu
Đọc modules/openapi-generator/src/main/resources/kotlin-client/libraries/multiplatform/infrastructure/ApiClient.kt.mustache và tệp tương ứng libraries/jvm-ktor/infrastructure/ApiClient.kt.mustache; kiểm tra cách hàm request xử lý EmptyContent và các phương thức đặt loại nội dung và phần thân. Kiểm tra các bài kiểm thử template hiện có cho những thư viện này, rồi xác minh rằng DELETE được tạo không có requestBody sẽ bỏ qua loại nội dung và phần thân, còn DELETE có phần thân JSON vẫn gửi cả hai.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
For operations without a requestBody, the kotlin client with library=multiplatform generates localVariableBody = io.ktor.client.utils.EmptyContent. ApiClient.request then sets a JSON content type and the body for every PUT, POST, PATCH, and DELETE, regardless of whether there is a body:
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH, RequestMethod.DELETE)) {
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
?: ContentType.Application.Json)
this.contentType(contentType)
this.setBody(body)
}
As a result, a DELETE /items/{id}?row=1 without a request body goes out with Content-Type: application/json and Content-Length: 0. Servers that bind a JSON body whenever the content type says JSON reject the request. ASP.NET Core with FastEndpoints answers:
{"statusCode":400,"message":"One or more errors occurred!","errors":{"/SerializerErrors":["The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0."]}}
The same DELETE succeeds when the client omits the Content-Type header. DELETE operations that do declare a JSON requestBody work as expected.
The same block exists in libraries/jvm-ktor/infrastructure/ApiClient.kt.mustache.
openapi-generator version
7.25.0 (Gradle plugin org.openapi.generator). The same code is in the 7.26.0 templates and on master (modules/openapi-generator/src/main/resources/kotlin-client/libraries/multiplatform/infrastructure/ApiClient.kt.mustache, the request function). Not a regression as far as I can tell.
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Delete without body
version: 1.0.0
paths:
/items/{id}:
delete:
operationId: deleteItem
parameters:
- name: id
in: path
required: true
schema:
type: string
- name: rowVersion
in: query
required: true
schema:
type: integer
responses:
'204':
description: Deleted
Generation Details
generatorName: kotlin
library: multiplatform
additionalProperties:
dateLibrary: kotlinx-datetime
Steps to reproduce
- Generate the client from the spec above with
-g kotlin --library multiplatform. - Call
deleteItem("1", 1)against any server, or a request logger. - Observe the request headers:
Content-Type: application/json,Content-Length: 0.
Actual: a JSON content type is sent although the operation has no request body.
Expected: no Content-Type header (and no body) when the operation declares no requestBody.
Related issues/PRs
None found (searched for "multiplatform DELETE content-type empty body" and "EmptyContent Content-Type").
Suggest a fix
Only set the content type and body when there is a body, for example:
if (requestConfig.method in listOf(RequestMethod.PUT, RequestMethod.POST, RequestMethod.PATCH, RequestMethod.DELETE) &&
body != null && body !is io.ktor.client.utils.EmptyContent
) {
val contentType = (requestConfig.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
?: ContentType.Application.Json)
this.contentType(contentType)
this.setBody(body)
}
The same change applies to the jvm-ktor template.
- Ngôn ngữ chính
- Java
- Star
- 26.8k
- Fork
- 7.7k
- Merge trung bình
- 1 ngày 12 giờ
- Pull request đã merge (30 ngày)
- 141
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] ExampleGenerator returns the string "{}" (or null) for `type: object` schemas without `properties`Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OpenAPITools/openapi-generator#25237 ·
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 78/100
OpenAPITools/openapi-generator#25236 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] OpenApiEvaluator reports "default value not in enum" for numerically equal values with different scale (e.g. 1.5 and 1.50)Có thể đã có người làm @mohitduhan19 đã nhận 1 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 78/100
OpenAPITools/openapi-generator#25233 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] OpenApiEvaluator emits "Unused model" warnings even when recommendations are disabledCó thể đã có người làm @mohitduhan19 đã nhận 1 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 82/100
OpenAPITools/openapi-generator#25232 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Issue: Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
OpenAPITools/openapi-generator#25218 ·
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ự
-
RRateLimiter acquire methods accept negative permits and raise available permits above the rate.Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 4 ngày
-
README 中的代码语言数量与当前目录不一致Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
krahets/hello-algo#1993 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
NationalSecurityAgency/ghidra#9884 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Status: Pending Review Type: Feature Request
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
OpenRefine/OpenRefine#8019 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[TEMP] pin maven version to 3.9.xCó thể đã có người làm @petrenyi-mark đã nhận hôm nay. Đang mở
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 62/100