Skip to content

[BUG][Java][Go-experimental] Referenced schema for array type badly generated #4362

@zippolyte

Description

@zippolyte

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

Defining a schema for an array type, and referencing it as the items type in another array doesn't generate valid code. It happened for me in Java and go-experimental that I'm using, but might be the case for other languages as well.

openapi-generator version

4.1.3

OpenAPI declaration file content or url

The following schemas definition exhibit the behavior

NestedArray:
  type: array
  items:
    type: string
MainSchema:
  type: object
  properties:
    nested_array:
      type: array
      items:
        $ref: '#/components/schemas/NestedArray'

In go, the generated output looks like this, which doesn't even compile since Array is undefined:

type MainSchema struct {
	NestedArray *[]Array `json:"nested_array,omitempty"`
}

instead of the expected output of either

type MainSchema struct {
	NestedArray *[]NestedArray `json:"nested_array,omitempty"`
}

if we generated a model for the NestedArray schema, or simply

type MainSchema struct {
	NestedArray *[][]string `json:"nested_array,omitempty"`
}

if we don't generate a model (which it seems is what is supposed to happen when we create a schema for a primitive type).

In java, the output is

public class MainSchema {
  public static final String JSON_PROPERTY_NESTED_ARRAY = "nested_array";
  private List<List> nestedArray = null;
}

instead of

public class MainSchema {
  public static final String JSON_PROPERTY_NESTED_ARRAY = "nested_array";
  private List<List<String>> nestedArray = null;
}

Please note that the correct output is generated when inlining the NestedArray definition instead of referencing it in the MainSchema, like so:

MainSchema:
  type: object
  properties:
    nested_array:
      type: array
      items:
        type: array
        items:
          type: string
Command line used for generation

openapi-generator generate --http-user-agent DataDog/1.0.0/java -g java -c config/languages/java_v1.json -i spec/v1/full_spec.yaml -o generated/datadog-api-client-java/v1 -t templates/java'

Steps to reproduce

Make a spec with the above schema definitions, and inspect the generated model for MainSchema

Related issues/PRs
Suggest a fix

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