OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Go] generator generates uncompilable model code in the NullableXY struct's Unset function

Open
#14,796 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

Description

If I generate a Go model from a schema which has an array typed component and use the --generate-alias-as-model the generated go code is not compiling.

A snippet from the generated code:

type NullableA struct {
	value A    <----- value is not a pointer
	isSet bool
}

...

func (v *NullableA) Unset() {
	v.value = nil   <----- this can't be nil
	v.isSet = false
}

The origin of the problem is around the nullable model template where it seems the value is not getting that * to become a pointer.
openapi-generator/src/main/resources/go/nullable_model.mustache

type Nullable{{{classname}}} struct {
	value {{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{{classname}}}
	isSet bool
}
openapi-generator version

tested with openapitools/openapi-generator-cli:6.0.0 and later

OpenAPI declaration file content or url

openapi: 3.0.1
info:
  title: test
  version: 1.0.0
  description: test
paths: {}
components:
  schemas:
    A:
      type: array
      items:
        type: object
        properties:
          B:
            type: integer

Generation Details

Used an array typed component and the --generate-alias-as-model flag with the go generator.

Steps to reproduce
openapi-generator-cli generate --generate-alias-as-model -i A.yaml -g go -o .
Related issues/PRs
Suggest a fix

In the openapi-generator/src/main/resources/go/nullable_model.mustache template:

  • either the pointer declaration should not be skipped in case the model isArray
  • or use the same filtering conditions in the Unset ({{^isArray}}{{^isFreeFormObject}}) as this can also be a problem in case of models which are isFreeFormObjects as the nil assignemnt could fail in those cases too.

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 openapi-generator/src/main/resources/go/nullable_model.mustache and reproduce the issue using the provided A.yaml and generate command with --generate-alias-as-model. Compare the pointer declaration with the Unset function for array and free-form models. Done means the generated Go model compiles and NullableA.Unset does not assign nil to a non-pointer value.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.