Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12955
Closed
Open
Issue created Jul 20, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][Go] anyOf with objects and arrays generates uncompilable model

Created by: MarcelGosselin

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

When a schema has anyOf whose entries are either a plain object or an array, the generated code contains invalid field names in the model.

Actual output:

type Any struct {
	[]Any *[]Any
	map[string]interface{} *map[string]interface{}
}

Expected output (not necessarily an exact match):

type Any struct {
	arrayOfAny *[]Any
	plainObject *map[string]interface{}
}
openapi-generator version

6.0.1

OpenAPI declaration file content or url

OpenAPI with objects and arrays

Generation Details

Running the following without config file, from a folder containing only the file openapi.json

docker run --rm -it -v $(pwd):/src openapitools/openapi-generator-cli:v6.0.1 generate -i /src/openapi.json -g go -o /src
Steps to reproduce
  1. Generate using above command line
  2. Look at file model_any.go, it contains invalid property names []Any and map[string]interface{}
    type Any struct {
        []Any *[]Any
        bool *bool
        float32 *float32
        int32 *int32
        map[string]interface{} *map[string]interface{}
        string *string
    }
Related issues/PRs
  1. Similar one with oneOf
  2. More far fetched one with names starting with numbers
Suggest a fix

The file modules/openapi-generator/src/main/resources/go/model_anyof.mustache uses #anyOf which is a Set<string> in CodegenModel.java. Unfortunately in that case the string is a datatype that cannot be used as a field name. I can see 3 ways to fix this (my favorite is the third one):

  1. (I don't know if it is possible in Mustache) In the mustache template, instead of
    {{#anyOf}}
    {{{.}}} *{{{.}}}
    {{/anyOf}}
    you could have something to normalize the identifier
    {{#anyOf}}
    {{{ toIdentifier(.) }}} *{{{.}}}
    {{/anyOf}}
  2. Change the data type of the Set of CodegenModel.anyOf to be a class which contains 2 properties: DataType, IdentifierName. Then modify usages of anyOf in model_anyof.mustache and any other places to use the right property.
  3. Use type definitions in Go anytime you encounter an array or object, something like the following.
    type openApiObject map[string]interface{}
    type arrayOfAny []Any
    You could then use that typedef in the Set<string> which would make the generation give this instead
    type Any struct {
        arrayOfAny *arrayOfAny
        bool *bool
        float32 *float32
        int32 *int32
        openApiObject *openApiObject
        string *string
    }
Assignee
Assign to
Time tracking