-
Notifications
You must be signed in to change notification settings - Fork 6k
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
[Go] optional boolean properties set to false are omitted when encoded #7391
[Go] optional boolean properties set to false are omitted when encoded #7391
Comments
@drennalls Can you please try the latest stable version (2.3.0), which has the Go API client refactored? |
This happens in version Got And add: ...
vaccinated:
type: boolean
description: indicate if pet is vaccinated
... To the Pet object definition. Generate the client: $ ./run-in-docker.sh generate -i issue7391.yaml -l go -o /gen/out/issue7391 -DpackageName=issue7391
$ ln -s ${PWD}/out $(go env GOPATH)/src/out Create a dummy server: from flask import Flask, request
app = Flask(__name__)
@app.route('/pet', methods=['POST'])
def root():
content = request.json
print content
return "all right"
app.run(port=8080) Run a Go test: package main
import (
"fmt"
"out/issue7391"
"testing"
)
func TestPost(t *testing.T) {
config := issue7391.NewConfiguration()
config.BasePath = "http://localhost:8080"
client := issue7391.NewAPIClient(config)
pet := issue7391.Pet{
Name: "john",
// Vaccinated: false,
}
res, err := client.PetApi.AddPet(nil, pet)
fmt.Printf("%+v\n%+v\n", res, err)
} Running with or without commenting on that line, the server logs:
Manually changing the ...
Vaccinated bool `json:"vaccinated,omitempty"`
... to ...
Vaccinated bool `json:"vaccinated"`
... Again, running with or without commenting the
So removing ...
Vaccinated *bool `json:"vaccinated"`
... Actualy works as intended. Maybe it's an option? /cc @drennalls @wing328 |
Sure: $ go version
go version go1.9.3 linux/amd64 |
@drennalls hello, setting default value should help
|
I seem to be running into this issue as well. Providing a default as @tenmozes suggested yields
which does work. However, my question is whether we should also consider using this style even when a default is NOT provided? Personally, I can't think of an instance where the existing
would be preferred over the pointer version that is generated when you provide a default. What do y'all think / can you think of one such scenario? In fact, wouldn't we run into this same issue with all scalar types (e.g., int, string)? Under the current scheme there is no way to pass 0 or an empty string unless you specify a default. |
It seems the fix by setting a default outlined in #7391 (comment) does not work for fields of type No defaultThe following: latitude:
type: number
description: the latitude in decimal format leads to Latitude float32 `json:"latitude,omitempty"` as expected. Default != 0Setting a default other than 0 will lead to the expecting pointer construct, such as: latitude:
type: number
default: 5
description: the latitude in decimal format leads to Latitude *float32 `json:"latitude,omitempty"` as expected after knowing this fixes it for Default == 0However, when the default is 0 (which is the default value of a latitude:
type: number
default: 0
description: the latitude in decimal format leads to Latitude float32 `json:"latitude,omitempty"` So, fixing this by setting a default, does not work if the default is 0. |
Fixes a bug where auto acquisition of networks was not transmitted properly due to an issue in swagger codegen: swagger-api/swagger-codegen#7391
I'm testing with version CLI |
I am also seeing the bool issue - on Golang 1.12.6 and go-swagger latest (c49ea4ca2). The fix does seem to be making the struct member a pointer in the model - which as suggested, a default value does - and also making the property required, eg :
|
any updates? |
Swagger clients will omit the value due to a bug: swagger-api/swagger-codegen#7391
This is a dirty workaround for the problem with Golang empty values and marshaling to JSON. The problem was described here: https://willnorris.com/2014/05/go-rest-apis-and-pointers/ In this library all struct's fields should be pointers, not simple types like `bool` or `string` because we can't say whether a value is empty and we should omit it (`omitempty` field tag) or not. This library is auto-generated by swagger codegen, and developers created an issue for this problem: swagger-api/swagger-codegen#7391 In this pull request we dirty fix our problem with only one field we want to update (set value to false)
Any updates? |
@vvalchev Can you help please? I tried but couldn't figure it out. Can you help me make boolean type not omitempty and be always a pointer type. |
@wkgcass Or anyone out there for solutions, I used -t (template directory) while generating for golang. And under model.mustache, I specified
Notice |
Description
If an object defines an optional boolean property it does not end up in the JSON if the value is
false
Swagger-codegen version
2.2.2
Swagger declaration file content or url
Command line used for generation
java -jar target/swagger-codegen-cli.jar generate -i ./mypetstore.yaml -l go -o ./mypetstore-client-go
Steps to reproduce
Include an optional boolean property (like
vaccinated
in the API above), set to false and encode it. The field will be missing from the JSON.Related issues/PRs
If the property is marked as
required
then things work as expected which I guess was a result of #4665Suggest a fix/enhancement
Don't use
omitempty
for boolean properties, even if optional...from https://golang.org/pkg/encoding/json/
..if you do then sending a
false
for optional properties is not possible.The text was updated successfully, but these errors were encountered: