Skip to content

fix(java): validate string enum values excluded by not - #25066

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/java-not-enum-validation
Oct 5, 2026
Merged

wing328 merged 1 commit into
OpenAPITools:masterfrom
AndreyVMarkelov:fix/java-not-enum-validation

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #25065

What does this PR do?

Fixes Java okhttp-gson validation for string properties that use not with an enum.

For a schema such as:

kind:
  type: string
  not:
    enum:
      - known

the generated validateJsonElement method previously checked only that kind was a string and did not enforce the value excluded by not.

This caused oneOf schemas that rely on not: enum to become ambiguous. For example, {"kind":"known"} could incorrectly match both the schema that explicitly allows "known" and the schema that excludes it.

Implementation

  • Reads string enum values from the property's composedSchemas.not metadata during Java client model-property post-processing.
  • Exposes those excluded values to the okhttp-gson POJO template.
  • Generates validation that rejects matching excluded string values.
  • Keeps the change intentionally scoped to Java okhttp-gson and string enums under not.

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 Other validation:

  • checks the actual kind property;
  • rejects "known";
  • does not incorrectly validate a synthetic not_schema property.

Targeted test:

JavaClientCodegenTest#testOneOfNotEnumValidation

passes successfully.


Summary by cubic

Fixes Java okhttp-gson validation so string properties using not with an enum reject excluded values, resolving ambiguity in oneOf schemas that rely on not: enum (issue #25065).

Previously validateJsonElement only 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 from composedSchemas.not, exposes them via a vendor extension, and generates validation that throws an IllegalArgumentException for disallowed values.

The change is scoped to Java okhttp-gson string enums under not, and adds a regression fixture plus a generation test covering the oneOf scenario, including enum references.

Written for commit fd764ca. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@AndreyVMarkelov

Copy link
Copy Markdown
Contributor Author

@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!

@wing328

wing328 commented Oct 5, 2026

Copy link
Copy Markdown
Member

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)

@wing328

wing328 commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

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.

@wing328
wing328 merged commit 39de92c into OpenAPITools:master Oct 5, 2026
15 checks passed
@wing328 wing328 added this to the 7.26.0 milestone Oct 5, 2026
rar91279 added a commit to rar91279/openapi-generator that referenced this pull request Oct 5, 2026
…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.
ariesclark added a commit to vrchatapi/vrchatapi-java that referenced this pull request Oct 7, 2026
Picks up OpenAPITools/openapi-generator#25066, which enforces not: enum when matching oneOf members.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Java][okhttp-gson] oneOf with not: enum does not exclude matching string enum values

2 participants