OpenAPITools / OpenAPITools/openapi-generator

[JAVA] "Object" generated for 2d arrays

Open
#11,800 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Colleagues of mine were puzzled with following: when they describe an object that has a 2d array of objects as one of the properties, when the schema of elements in the array is defined inline, Java and Spring generators produce a List<List<Object>>, where Object in runtime becomes some meaningful type. When the elements are references to a component schema, the generators produce a separate class.

For example, here is a schema:

components:
  schemas:
    data:
      type: object
      properties:
        stuff:
          type: array
          items:
            type: array
            items:
              type: object
              additionalProperties: false
              properties:
                first:
                  type: integer
                second:
                  type: integer

It leads to a following using java generator:

@SerializedName(SERIALIZED_NAME_STUFF)
private List<List<Object>> stuff = null;

One can change the schema as follows:

components:
  schemas:
    data:
      type: object
      properties:
        stuff:
          type: array
          items:
            type: array
            items:
              $ref: "#/components/schemas/measurement"
              
    measurement:
      type: object
      additionalProperties: false
      properties:
        first:
          type: integer
        second:
          type: integer

It becomes:

@SerializedName(SERIALIZED_NAME_STUFF)
private List<List<Measurement>> stuff = null;

The corresponding class "Measurement" is also generated.

The imperfections with this are:

  • Object is not very descriptive, if you do not know it in advance, you need to check the type at runtime with a debugger
  • When you want to access the properties of the object, you have to (down-)cast it to the actual type
  • It is not immediately clear that inline and split schemas produce different results

Describe the solution you'd like

To me, the code generated for second schema is much better while the schemas are equivalent. My question is whether it would be possible to change the generator in one of the following ways:

  • Change Object to an actual type of the element (in my example it will be Map<String, String>).
  • Always generate a class for elements of 2d arrays

Additional context

The full schemas and command line statement to generate Java and Spring classes from them:

openapi.yaml

openapi: 3.0.3
info:
  title: Example
  description: Example 2d array
  version: 0.0.1
paths:
  /do-stuff:
    put:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/data"

      responses:
        200:
          description: Data updated

components:
  schemas:
    data:
      type: object
      properties:
        stuff:
          type: array
          items:
            type: array
            items:
              type: object
              additionalProperties: false
              properties:
                first:
                  type: integer
                second:
                  type: integer

openapi_split.yaml

openapi: 3.0.3
info:
  title: Example
  description: Example 2d array
  version: 0.0.1
paths:
  /do-stuff:
    put:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/data"          

      responses:
        200:
          description: Data updated

components:
  schemas:
    data:
      type: object
      properties:
        stuff:
          type: array
          items:
            type: array
            items:
              $ref: "#/components/schemas/measurement"
              
    measurement:
      type: object
      additionalProperties: false
      properties:
        first:
          type: integer
        second:
          type: integer

Generation script:

java -jar openapi-generator-cli-5.4.0.jar generate -i openapi.yaml -g java -o java 
java -jar openapi-generator-cli-5.4.0.jar generate -i openapi_split.yaml -g java -o java_split
java -jar openapi-generator-cli-5.4.0.jar generate -i openapi.yaml -g spring -o spring
java -jar openapi-generator-cli-5.4.0.jar generate -i openapi_split.yaml -g spring -o spring_split

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the discrepancy with openapi.yaml and openapi_split.yaml using the listed Java and Spring generation commands, then trace how each generator resolves inline nested array items versus component references. A complete change should establish the intended generated type or model behavior for both schemas and add regression coverage for the 2D-array case; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
backend-api-design, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.