OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Go] Optional array properties are rendered as values instead of pointers

Open
#22,096 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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?
  • 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When creating a schema that contains an optional field of type array, the resulting code will always generate a slice type in the resulting Go code.

This makes it impossible to differentiate between input that:

  • contains the array property, with an empty list
  • does not contain the array property

Adding nullable: true has no influence on the result. Adding an indirect schema that wraps the array type has no effect either.

openapi-generator version

7.16.0

OpenAPI declaration file content or url
---
openapi: 3.0.0
info:
  title: unset
  version: "1"
paths: {}
components:
  schemas:
    Bar:
      type: object
      properties:
        X:
          type: integer
        Y:
          type: string
        Z:
          # has no influence on the result:
          # nullable: true
          type: array
          items:
            $ref: "#/components/schemas/Baz"
      required:
        - X
    Baz:
      type: string
Generation Details

No special options are used.

The CLI was called as follows:

openapi-generator-cli generate -i openapi.yaml -g go -o openapi
Steps to reproduce

Run the CLI as described above on the provided template.

This produces a model like this:

// Bar struct for Bar
type Bar struct {
        X int32 `json:"X"`
        Y *string `json:"Y,omitempty"`
        Z []string `json:"Z,omitempty"`
}

My expectation would be that Z has the type *[]string instead.

Related issues/PRs

There were several discussions about how to handle optional and nullable properties in the past (most prominently: https://github.com/OpenAPITools/openapi-generator/issues/522), but it looks like handling of array types was not implemented in the same way as for other types.

I tested a few older versions of openapi-generator, and found that 4.3.1 does indeed produce a pointer type (*[]string). So this seems like a regression to me, and that it was simply forgotten when handling of optional properties was overhauled in the Go generator. Ironically, 4.3.1 doesn't produce a pointer type on property Y.

Suggest a fix

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 by running the stated openapi-generator-cli command with the provided openapi.yaml and the Go generator, then inspect the generated Bar model. Trace how the optional array property Z is represented and compare it with the expected *([]string) output. Done means optional arrays preserve the distinction between an absent property and an empty list.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.