Skip to content

[BUG] [Go] oneOf generates uncompilable code for primitive types #8489

Closed
@aeneasr

Description

@aeneasr

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

The go generator creates uncompilable Go code when using oneOf with base JSON types (string, number, ...)

openapi-generator version

5.0.0 and current 5.0.1-SNAPSHOT

OpenAPI declaration file content or url

Subpath is #/components/schemas/

      "uiNodeInputAttributes": {
        "description": "InputAttributes represents the attributes of an input node",
        "properties": {
          "disabled": {
            "description": "Sets the input's disabled field to true or false.",
            "type": "boolean"
          },
          "label": {
            "$ref": "#/components/schemas/uiText"
          },
          "name": {
            "description": "The input's element name.",
            "type": "string"
          },
          "pattern": {
            "description": "The input's pattern.",
            "type": "string"
          },
          "required": {
            "description": "Mark this input field as required.",
            "type": "boolean"
          },
          "type": {
            "$ref": "#/components/schemas/uiNodeInputAttributeType"
          },
          "value": {
            "description": "The input's value.",
            "nullable": true,
            "oneOf": [
              {
                "type": "string"
              },
              {
                "type": "number"
              },
              {
                "type": "boolean"
              }
            ]
          }
        },
        "required": [
          "name",
          "type",
          "disabled"
        ],
        "type": "object"
      },

(for JSON code), so it becomes more readable. If it is longer than about ten lines,
please create a Gist (https://gist.github.com) or upload it somewhere else and
link it here.
-->

Generation Details

The generated code is:

// UiNodeInputAttributes InputAttributes represents the attributes of an input node
type UiNodeInputAttributes struct {
	// Sets the input's disabled field to true or false.
	Disabled bool `json:"disabled"`
	Label *UiText `json:"label,omitempty"`
	// The input's element name.
	Name string `json:"name"`
	// The input's pattern.
	Pattern *string `json:"pattern,omitempty"`
	// Mark this input field as required.
	Required *bool `json:"required,omitempty"`
	Type string `json:"type"`
	// The input's value.
	Value NullableOneOfstringnumberboolean `json:"value,omitempty"` // This type is unknown and does not compile
}

Similar problems occur if nullable is false or if we use refs with the same types instead. Here is another example:

	Value *OneOfstringnumberboolean `json:"value,omitempty"`
Steps to reproduce

Happy to provide a reproducible test case if required. Code is generated as such:

npm run openapi-generator-cli -- generate -i ".schema/api.openapi.json" \
				-g go \
				-o "internal/httpclient" \
				--git-user-id ory \
				--git-repo-id kratos-client-go \
				--git-host github.com \
				-c .schema/openapi/gen.go.yml

gen.go.yml config is:

disallowAdditionalPropertiesIfNotPresent: true
packageName: kratos
generateInterfaces: false
isGoSubmodule: false
structPrefix: true
Related issues/PRs

Couldn't find any.

Suggest a fix

If oneOf can't be properly determined, just use interface{}.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions