OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Go] generator for array typed schema component with --generate-alias-as-model flag creates uncompilable model

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

Hi,
There is an issue in the modules/openapi-generator/src/main/resources/go/model_simple.mustache template, introduced in commit 4487042.

The ToMap function is not handling properly the case when the model isArray

func (o {{classname}}) ToMap() (map[string]interface{}, error) {
	toSerialize := {{#isArray}}make([]interface{}, len(o.Items)){{/isArray}}{{^isArray}}map[string]interface{}{}{{/isArray}}
...
	return toSerialize
}

here the ToMap function is returning a map[string]interface{} but the toSerialize will be a []interface{} in case the model is array.

Previously this was part of the MarshalJSON function, now this new MappedNullable interface is introduced for some reason, so the ToMap function was created but the original business logic template left there.

openapi-generator version

tested with openapitools/openapi-generator-cli:6.3.0
with openapitools/openapi-generator-cli:6.2.1 this issue does not occure.

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  description: test
  title: test
  version: 1.0.0
paths: {}
components:
  schemas:
    A:
      items:
        properties:
          B:
            type: integer
        type: object
      type: array
Generation Details
openapi-generator-cli generate --generate-alias-as-model -i A.yaml -g go -o .
Steps to reproduce

Generate Go model with --generate-alias-as-model flag from the provided schema.

Related issues/PRs
Suggest a fix

I did not dig too deep why the MappedNullable interface had been introduced, but I can think of 3 solutions:

  • Introduce a ArrayedNullable interface with []interface{} return type and use that in case the component is array typed
  • Change the ToMap function's return value to interface{} or any in the MappedNullable and olso in the generated ToMap
  • Revert back to the original MarshalJson implementation without the ToMap function call.

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 reading modules/openapi-generator/src/main/resources/go/model_simple.mustache, then run the provided openapi-generator-cli command with the YAML schema and --generate-alias-as-model. Check the generated array model's ToMap return type and compile the generated Go model. Done means array-typed schema components generate compilable Go code.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
api, 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.