Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở Phù hợp với người mới
#25,218 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue nà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
65/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu tại modules/openapi-generator/src/main/resources/csharp/ApiClient.mustache gần dòng 500 và lần theo lời gọi FromJson cùng cách xử lý throwOnAnyError hiện có. Tái hiện lỗi bằng một RestSharp client được tạo với phản hồi oneOf hoặc anyOf không khớp với biến thể nào và một phản hồi 200. Hoàn tất khi lỗi phân tích đến được bên gọi dưới dạng ApiException chứa status 200, nội dung thô, headers và InvalidDataException là ngoại lệ bên trong.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Java
Star
26.8k
Fork
7.7k
Merge trung bình
1 ngày 15 giờ
Pull request đã merge (30 ngày)
145

Chuẩn bị môi trường

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của OpenAPITools/openapi-generator

Tất cả issue của OpenAPITools/openapi-generator

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.