OpenAPITools / OpenAPITools/openapi-generator

[BUG] Nested arrays containing objects have no type

Open
#5,667 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Kotlin Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The generated type of an array that is nested within another array is erased to the "Any" type. The type is well defined in the OAPI declaration, and should be reflected in the generated code.

This issue does not happen for objects in array unless that array is within another array.

The issue also does not occur when the nested object type of specified via $ref. Examples provided.

openapi-generator version

master (4.3.0-SNAPSHOT)

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: API
  version: "1.0"
paths:
  /endpoint:
    get:
      operationId: getTest
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                type: object
                additionalProperties: false
                properties:
                  badProp:
                    type: array
                    items:
                      type: array
                      items:
                        type: object
                        additionalProperties: false
                        properties:
                          prop:
                            type: string
                  okProp:
                    type: array
                    items:
                      type: array
                      items:
                        $ref: '#/components/schemas/MyObj'
components:
  schemas:
    MyObj:
      type: object
      additionalProperties: false
      properties:
        prop:
          type: string
Command line used for generation

java -jar openapi-generator-cli.jar generate -g kotlin -i spec.yaml -o gen

(also fails for Swift and probably any typed languge)

Steps to reproduce
  1. Save the example declaration above to spec.yaml & run the example command-line
  2. Open the generated model file: cat gen/src/main/kotlin/org/openapitools/client/models/InlineResponse200.kt
  3. Observe invalid property, which includes kotlin.Any inner type:
val badProp: kotlin.Array<kotlin.Array<kotlin.Any>>? = null
  1. Observe correct version, when using $ref, the inner type is MyObj:
val okProp: kotlin.Array<kotlin.Array<MyObj>>? = null

Desired output:

val badProp: kotlin.Array<kotlin.Array<MyObj>>? = null

Either MyObj or a generated inline model. It should just not be Any.

Related issues/PRs

n/a

Suggest a fix

The code used to introspect the properties of arrays should respect for nested child arrays. I have not been able to figure out a fix for this yet.

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

Start with spec.yaml and reproduce the issue using the shown Java CLI command. Inspect gen/src/main/kotlin/org/openapitools/client/models/InlineResponse200.kt, comparing badProp with the $ref-based okProp. Done means nested inline objects retain their generated type instead of kotlin.Any, with the behavior also checked for Swift if applicable.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, openapi, swift
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.