-
Notifications
You must be signed in to change notification settings - Fork 1.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
message with oneof cannot unmarshal from json string #446
Comments
I ran into this problem too. It's particularly frustrating because the marshaler produces json that the unmarshaler can't consume. https://gist.github.com/spraints/3cbd71e5f1ccbcd30dc1d266ec19ac78 is an example. |
@bh4rtp I just tried your example and it works for me on latest master or dev branches. Note that you have to use jsonpb. Here's what I have ...
Outputs:
|
Hi @cybrcodr, I'm having a similar issue, and I wonder if it's because the OneOf I'm marshalling with // The capabilities that the provisioned volume MUST have: the Plugin
// MUST provision a volume that could satisfy ALL of the
// capabilities specified in this list. The Plugin MUST assume that
// the CO MAY use the provisioned volume later with ANY of the
// capabilities specified in this list. This also enables the CO to do
// early validation: if ANY of the specified volume capabilities are
// not supported by the Plugin, the call SHALL fail. This field is
// REQUIRED.
VolumeCapabilities []*VolumeCapability `protobuf:"bytes,4,rep,name=volume_capabilities,json=volumeCapabilities" json:"volume_capabilities,omitempty"` // Specify a capability of a volume.
type VolumeCapability struct {
// Specifies what API the volume will be accessed using. One of the
// following fields MUST be specified.
//
// Types that are valid to be assigned to AccessType:
// *VolumeCapability_Block
// *VolumeCapability_Mount
AccessType isVolumeCapability_AccessType `protobuf_oneof:"access_type"`
// This is a REQUIRED field.
AccessMode *VolumeCapability_AccessMode `protobuf:"bytes,3,opt,name=access_mode,json=accessMode" json:"access_mode,omitempty"`
} // XXX_OneofFuncs is for the internal use of the proto package.
func (*VolumeCapability) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
return _VolumeCapability_OneofMarshaler, _VolumeCapability_OneofUnmarshaler, _VolumeCapability_OneofSizer, []interface{}{
(*VolumeCapability_Block)(nil),
(*VolumeCapability_Mount)(nil),
}
} Everything marshals correctly, but on unmarshal I get the unknown field warning. |
Sorry for the late response on this. Can you provide a simple reproducible case with the proto definition and similar Go code as I've posted that marshals and unmarshals? |
but why just marshaled json works, the native json can't unmarshaled however. How can I solve it? |
I'm not sure I fully understand your question. If you are asking how come using This can be approached differently to make it work with |
I'm sorry I mislead you. This is my problem. The proto message is like this:
Then my go file is like this:
And this is my test json string:
It looks well, the
but when my test json string Unmarshale to the struct, it will be like this:
And when I tried to use the
Yes, the |
Your posted example is not complete, i.e. there's no message definition for Site and App. So, it won't be easy for me to reproduce. However, instead of providing those, would you be able to trim down a reproducible test case that still fails? We do have unmarshaling testcases for oneofs here https://github.com/golang/protobuf/blob/master/jsonpb/jsonpb_test.go#L783-L787. So, oneofs should work in general unless there's a bug. |
hi, i am using protobuf oneof, i.e. union. the message can be marshaled to json. but it cannot be unmarshaled back.
1. message definition
2. message initialization
3. messsage output
name:"Allen" age:30 married:true f:14500.6
4. jsonpb marshal
{"name":"Allen","age":30,"married":true,"f":14500.6}
5. json marshal
{"name":"Allen","age":30,"married":true,"Number":{"F":14500.6}}
6. json & jsonpb unmarshal
{Allen 30 true <nil>}
as above line shows, the number.f is not unmarshaled. how to solve it?
The text was updated successfully, but these errors were encountered: