Openapi-generator: [BUG] [Go] object nullable not working

Created on 11 Feb 2019  路  13Comments  路  Source: OpenAPITools/openapi-generator

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
Go Bug

Most helpful comment

Ah 馃挕 It is due to InlineModelResolver. (The issue doesn't caused by the Go generator. All generator faces the issue)

https://github.com/OpenAPITools/openapi-generator/blob/f76dca84f542fe5d9354501f7f02142238fa562a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java#L888

InlineModelResolver#modelFromProperty() (which is called from its flatten()) resolves inline model user.address to UserAddress but the resolved UserAddress model has no nullable value. 馃槩

All 13 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Hi @hatappi ,

nullable support for the Go generator has been merged into master. please give it a try with latest docker image. 馃槈

openapi: "3.0.6"
info:
  version: "1.0"
  title: test

Btw, I don't think OAS 3.0.6 has released yet. The latest is "3.0.2": https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md

@ackintosh @wing328
Thank you for reply.
I tried latest docker image. Image Id is e0c35c5a73f5 and openapi version is 3.0.2
But the pointer did not attach to a struct.

test.yaml

openapi: "3.0.2"
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

command

docker run --rm \
  -v ${PWD}:/local \
  openapitools/openapi-generator-cli \
  generate \
  -i /local/test.yaml \
  -g go \
  -o /local/pkg

generated model

/*
 * test
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * API version: 1.0
 * Generated by: OpenAPI Generator (https://openapi-generator.tech)
 */

package openapi

type User struct {
    Name *string `json:"name,omitempty"`
    Age *int32 `json:"age,omitempty"`
    Address UserAddress `json:"address,omitempty"`
}

Thank you very much for reporting issue!
I am sorry that confirmation with Issue #1869 was insufficient.
I will investigate.

Hmm..
In the fromProperty method of the DefaultCodegen.java, p.getNullable() returns false for the address schema. this link

@wing328 @ackintosh
Sorry, could you please give me any advice?

Thanks @kemokemo . Let me check 馃憖

Ah 馃挕 It is due to InlineModelResolver. (The issue doesn't caused by the Go generator. All generator faces the issue)

https://github.com/OpenAPITools/openapi-generator/blob/f76dca84f542fe5d9354501f7f02142238fa562a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java#L888

InlineModelResolver#modelFromProperty() (which is called from its flatten()) resolves inline model user.address to UserAddress but the resolved UserAddress model has no nullable value. 馃槩

I'll file a PR to fix the missing nullable. 馃槈

@hatappi The fix has been merged into master but the docker image latest has not updated yet. please wait a moment. 馃槍

@ackintosh
Thank you 馃槉
I'm looking forward to it.

The docker image has been updated. I've verified with the image that the UserAddress is generated as pointer.

type User struct {
        Name *string `json:"name,omitempty"`
        Age *int32 `json:"age,omitempty"`
-        Address UserAddress `json:"address,omitempty"`
+        Address *UserAddress `json:"address,omitempty"`
}

Again thanks for reporting this issue. 馃憤

@ackintosh
I also confirmed that it was fixed by 246d3a3f904a docker image 馃帀
Thank you for fixed.

Was this page helpful?
0 / 5 - 0 ratings