OpenAPITools / OpenAPITools/openapi-generator

[GoLang] Array Items anyOf do not properly map

Open
#2,622 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I have an object with a two-dimensional array, and the inner array items may be floator integer. If I attempt to generate the go client code it will create a nested map instead.

So instead of this:

Metrics map[string][][]map[string]interface{} `json:"metrics,omitempty"`

I was hoping for this:

Metrics map[string][][]interface{} `json:"metrics,omitempty"`
openapi-generator version

3.3.4

OpenAPI declaration file content or url
Command line used for generation

GO_POST_PROCESS_FILE="/usr/local/bin/gofmt -w" openapi-generator generate -i ../api/openapi.yaml -g go -o ../vendor/instana/openapi

Steps to reproduce

Create the client. (Bug is already apparent)
Invoke a service.
Note that JSON result cannot be mapped:
panic: json: cannot unmarshal number into Go struct field MetricItem.metrics of type map[string]interface {}

Related issues/PRs
Suggest a fix/enhancement

I would have to dive in, I assume that the problem lies in org.openapitools.codegen.languages. AbstractGoCodegen as it "overmatches" the object type. See typeMapping.put("object", "map[string]interface{}");

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 org.openapitools.codegen.languages.AbstractGoCodegen and its typeMapping entry for object, using the supplied OpenAPI declaration and generation command to reproduce the issue. Confirm that the nested anyOf array generates Metrics as map[string][][]interface{} and that the resulting Go client can unmarshal numeric JSON results without the reported panic.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.