abujehad139 / google-api-go-client

Automatically exported from code.google.com/p/google-api-go-client
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

bool fields should not have omitempty tag #61

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Checkout Genomics API client
2. Realize that you can't issue Datasets.Create request to create a private 
project (IsPublic == false)

This is due to the fact that the generated code for this field look the 
following:

// IsPublic: Flag indicating whether or not a dataset is publicly               

// viewable. If a dataset is not public, it inherits viewing permissions        

// from its project.                                                            

IsPublic bool `json:"isPublic,omitempty"`

If IsPublic == false, json will not have this field and the server will 
complain:

googleapi: Error 400: Missing required argument: Dataset.IsPublic 

This check is done for a purpose. Creating a dataset and specifying an ACL is 
too important to allow default values. And omitempty tag stands on the way 
between the server and the goal to create a private dataset.

Is there a positive outcome from omitempty, if the type is bool? I understand 
its value for strings, maps, arrays or struct pointers. It's less obvious for 
integer or float types. It seems completely irrelevant for bools.

Is it fine to drop omitempty from booleans? I am ready to produce a CL for that.

Original issue reported on code.google.com by krasin@google.com on 17 Jun 2014 at 6:58

GoogleCodeExporter commented 9 years ago
After thinking a bit, I am not entirely sure that it's a valid request. Or, 
likely, it's too narrow.

Unlike protobuf library, the Go API client library does not distinguish between 
zero value and not set value. There would always be APIs which could not be 
properly accessed due to this issue.

I don't say it's impossible to design an API which will always obey "not set 
value == default value", but the existing APIs are not like that.

Original comment by krasin@google.com on 17 Jun 2014 at 6:47

GoogleCodeExporter commented 9 years ago
(feel free to close this issue with Working-As-Intended).

Original comment by krasin@google.com on 17 Jun 2014 at 6:47

GoogleCodeExporter commented 9 years ago
The same issue exists with the Content API for Shopping. For an AccountsUser, 
the admin field is a required boolean, so you can't make non-admin users with 
the Go library.

Better than removing the omitempty would be having an omitnil, but sadly that 
doesn't exist.

Original comment by msca...@google.com on 11 Aug 2014 at 9:02

GoogleCodeExporter commented 9 years ago
Same things happens with Cloud Datastore, if I post an entity like this:

entity := &datastore.Entity{
        Key: key,
        Properties: map[string]datastore.Property{
            "username": datastore.Property{StringValue: "someuser"},
            "favorite": datastore.Property{BooleanValue: false},
        },
    }

I get this error in response: "googleapi: Error 400: Property \"favorite\" has 
no value., INVALID_ARGUMENT"

So I can only store `true` values in Datastore. 

Original comment by treeder on 21 Nov 2014 at 1:17