OpenAPITools / OpenAPITools/openapi-generator

[BUG][Go] Wrong code generation for "Any Type"

Open
#15,281 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 (example)?
  • 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

I have an Any Type declaration in my openapi file and the code generator produces an unexpected output as it generates code for an object type.

To represent an Any Type in Go, one would use interface{}, instead of map[string]interface{} which is used to represent an Object Type.

However, the currently generated code is map[string]interface{}.

openapi-generator version

latest and v6.5.0

OpenAPI declaration file content
swagger: "2.0"
info: 
  title: "Example with a list of any type"
  version: 0.0.1

paths: 
  /example: 
    get: 
      responses: 
        "200":
          description: "Returns a list of any type"
          schema: 
            $ref: '#/definitions/ContainsListOfAnyType'

definitions:
  ContainsListOfAnyType:
    type: object
    properties:
      values:
        type: array
        items:
          {}
Steps to reproduce
  • Store the given yml file as as example.yml
  • Run docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/example.yml -g go -o /local/out/go

using grep -A 2 'type ContainsListOfAnyType struct {' out/go/model_contains_list_of_any_type.go, one can see that the generated code for ContainsListOfAnyType is

type ContainsListOfAnyType struct {
	Values []map[string]interface{} `json:"values,omitempty"`
}

whereas the expected output would be

type ContainsListOfAnyType struct {
	Values []interface{} `json:"values,omitempty"`
}

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

Reproduce the issue with example.yml using the Docker generate command, then inspect out/go/model_contains_list_of_any_type.go and trace how the Go generator maps the empty array-item schema. Done means the generated Values field uses []interface{} for Any Type while object schemas continue to use map[string]interface{}.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.