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

jsonpb Marshal and Unmarshal should return error on missing required field #440

Closed
mkevac opened this issue Oct 11, 2017 · 7 comments
Closed
Labels

Comments

@mkevac
Copy link

mkevac commented Oct 11, 2017

If I have a message in *.proto file with required field and I try to convert json representation of this message to message itself, I would expect my Unmarshaller to check for required fields and to give an error if required fields are not in json representation. But jsonpb does not check that.

package main

import (
	gsp "lala/lala/proto"
	"log"
	"github.com/golang/protobuf/jsonpb"
)

func main() {
	jsonMsg := "{\"age_from\": 18}"
	msg := gsp.RequestSearch{}
	if err := jsonpb.UnmarshalString(jsonMsg, &msg); err != nil {
		log.Printf("error unmarshalling json: %s", err)
	}
	log.Println(msg)
}
type RequestSearch struct {
	UserId                *uint32     `protobuf:"varint,1,req,name=user_id" json:"user_id,omitempty"`
	AgeFrom               *uint32     `protobuf:"varint,2,opt,name=age_from" json:"age_from,omitempty"`
	AgeTo                 *uint32     `protobuf:"varint,3,opt,name=age_to" json:"age_to,omitempty"`
	Gender                *UserGender `protobuf:"varint,4,opt,name=gender,enum=badoo.gophersearch.UserGender" json:"gender,omitempty"`
	Lon                   *float32    `protobuf:"fixed32,5,opt,name=lon" json:"lon,omitempty"`
	Lat                   *float32    `protobuf:"fixed32,6,opt,name=lat" json:"lat,omitempty"`
	ExcludeList           []uint32    `protobuf:"fixed32,7,rep,name=exclude_list" json:"exclude_list,omitempty"`
	MainVectorNeeded      *uint32     `protobuf:"varint,8,opt,name=main_vector_needed" json:"main_vector_needed,omitempty"`
	BeautifulVectorNeeded *uint32     `protobuf:"varint,9,opt,name=beautiful_vector_needed" json:"beautiful_vector_needed,omitempty"`
	SearchDistance        *uint32     `protobuf:"varint,10,opt,name=search_distance" json:"search_distance,omitempty"`
	XXX_unrecognized      []byte      `json:"-"`
}
@dsnet
Copy link
Member

dsnet commented Oct 11, 2017

\cc @cybrcodr

@dsnet dsnet added the jsonpb label Dec 6, 2017
@cybrcodr
Copy link
Contributor

cybrcodr commented Dec 8, 2017

JSON serialization is only described in proto3 page --
https://developers.google.com/protocol-buffers/docs/proto3#json

Sadly a "required" field does not apply in proto3. If we do change the unmarshaler to respect required for proto2, it may break current users. We can add an option to check for required fields instead.

@mkevac
Copy link
Author

mkevac commented Dec 8, 2017

@cybrcodr, how would it break current users?

@cybrcodr
Copy link
Contributor

cybrcodr commented Dec 8, 2017

If an existing application is relying on the current behavior of not returning error on missing required fields, and it switches to returning an error, then it may break that app.

Anyways, I think chances are probably small though I could be totally wrong on this. I don't have the bandwidth to work on this, but am open to review a PR.

@cybrcodr
Copy link
Contributor

Btw, for consistency, if we do change this behavior to return an error value if required field is not in set on Unmarshal, we should also change Marshal to return error if required field is not set before Marshal'ing.

@mkevac
Copy link
Author

mkevac commented Dec 12, 2017

Sure. And that is how it is supposed to be IMO :-\

@cybrcodr cybrcodr changed the title jsonpb UnmarshalString doesn't check for required fields jsonpb Marshal and Unmarshal should return error on missing required field Dec 13, 2017
@dsnet dsnet added the bug label Dec 23, 2017
@cybrcodr
Copy link
Contributor

cybrcodr commented Jan 5, 2018

Fixed in #472

@cybrcodr cybrcodr closed this as completed Jan 5, 2018
@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
Projects
None yet
Development

No branches or pull requests

3 participants