Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hatappi opened this issue Feb 11, 2019 · 13 comments · Fixed by #2189
Closed

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

hatappi opened this issue Feb 11, 2019 · 13 comments · Fixed by #2189

Comments

@hatappi
Copy link

hatappi commented Feb 11, 2019

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
@auto-labeler
Copy link

auto-labeler bot commented Feb 11, 2019

👍 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.

@ackintosh
Copy link
Contributor

Hi @hatappi ,

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

@wing328
Copy link
Member

wing328 commented Feb 17, 2019

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

@hatappi
Copy link
Author

hatappi commented Feb 17, 2019

@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"`
}

@kemokemo
Copy link
Contributor

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

@kemokemo
Copy link
Contributor

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?

@ackintosh
Copy link
Contributor

Thanks @kemokemo . Let me check 👀

@ackintosh
Copy link
Contributor

ackintosh commented Feb 19, 2019

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

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

@ackintosh
Copy link
Contributor

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

@ackintosh
Copy link
Contributor

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

@hatappi
Copy link
Author

hatappi commented Feb 20, 2019

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

@ackintosh
Copy link
Contributor

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. 👍

@hatappi
Copy link
Author

hatappi commented Feb 21, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants