Skip to content

[BUG] [Go] object nullable not working #2119

Closed
@hatappi

Description

@hatappi
Description

output json using Go struct.
When there is no value in strcut, it may not be displayed in json.
In such a case I will use pointer.

However, if specify nullable: true for the object in the definition file, it will not be output with a pointer.
For integer and string pointers are given.

openapi-generator version
  • 3.3.4
  • 4.0.0-SNAPSHOT
OpenAPI declaration file content or url

test.yaml

openapi: "3.0.6"
info:
  version: "1.0"
  title: test
paths:
  /test:
    get:
      summary: test
      responses:
        '200':
          description: OK
components:
  schemas:
    user:
      type: object
      properties:
        name:
          type: string
          nullable: true
        age:
          type: integer
          nullable: true
        address:
          type: object
          nullable: true
          properties:
            city:
              type: string

model.mustache

{{>partial_header}}
package {{packageName}}
{{#models}}
{{#imports}}
{{#-first}}
import (
{{/-first}}
	"{{import}}"
{{#-last}}
)
{{/-last}}
{{/imports}}
{{#model}}
{{#isEnum}}
{{#description}}
// {{{classname}}} : {{{description}}}
{{/description}}
type {{{classname}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{/format}}

// List of {{{name}}}
const (
	{{#allowableValues}}
	{{#enumVars}}
	{{^-first}}
	{{/-first}}
	{{{classname}}}{{name}} {{{classname}}} = "{{{value}}}"
	{{/enumVars}}
	{{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}}
// {{{description}}}{{/description}}
type {{classname}} struct {
{{#vars}}
{{^-first}}
{{/-first}}
{{#description}}
	// {{{description}}}
{{/description}}
	{{name}} {{#isNullable}}*{{/isNullable}}{{dataType}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{/vars}}
}
{{/isEnum}}
{{/model}}
{{/models}}
Command line used for generation
docker run --rm \
  -v ${PWD}:/local \
  openapitools/openapi-generator-cli:v3.3.4 \
  generate \
  -i /local/test.yaml \
  -g go \
  -o /local/pkg \
  -t /local/templates
Steps to reproduce
  1. Prepare templates/model.mustache
  2. Save OpenAPI declaration file with test.yaml
  3. run command
  4. check pkg/model_user.go

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