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

Update jsonpb to handle Map, Slice, Ptr nil values for EmitDefaults #338

Merged
merged 1 commit into from
May 23, 2017
Merged

Update jsonpb to handle Map, Slice, Ptr nil values for EmitDefaults #338

merged 1 commit into from
May 23, 2017

Conversation

ewang
Copy link
Contributor

@ewang ewang commented Apr 13, 2017

My attempt at making the Map, Slice, nil Ptr type marshaling match the proto3 JSON spec.

Fixes: #323

Related PRs:
#232
#287

Copy link
Contributor

@cybrcodr cybrcodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed review. I just got assigned to start looking at these. Changes look good. Please to sync to head first and I'll approve.

Thanks.

@ewang
Copy link
Contributor Author

ewang commented May 23, 2017

@cybrcodr Rebased

Copy link
Contributor

@cybrcodr cybrcodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@cybrcodr cybrcodr merged commit 7a211bc into golang:master May 23, 2017
@tamird
Copy link
Contributor

tamird commented Aug 5, 2017

@cybrcodr @ewang this commit seems to have the unfortunate effect of (*string)(nil) becoming &"" when marshalled and unmarshalled. Specifically, we have this proto:

syntax = "proto2";
package cockroach.build;
option go_package = "build";

import "gogoproto/gogo.proto";

message Info {
  optional string go_version = 1 [(gogoproto.nullable) = false];
  optional string tag = 2 [(gogoproto.nullable) = false];
  optional string time = 3 [(gogoproto.nullable) = false];
  optional string revision = 4 [(gogoproto.nullable) = false];
  optional string cgo_compiler = 5 [(gogoproto.nullable) = false];
  optional string platform = 6 [(gogoproto.nullable) = false];
  optional string distribution = 7 [(gogoproto.nullable) = false];
  optional string type = 8 [(gogoproto.nullable) = false];

  optional string dependencies = 10000;
}

Here's my test case:

func TestProtobuf(t *testing.T) {
	var b bytes.Buffer

	var in build.Info
	if err := (&jsonpb.Marshaler{EmitDefaults: true}).Marshal(&b, &in); err != nil {
		t.Fatal(err)
	}
	var out build.Info
	if err := jsonpb.Unmarshal(&b, &out); err != nil {
		t.Fatal(err)
	}
	if out != in {
		t.Error(pretty.Diff(out, in))
	}
}

and it fails with

--- FAIL: TestProtobuf (0.00s)
	status_test.go:140: [Dependencies: &"" != nil]

@cybrcodr
Copy link
Contributor

cybrcodr commented Aug 5, 2017

Can you try out head version? In #394, deserializing "null" for all message types except wkt ValueType should translate to nil value.

I also tried it out with a simplified example from yours.

syntax = "proto2";
option go_package = "optstring/pb";

message Info {
  optional string dependencies = 10000;
}
package main

import (
        "bytes"
        "fmt"
        "log"

        "optstring/pb"

        "github.com/golang/protobuf/jsonpb"
)

func main() {
        b := &bytes.Buffer{}
        p1 := &pb.Info{}
        fmt.Printf("before serializing %#+v\n", p1)
        m := jsonpb.Marshaler{EmitDefaults: true}
        if err := m.Marshal(b, p1); err != nil {
                log.Fatal(err)
        }
        fmt.Printf("serialized json: %s\n", b.String())

        p2 := &pb.Info{}
        if err := jsonpb.Unmarshal(b, p2); err != nil {
                log.Fatal(err)
        }
        fmt.Printf("deserialized into %#+v\n", p2)
        if p2.Dependencies == nil {
                fmt.Println("is nil!!!")
        }
}

Output...

before serializing &pb.Info{Dependencies:(*string)(nil), XXX_unrecognized:[]uint8(nil)}
serialized json: {"dependencies":null}
deserialized into &pb.Info{Dependencies:(*string)(nil), XXX_unrecognized:[]uint8(nil)}
is nil!!!

Let me know if head doesn't work out for you.

@tamird
Copy link
Contributor

tamird commented Aug 5, 2017

Thanks! We're using gogo/protobuf, so I've filed gogo/protobuf#319. I'll give it a shot once #394 is brought over.

@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[jsonpb] Do not marshal empty slices, maps and objects
4 participants