Repository navigation
fix(java): validate string enum values excluded by not - #25066
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 4 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
53ddfd7 to
fd764ca
Compare
|
@KannaKim @martin-mfg would you mind taking a look when you have a chance? This is a small Java client bug fix with regression coverage and green CI. This issue currently blocks our work on a new Dropbox SDK generated with OpenAPI Generator, so getting it into the next release would be very helpful. Thanks! |
|
thanks for the PR cc @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08) @KannaKim (2026/07) |
|
tested and the change compiles without issues, e.g. -@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2026-10-05T16:16:20.930519800+08:00[Asia/Hong_Kong]", comments = "Generator version: 7.26.0-SNAPSHOT")
+@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2026-10-05T16:18:30.360073600+08:00[Asia/Hong_Kong]", comments = "Generator version: 7.26.0-SNAPSHOT")
public class Other {
public static final String SERIALIZED_NAME_KIND = "kind";
@SerializedName(SERIALIZED_NAME_KIND)
@@ -153,6 +153,16 @@ public class Other {
}
}
JsonObject jsonObj = jsonElement.getAsJsonObject();
+ if (jsonObj.get("kind") != null
+ && !jsonObj.get("kind").isJsonNull()
+ && jsonObj.get("kind").isJsonPrimitive()
+ && jsonObj.get("kind").getAsJsonPrimitive().isString()
+ && "known".equals(jsonObj.get("kind").getAsString())) {
+ throw new IllegalArgumentException(String.format(
+ java.util.Locale.ROOT,
+ "The field `kind` in the JSON string matches a value disallowed by `not`: `%s`",
+ jsonObj.get("kind").toString()));
+ }
but the indentation seems missing 2 spaces. will file a follow up PR to fix it. UPDATE: merged #25122 to fix the indentation. |
…penAPITools#25066) Gson's validateJsonElement and the leaf JSON-B deserializers now reject a string property value listed in its `not: enum` (inline or $ref), so a oneOf with complementary branches resolves to exactly one of them. Jackson is left as is, like the other Jackson-based Java libraries.
Picks up OpenAPITools/openapi-generator#25066, which enforces not: enum when matching oneOf members.
Fixes #25065
What does this PR do?
Fixes Java
okhttp-gsonvalidation for string properties that usenotwith anenum.For a schema such as:
the generated
validateJsonElementmethod previously checked only thatkindwas a string and did not enforce the value excluded bynot.This caused
oneOfschemas that rely onnot: enumto become ambiguous. For example,{"kind":"known"}could incorrectly match both the schema that explicitly allows"known"and the schema that excludes it.Implementation
composedSchemas.notmetadata during Java client model-property post-processing.okhttp-gsonPOJO template.okhttp-gsonand string enums undernot.Tests
Added an OpenAPI 3.1 regression fixture with:
Known.kind:enum: [known]Other.kind:not: { enum: [known] }Union:oneOf: [Known, Other]Added a Java client generation test that verifies the generated
Othervalidation:kindproperty;"known";not_schemaproperty.Targeted test:
passes successfully.
Summary by cubic
Fixes Java
okhttp-gsonvalidation so string properties usingnotwith anenumreject excluded values, resolving ambiguity inoneOfschemas that rely onnot: enum(issue #25065).Previously
validateJsonElementonly checked that the property was a string, so{"kind":"known"}could match both a schema allowing"known"and one excluding it. The generator now reads the excluded values fromcomposedSchemas.not, exposes them via a vendor extension, and generates validation that throws anIllegalArgumentExceptionfor disallowed values.The change is scoped to Java
okhttp-gsonstring enums undernot, and adds a regression fixture plus a generation test covering theoneOfscenario, including enum references.Written for commit fd764ca. Summary will update on new commits.