Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[BUG][KOTLIN] multiplatform/jvm-ktor: `DELETE` without `requestBody` sends `"Content-Type: application/json"` with an empty body

未关闭 适合新手
#25,223 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@mohitduhan19 已经在做这个了。

开始于 2026年10月13日。

  • #25243 来自 @mohitduhan19 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
kotlin
领域
backend

调研方向

阅读 modules/openapi-generator/src/main/resources/kotlin-client/libraries/multiplatform/infrastructure/ApiClient.kt.mustache 和对应的 libraries/jvm-ktor/infrastructure/ApiClient.kt.mustache;检查函数 request 如何处理 EmptyContent,以及设置内容类型和请求体的方法。检查这些库现有的模板测试,然后验证不带 requestBody 的生成 DELETE 请求会省略内容类型和请求体,而带有 JSON 请求体的 DELETE 请求仍会发送两者。

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

描述

Issue: Bug
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
  1. Generate the client from the spec above with -g kotlin --library multiplatform.
  2. Call deleteItem("1", 1) against any server, or a request logger.
  3. 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.

主要语言
Java
星标
26.8k
派生
7.7k
平均合并
1 天 15 小时
30 天内合并 PR
144

环境准备

在 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 摘要。