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

[BUG][csharp][restsharp] throwOnAnyError: oneOf/anyOf deserialization failure on 2xx escapes as InvalidDataException instead of ApiException

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

从 modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache 第 500 行附近开始,追踪 FromJson 调用以及现有的 throwOnAnyError 处理逻辑。使用生成的 RestSharp 客户端进行复现:响应为无法匹配任何变体的 oneOf 或 anyOf,且状态为 200。完成标准是解析失败以 ApiException 的形式传递到调用方,其中包含状态 200、原始响应正文、响应头,以及作为内部异常的 InvalidDataException。

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

描述

Issue: Bug
Description

With throwOnAnyError=true the generated ApiClient promises that every failure surfaces as ApiException. For a 2xx response whose body matches none of the oneOf/anyOf variants, FromJson throws InvalidDataException after the ErrorException guard has already passed, so the raw exception reaches the caller without status code, body or headers.

openapi-generator version

master (after #25217)

Steps to reproduce
  1. Generate a csharp client with --library restsharp --additional-properties throwOnAnyError=true from a spec where an operation returns a oneOf model.
  2. Call that operation against a server that returns 200 with a body matching no variant.
  3. Observe InvalidDataException instead of ApiException.
Expected behaviour

ApiException with ErrorCode = 200, ErrorContent = raw body, Headers populated, and the InvalidDataException as InnerException.

Original findings

P2: The FromJson call below can throw InvalidDataException for a 2xx body that matches no oneOf/anyOf schema, after this check has passed; the raw exception escapes instead of the promised ApiException. Wrap that parse failure with the response status, content, and headers.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache, line 500:

<comment>The `FromJson` call below can throw `InvalidDataException` for a 2xx body that matches no oneOf/anyOf schema, after this check has passed; the raw exception escapes instead of the promised `ApiException`. Wrap that parse failure with the response status, content, and headers.</comment>

<file context>
@@ -495,6 +494,14 @@ namespace {{packageName}}.Client
+                {{#throwOnAnyError}}
+
+                // 4xx/5xx also carry ErrorException; those go through ExceptionFactory so the body is kept
+                if (response.ErrorException != null && (int)response.StatusCode < 400)
+                {
+                    throw new ApiException((int)response.StatusCode, string.Format("Error calling {0}: {1}", request.Resource, response.ErrorMessage), response.Content, ToApiResponse(response).Headers);
</file context>

Originally posted by @cubic-dev-ai[bot] in #25217

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

环境准备

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