[BUG][KOTLIN] multiplatform/jvm-ktor: `DELETE` without `requestBody` sends `"Content-Type: application/json"` with an empty body
维护者通常 1 天内回复
评估
调研方向
阅读 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 内容生成。
描述
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.
- 主要语言
- Java
- 星标
- 26.8k
- 派生
- 7.7k
- 平均合并
- 1 天 15 小时
- 30 天内合并 PR
- 144
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
OpenAPITools/openapi-generator 的其他 Issue
-
[BUG][RUST] Reqwest - encoding of spaces in path parameters可能已有人在做 @GlenWright 于 2 天前认领。 未关闭Issue: Bug
难度 2/5 1-3 小时 新手友好度 68/100
OpenAPITools/openapi-generator#25257 · 3 条评论 ·
维护者通常 1 天内回复
-
[BUG] ExampleGenerator returns the string "{}" (or null) for `type: object` schemas without `properties`可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
OpenAPITools/openapi-generator#25237 ·
维护者通常 1 天内回复
-
[BUG] OpenApiEvaluator reports "default value not in enum" for numerically equal values with different scale (e.g. 1.5 and 1.50)可能已有人在做 @mohitduhan19 于 4 天前认领。 未关闭Issue: Bug
难度 2/5 1-3 小时 新手友好度 78/100
OpenAPITools/openapi-generator#25233 ·
维护者通常 1 天内回复
-
[BUG] OpenApiEvaluator emits "Unused model" warnings even when recommendations are disabled可能已有人在做 @mohitduhan19 于 4 天前认领。 未关闭Issue: Bug
难度 2/5 1-3 小时 新手友好度 82/100
OpenAPITools/openapi-generator#25232 ·
维护者通常 1 天内回复
-
Issue: Bug
难度 2/5 1-3 小时 新手友好度 65/100
OpenAPITools/openapi-generator#25218 ·
维护者通常 1 天内回复
查看 OpenAPITools/openapi-generator 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
apache/rocketmq-dashboard#6896 ·
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
OpenIntegrationEngine/engine#496 ·
维护者通常 1 天内回复
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 76/100
SasanLabs/VulnerableApp#836 ·
维护者通常 1 天内回复