Skip to content

[BUG][Java][okhttp-gson] Cannot serialize oneOf array of strings #25541

Open

Description

@AndreyVMarkelov

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?
Description

The Java okhttp-gson client throws IllegalStateException: Not a JSON Primitive: ["alpha","beta"] when serializing a oneOf value containing an array of strings. The expected result is the JSON array ["alpha","beta"].

openapi-generator version

Reproduced on master 57edb417ea3fb15de7bc523a133c9cc016acab19 (7.28.0-SNAPSHOT).

OpenAPI declaration file content
openapi: 3.0.3
info:
  title: oneOf primitive array
  version: '1'
paths: {}
components:
  schemas:
    Union:
      oneOf:
        - $ref: '#/components/schemas/Variant'
        - type: array
          items:
            type: string
    Variant:
      type: object
      required: [kind]
      properties:
        kind:
          type: string
Generation Details
java -jar openapi-generator-cli.jar generate -g java --library okhttp-gson -i spec.yaml -o out
Steps to reproduce

Using the generated client and its dependencies:

Union value = JSON.deserialize("[\"alpha\",\"beta\"]", Union.class);
System.out.println(JSON.serialize(value));

The deserialization succeeds but serialization throws IllegalStateException: Not a JSON Primitive. A separate pre-existing limitation causes empty arrays to be rejected by setActualInstance; the reproduction above uses a nonempty array.

Related issues/PRs

#25539 also concerns Java Gson oneOf generation, but this issue reproduces on master without that PR.

Suggest a fix

In the generated Gson adapter, write array-valued primitive schemas as JsonArray instead of converting them to JsonPrimitive.

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