Skip to content

[BUG][JAVA] Oneof + type discrimination + empty arrays => IndexOutOfBoundsException #21276

@gurgl

Description

@gurgl

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?

Code that handles empty arrays

  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I'm using the openapi-generator-maven-plugin with default settings (uses okhttp-gson) to produce a java client from an openapi.json file.. I've noticed that oneof declarations, relying on type discrimination AND having array types, leads to problems with empty arrays. It's the

list.get(0)

...which will lead to index-out-of-bounds exceptions on empty arrays. Through debugging I've pinned the problem to this file:

https://github.com/OpenAPITools/openapi-generator/blob/6ee94d636b7a7e06b74faf4423b175a7271d5343/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/oneof_model.mustache

Looks to me like it can never work for empty arrays? And I see no other way, than sending an empty array to signal that an instance of that particular oneof variant was sent - as this is information too. IMO you shouldn't need to do special handling on the producer to make the consumer happy for this basic case.

Other than that I more than happy with your framework. I feed a 2k line openapi spec into it and it's been working great this far.

openapi-generator version

7.12.0

OpenAPI declaration file content or url

From #/component/schemas

   "ComponentF_unit_PlainOrAOU_Char": {
      "oneOf": [
          {
              "$ref": "#/components/schemas/FormF_PlainOrAOU_Char"
          },
          {
              "items": {
                  "$ref": "#/components/schemas/FormF_PlainOrAOU_Char"
              },
              "type": "array"
          }
      ],
      "type": "object"
  },

Generation Details

java + maven with default settings

Steps to reproduce

Create a oneof that that has 2 elements. On scalar type and one array of same scalar type. serialize and empty array using the array-type alternative

Related issues/PRs

couldn't find any

Suggest a fix

Either only do type checking on non empty arrays or get rid of the type checking all together.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions