OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Go] generator for array typed schema component with --generate-alias-as-model flag creates uncompilable model
Nobody has claimed this yet.
- 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
ArrayedNullableinterface with[]interface{}return type and use that in case the component isarraytyped - Change the
ToMapfunction's return value tointerface{}oranyin theMappedNullableand olso in the generatedToMap - Revert back to the original
MarshalJsonimplementation without theToMapfunction call.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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