Skip to content

[JSONPB] Invalid conversion between json and protbuf #347

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

Closed
nlamirault opened this issue May 11, 2017 · 4 comments
Closed

[JSONPB] Invalid conversion between json and protbuf #347

nlamirault opened this issue May 11, 2017 · 4 comments

Comments

@nlamirault
Copy link

nlamirault commented May 11, 2017

Hi,
i've got an error to convert JSON to Protobuf :

The proto file :

message VirtualMachine {
  string name = 1;
  int32 memory = 2;
  int32 num_cpu = 3;
  string powerState = 4; 
  string host = 5;
  repeated Disk disks = 6;
  repeated CloudNetwork nics = 7;
}
import (
	"testing"
)

var (
          // defaultMarshaler = &runtime.JSONPb{OrigName: true}
         defaultMarshaler = &runtime.JSONPb{OrigName: false}

	jsonVirtualMachines = []byte(`[{"description": "42701226-f45b-11e6-80e6-0242ac110002", "num_cpu": 4,  "name": "iop-ut00-itg", "powerState": "poweredOff"}]`)
)

func Test_DecodingListVirtualMachines(t *testing.T) {
	var virtualmachines []*pb.VirtualMachine
	if err := defaultMarshaler.Unmarshal(jsonVirtualMachines, &virtualmachines); err != nil {
		t.Fatalf(err.Error())
	}
	for _, virtualmachine := range virtualmachines {
		if len(virtualmachine.PowerState) == 0 {
			t.Fatalf("Invalid power state: %s", virtualmachine)
		}
	}
}

The test :

$ go test -run Test_DecodingListVirtualMachines\$ .
--- FAIL: Test_DecodingListVirtualMachines (0.00s)
        virtualmachine_test.go:30: Invalid power state: &pb.VirtualMachine{Name:"iop-ut00-itg", Memory:0, NumCpu:4, PowerState:"", Host:"", Disks:[]*pb.Disk(nil), Nics:[]*pb.CloudNetwork(nil)}
FAIL                                            
FAIL    gitlab.sys.op.mbs/nimbus/diablo/api     0.004s

Any idea why "PowerState" is not set ?
Thanks

@nlamirault
Copy link
Author

I try to add

message VirtualMachine {
  string name = 1;
  int32 memory = 2;
  int32 num_cpu = 3;
  string power_state = 4 [json_name="powerState"];
  string host = 5;
  repeated Disk disks = 6;
  repeated CloudNetwork nics = 7;
}

Generated struct is :

type VirtualMachine struct {
        Name       string          `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
        Memory     int32           `protobuf:"varint,2,opt,name=memory" json:"memory,omitempty"`
        NumCpu     int32           `protobuf:"varint,3,opt,name=num_cpu,json=numCpu" json:"num_cpu,omitempty"`
        PowerState string          `protobuf:"bytes,4,opt,name=power_state,json=powerState" json:"power_state,omitempty"`
        Host       string          `protobuf:"bytes,5,opt,name=host" json:"host,omitempty"`
        Disks      []*Disk         `protobuf:"bytes,6,rep,name=disks" json:"disks,omitempty"`
        Nics       []*CloudNetwork `protobuf:"bytes,7,rep,name=nics" json:"nics,omitempty"`
}

Same error with the unit test. If i manually change the struct to that it works :

type VirtualMachine struct {
        Name       string          `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
        Memory     int32           `protobuf:"varint,2,opt,name=memory" json:"memory,omitempty"`
        NumCpu     int32           `protobuf:"varint,3,opt,name=num_cpu,json=numCpu" json:"num_cpu,omitempty"`
        PowerState string          `protobuf:"bytes,4,opt,name=power_state,json=powerState" json:"powerState,omitempty"`
        Host       string          `protobuf:"bytes,5,opt,name=host" json:"host,omitempty"`
        Disks      []*Disk         `protobuf:"bytes,6,rep,name=disks" json:"disks,omitempty"`
        Nics       []*CloudNetwork `protobuf:"bytes,7,rep,name=nics" json:"nics,omitempty"`
}

Perhaps related to #256

@dsnet
Copy link
Member

dsnet commented May 18, 2017

Hi, I'm trying to understand your test code.

You call defaultMarshaler.Unmarshal, where defaultMarshaler = &runtime.JSONPb{OrigName: false}. What is runtime.JSONPb?

@dsnet
Copy link
Member

dsnet commented May 18, 2017

So I looked into this further and I'm going to close this as a issue in grpc-gateway and not golang.org/protobuf/jsonpb.

The logic in grpc-gateway/runtime checks if the object satisfies the proto.Message interface) and if it doesn't, then it skips using the jsonpb unmarshaller and relies on it's own homebrewed JSON decoder in the decodeNonProtoField function.

You are hitting this case because you are trying to unmarshal into a []*pb.VirtualMachine, which won't satisfy the proto.Message interface.

Feel free to file a bug against gRPC and reference this issue.

@dsnet dsnet closed this as completed May 18, 2017
@nlamirault
Copy link
Author

OK. Thanks for your help.

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

No branches or pull requests

2 participants