[BUG][JAVA][native] oneOf without discriminator rejects valid payloads when variant required properties differ
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 20/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- java, openapi
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Start with the generated native model templates, especially oneof_model.mustache, and inspect how model constructors and required properties are generated; the report identifies @JsonCreator as a possible approach. Review the linked pull request #25728, which indicates work is already underway. Done means valid payloads deserialize to their matching oneOf variant and payloads missing that variant’s required fields are rejected.
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?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
With java + library: native, a oneOf of two object schemas without a discriminator cannot deserialize any valid payload when the variants are distinguished by their required properties. Every variant is accepted for every input, so the generated oneOf deserializer always reports more than one match:
Failed deserialization for Author: 2 classes match result, expected 1
The root cause is that the generated native models only have a no-arg constructor and setters. Annotating a field with @JsonProperty(required = true) does not make Jackson reject a missing property, and unknown properties are ignored. The oneOf deserializer in oneof_model.mustache counts a match whenever readValueAs() does not throw, so both object variants match any JSON object — including valid ones.
This is exactly the case where oneOf is resolvable without a discriminator: the required sets are disjoint (subject vs id + domainId), so a JSON Schema validator accepts exactly one variant. The generated client rejects all of them.
openapi-generator version
Reproduced with 7.27.0 and with the latest master before the fix (commit e4370ff1e1f).
This does not appear to be a recent regression; the native oneOf deserializer has counted successful subtype deserializations for a long time. A discriminator works around it, but the spec does not need one (and the real-world spec this was found in does not use one).
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: oneOf required properties
version: 1.0.0
paths:
/author:
get:
operationId: getAuthor
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Author'
components:
schemas:
Author:
oneOf:
- $ref: '#/components/schemas/ServiceAuthor'
- $ref: '#/components/schemas/UserAuthor'
ServiceAuthor:
type: object
required:
- subject
properties:
subject:
type: string
UserAuthor:
type: object
required:
- id
- domainId
properties:
id:
type: string
domainId:
type: string
Complete standalone repro (spec, Java harness, run script, expected outputs): https://gist.github.com/thorstenhirsch/9d1d931032530c778a414725163f4eca
Generation Details
java -jar openapi-generator-cli-7.27.0.jar generate \
-g java --library native \
-i spec.yaml -o out \
--additional-properties=useJackson3=true,apiPackage=repro.api,modelPackage=repro.model,invokerPackage=repro
Then:
cd out
mvn -q -DskipTests compile
mvn -q dependency:build-classpath -Dmdep.outputFile=cp.txt
cd ..
CP="out/target/classes:$(cat out/cp.txt)"
javac -cp "$CP" -d out Reproduce.java
java -cp "$CP:out" Reproduce
Steps to reproduce
Reproduce.java reads three JSON documents into the generated Author type:
new JsonMapper().readValue("{\"subject\":\"Jane\"}", Author.class);
new JsonMapper().readValue("{\"id\":\"u-1\",\"domainId\":\"acme\"}", Author.class);
new JsonMapper().readValue("{\"id\":\"u-1\"}", Author.class); // must be rejected
Actual result (7.27.0):
FAIL service author (subject only) rejected: Failed deserialization for Author: 2 classes match result, expected 1
FAIL user author (id + domainId) rejected: Failed deserialization for Author: 2 classes match result, expected 1
OK incomplete user author (id only) rejected: Failed deserialization for Author: 2 classes match result, expected 1
RESULT: 2 FAILURE(S)
Expected result:
OK service author (subject only) -> ServiceAuthor
OK user author (id + domainId) -> UserAuthor
OK incomplete user author (id only) rejected: Failed deserialization for Author: 0 classes match result, expected 1
RESULT: ALL OK
With an opt-in @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) constructor that takes the required properties (generateJsonCreator=true in a local patch), the required sets are enforced. Both valid payloads deserialize as the correct variant, and the incomplete payload is rejected with 0 classes match result, expected 1:
OK service author (subject only) -> ServiceAuthor
OK user author (id + domainId) -> UserAuthor
OK incomplete user author (id only) rejected: Failed deserialization for Author: 0 classes match result, expected 1
RESULT: ALL OK
The same required-property enforcement is also relevant for anyOf and for XML (withXml=true), since those deserializers use the same generated model classes. The oneOf-specific multi-match failure above is what this issue reports.
Related issues/PRs
I searched for an existing report for the Java native library, but the closest issues are different:
- #9606:
anyOfwith a discriminator where one schema is a subset of another; that case is genuinely ambiguous and is not fixed by enforcing required properties. - #20545:
jaxrs-spec, where the generated@JsonCreatortakes the union of alloneOffields and therefore rejects valid input. That is the opposite problem and a different library. - #20399:
jersey2, aboutisNullablebeing lost during deserialization, not about subtype matching. - #19577:
jaxrs-spec, required fields are not enforced at all; nooneOf.
Suggest a fix
Enforce required properties while deserializing models, so that a subtype that is missing a required property fails and does not count as a oneOf match. For Jackson this means a @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) constructor whose parameters are the required properties, annotated with @JsonProperty(value = "...", required = true).
For the native library this should be opt-in (e.g. a new generateJsonCreator option) to avoid changing the behavior of existing clients. I have a patch for this, including support for inherited required properties (super(...)), read-only properties and XML annotations, and I will open a PR.
- Ngôn ngữ chính
- Java
- Star
- 26.9k
- Fork
- 7.8k
- Merge trung bình
- 2 ngày 23 phút
- Pull request đã merge (30 ngày)
- 177
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
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
OpenAPITools/openapi-generator#25768 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Enhancement: Feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
OpenAPITools/openapi-generator#25753 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUBY-NEXTGEN] A non-String value in a pattern-constrained attribute raises NoMethodErrorCó thể đã có người làm @Inf1mum đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
OpenAPITools/openapi-generator#25724 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUBY-NEXTGEN] Response#to_hash is advertised and raises when the data is an ArrayCó thể đã có người làm @Inf1mum đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
OpenAPITools/openapi-generator#25723 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG][RUBY-NEXTGEN] A JSON string response raises ApiError because the body is parsed twiceCó thể đã có người làm @Inf1mum đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
OpenAPITools/openapi-generator#25719 · 1 bình luận ·
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ự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
eclipse-tradista/tradista#371 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Failed to start Rescue missionĐang mởcritical
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
mars-sim/mars-sim#2199 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG-02] Endpoints /assistente/relatorio/*/preview retornam 404 ("Ver na tela" dos relatórios)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
projeto-ong-amar/projeto-ong-amar-backend#26 ·
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 72/100
primefaces/primefaces#15568 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
崩溃日志只按 CRLF 分行Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
PCL-Community/PCL-CE#3872 ·
Maintainer thường phản hồi trong vòng 1 ngày