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

JSON mismatch between server generated JSON and client side JSON when using wrappers. #1312

Open
GregHattJr opened this issue Oct 24, 2019 · 0 comments

Comments

@GregHattJr
Copy link

First off, I apologize if this problem has been raised in another issue. I looked over them and couldn't find one that directory talked about a work around for this problem. The closest I found was #677 . But I don't understand how to apply the suggestion of wrappers from OUR code (not protobufJS's source code).

So we use "wrapper" values such as google.protobuf.StringValue and google.protobuf.DoubleValue to handle distinguishing between null and default. We are facing two problems related to creating and sending JSON from messages that contain these wrapped values.

Given a message with the following structure:

message Alarm
StringValue name = 1;
DoubleValue durationInSeconds = 2;
StringValue description = 3;

In our server code we transmit this message as JSON on our endpoints in Python. When we create JSON from protobuf instances on the server using MessageToDict or MessageToJSON we end up with the following:

{
    name: 'foo',
    durationInSeconds: 34,
    description: 'boiler temperature exceeds limit'
}

In the UI we have the same model compiled with ProtobufJS/ProtobufTS. There are two problems we're having.

The first is that we're not able to take this JSON payload and turn it back into an Alarm in the UI since the JSON structure doesn't have wrapped values. Essentially there is a mismatch between the JSON created from a message and the JSON needed to create a message.
At the very least I'd like to be able to use fromObject / toObject to consume the JSON to make a new Alarm or to produce JSON from an existing Alarm.

This issue describes what I'm talking about and someone has made a PR to allow us to write wrappers for the wrapped values (#677 (comment)). What I don't understand where do I include this wrapper code? Basically in our project we have a .proto file and we compile it with protobufJS.
Where do I define these wrappers and how can I include them when I compile our .proto files?

The second problem is there is no interface available in the UI TS files to describe the JSON structure. The JavaScript interface of an Alarm for instance doesn't need wrapped values. So if we want to use the JSON structure that we get from the server we have to manually create an interface for each of our messages that matches the JSON version of message.
I understand I could just use the Protobuf interface which has wrappers rather than primitives but it means dealing with the extra .value everywhere and not having an interface to describe the actual JSON data that will get sent back to the server for updates.

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

No branches or pull requests

1 participant