Skip to content

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

Closed

Description

@AndreyVMarkelov

Description

For the Java okhttp-gson client, a property schema using not with a string enum is not enforced by the generated validateJsonElement method.

This can make a oneOf ambiguous even when the schemas are mutually exclusive according to the OpenAPI/JSON Schema definition.

OpenAPI Generator version

7.26.0-SNAPSHOT

Generator

java with library=okhttp-gson

OpenAPI definition

openapi: 3.1.0

info:
  title: oneOf not enum repro
  version: 1.0.0

paths: {}

components:
  schemas:
    Known:
      type: object
      required:
        - kind
      properties:
        kind:
          type: string
          enum:
            - known

    Other:
      type: object
      required:
        - kind
      properties:
        kind:
          type: string
          not:
            enum:
              - known

    Union:
      oneOf:
        - $ref: '#/components/schemas/Known'
        - $ref: '#/components/schemas/Other'

The spec validates successfully with the OpenAPI Generator CLI. The validator only reports recommendations for unused models because there are no paths.

Current behavior

For Other.kind, the generated model validates that kind is a string, but does not enforce the nested:

not:
  enum:
    - known

As a result, {"kind":"known"} matches both Known and Other, and deserializing Union fails because two oneOf schemas match.

Expected behavior

The generated validation for Other.kind should reject "known", so:

  • {"kind":"known"} matches only Known
  • another string value such as {"kind":"other"} matches Other

Reproduction

With a generated Java client, deserializing {"kind":"known"} as Union currently fails with the oneOf multiple-match error.

A regression test covering both the known and unknown tag cases demonstrates the issue.

Proposed fix

For Java okhttp-gson, propagate string enum values from a property's composedSchemas.not metadata to the parent property and generate validation that rejects those values in pojo.mustache.

The change is intentionally limited to string enums under not; it does not claim general support for all not schemas or enum types.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions