-
I have a proto message that utilizes anypb.Any type, the server successfully returns with 200, but connect client throws an error with Protobuf file: message Operation {
// The server-assigned name, which is only unique within the same service that
// originally returns it. If you use the default HTTP mapping, the
// `name` should be a resource name ending with `operations/{unique_id}`.
string name = 1;
// Service-specific metadata associated with the operation. It typically
// contains progress information and common metadata such as create time.
// Some services might not provide such metadata. Any method that returns a
// long-running operation should document the metadata type, if any.
google.protobuf.Any metadata = 2;
// If the value is `false`, it means the operation is still in progress.
// If `true`, the operation is completed, and either `error` or `response` is
// available.
bool done = 3;
oneof result {
// The error result of the operation in case of failure or cancellation.
google.rpc.Status error = 4;
// The normal response of the operation in case of success. If the original
// method returns no data on success, such as `Delete`, the response is
// `google.protobuf.Empty`. If the original method is standard
// `Get`/`Create`/`Update`, the response should be the resource. For other
// methods, the response should have the type `XxxResponse`, where `Xxx`
// is the original method name. For example, if the original method name
// is `TakeSnapshot()`, the inferred response type is
// `TakeSnapshotResponse`.
google.protobuf.Any response = 5;
}
} JSON response from server: {
"operations": [
{
"name": "operations/bkljffa7qzf5vb24s4t2y7bx5i",
"done": true,
"response": {
"@type": "type.googleapis.com/wpcbuf.gpcp.v1.SleepResponse"
}
},
{
"name": "operations/uzxd5xiftzf4hnz7nofboubloa",
"metadata": {
"@type": "type.googleapis.com/wpcbuf.gpcp.v1.SleepOperationMetadata",
"progress": 1,
"passed": 15
},
"done": true,
"response": {
"@type": "type.googleapis.com/wpcbuf.gpcp.v1.SleepResponse",
"seconds": 15
}
},
{
"name": "operations/fbbirfigyrghvbgufgzwjdnlwq",
"metadata": {
"@type": "type.googleapis.com/wpcbuf.gpcp.v1.SleepOperationMetadata",
"progress": 1,
"passed": 10
},
"done": true,
"response": {
"@type": "type.googleapis.com/wpcbuf.gpcp.v1.SleepResponse",
"seconds": 10
}
},
{
"name": "operations/u2mn65wdbbhyfnsoairelldsmu",
"metadata": {
"@type": "type.googleapis.com/wpcbuf.gpcp.v1.SleepOperationMetadata",
"progress": 0.4,
"passed": 4,
"remaining": 6
}
}
],
"totalSize": 4
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @coobeet, the message google.protobuf.Any is just string field for the type name and a bytes field for the data. If you parse an Any from binary, the packed message remains serialized until you unpack it. To unpack it, the concrete type information is required. In the JSON format, Any is is not just a slice of bytes with a name, but the actual fields of the packed message are part of the JSON object. Therefore, concrete type information is required to parse from JSON, not just to unpack. In Go, the type is automatically looked up in a global registry that is populated at package init. Our protobuf implementation does not have a global registry because it would hamper tree-shaking, and because it is quite easy to run into conflicts with global registries. This is why you see the error message. The protobuf implementation does not know how to parse the Any from JSON without additional type information. You can provide the types you need by passing a type registry to a transport. Here is an example: import {createRegistry} from "@bufbuild/protobuf";
const transport = createConnectTransport({
baseUrl: "https://demo.connect.build",
jsonOptions: {
typeRegistry: createRegistry(SleepResponse, SleepOperationMetadata)
}
}); This will allow you to parse JSON responses that contain a |
Beta Was this translation helpful? Give feedback.
Hey @coobeet, the message google.protobuf.Any is just string field for the type name and a bytes field for the data. If you parse an Any from binary, the packed message remains serialized until you unpack it. To unpack it, the concrete type information is required. In the JSON format, Any is is not just a slice of bytes with a name, but the actual fields of the packed message are part of the JSON object. Therefore, concrete type information is required to parse from JSON, not just to unpack.
In Go, the type is automatically looked up in a global registry that is populated at package init. Our protobuf implementation does not have a global registry because it would hamper tree-shaking, and…