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

fromJSON doesn't work for Well-Known Types. #336

Closed
CatsMiaow opened this issue Jul 21, 2021 · 2 comments · Fixed by #396
Closed

fromJSON doesn't work for Well-Known Types. #336

CatsMiaow opened this issue Jul 21, 2021 · 2 comments · Fixed by #396
Labels
bug Something isn't working help wanted Extra attention is needed released

Comments

@CatsMiaow
Copy link

CatsMiaow commented Jul 21, 2021

import "google/protobuf/struct.proto";

message HelloRequest {
  string name = 1;

  google.protobuf.Struct param_struct = 2;
  google.protobuf.ListValue param_list_value = 3;
  google.protobuf.Value param_value = 4;
}

When using a WellKnownType of google-protobuf, can pass a value from the client to fromJavaScript as shown below.

import { ListValue, Struct, Value } from 'google-protobuf/google/protobuf/struct_pb';

Struct.fromJavaScript({ foo: 'bar', bar: 'foo' });
ListValue.fromJavaScript([{ foo: 'bar' }, { bar: 'foo' }]);
Value.fromJavaScript('Any Value');

Reference

In ts-proto, the code below doesn't pass values properly or throws an error.
The struct_pb in google-protobuf are not compatible because they have different types.

import { ListValue, Struct, Value } from "./google/protobuf/struct";

Struct.fromJSON({ foo: 'bar', bar: 'foo' });
ListValue.fromJSON([{ foo: 'bar' }, { bar: 'foo' }]);
Value.fromJSON('Any Value'); // Value.fromJSON({ stringValue: 'Any Value' }); This works.

https://github.com/stephenh/ts-proto#highlights

fromJSON/toJSON support the canonical Protobuf JS format (i.e. timestamps are ISO strings)

https://developers.google.com/protocol-buffers/docs/proto3#json

proto3 JSON JSON example Notes
Struct object { … } Any JSON object.
ListValue array [foo, bar, …]
Value value Any JSON value.

Depending on proto3 json mapping, it looks like fromJSON will also work like fromJavaScript or a separate method needs to be added.

@stephenh
Copy link
Owner

@CatsMiaow yeah, you're right that Struct, ListValue, and Value aren't specifically handled by ts-proto, but should be.

@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 1.88.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants