gRPC 无法序列化具有任意属性的对象

问题描述 投票:0回答:1

上下文是我正在开发一个可配置的模拟 gRPC 服务器,它将在 Docker 实例中运行并充当后端服务的模拟实例。我使用原型文件和配置来初始化每个实例,该配置告诉它要侦听哪些函数以及要响应哪些数据。

我正在使用

@grpc/grpc-js
@grpc/proto-loader
。我已经将其设置为返回任意标量值以及具有已知模式的数组和对象。一切都正常,直到我到达我想要生成任意对象(即地图)的部分。由于某种原因,gRPC 服务器无法识别我作为对象传递给它的对象并抱怨。

客户报告的错误:

Error: 13 INTERNAL: Error serializing response: .Result.testObject: object expected
    at callErrorFromStatus (***/test/grpc/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
    at Object.onReceiveStatus (***/test/grpc/node_modules/@grpc/grpc-js/build/src/client.js:192:76)
    at Object.onReceiveStatus (***/test/grpc/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)
    at Object.onReceiveStatus (***/test/grpc/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)
    at ***/test/grpc/node_modules/@grpc/grpc-js/build/src/resolving-call.js:99:78
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
    at ServiceClientImpl.makeUnaryRequest (***/test/grpc/node_modules/@grpc/grpc-js/build/src/client.js:160:32)
    at ServiceClientImpl.<anonymous> (***/test/grpc/node_modules/@grpc/grpc-js/build/src/make-client.js:105:19)
    at file://***/test/grpc/index.mjs:14:10
    at file://***/test/grpc/index.mjs:18:3
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12) {
  code: 13,
  details: 'Error serializing response: .Result.testObject: object expected',
  metadata: Metadata {
    internalRepr: Map(2) { 'content-type' => [Array], 'date' => [Array] },
    options: {}
  }
}

这是我的原型文件(客户端和服务器都相同):

syntax = "proto3";

service TestApi {
  rpc Execute(None) returns (Result);
}

message None {}

message Result {
  map<string, Value> testObject = 1;
}

message Value {
  oneof kind {
    bool boolValue = 1;
    int32 intValue = 2;
    double doubleValue = 3;
    string stringValue = 4;
  }
}

服务器处理函数:

export function handler(call, callback) {
  try {
    const response = generateValues('$', outputs); // Generate random response
    console.log(response);
    callback(null, response);
  } catch (e) {
    console.error(e);
    callback(e, null);
  }
}

console.error
永远不会点火。
console.log
输出示例:

{
  testObject: {
    VmZWD: false,
    VXiRwi: 28.087891844342792,
    cjCTDFD: false,
    inYZS: 6,
    AYKahk: ' xsyflf szsnwb ush',
    pgUT: 23.19160119367565
  }
}

我尝试过的事情导致了同样的错误:

  • Value
    更改为
    google.protobuf.Any
    (带有关联的导入)
  • json
    加载函数的
    object
    oneOf
    proto-loader
    选项设置为 true 或 false。

请注意,将

map
更改为另一个结构并不是一个选项,因为该图像旨在能够模拟的生产原型已经在其定义中使用了
map

javascript node.js protocol-buffers grpc
1个回答
0
投票

您生成的对象与您定义的消息类型不匹配。

testObject
映射字段具有值类型
Value
,它被定义为包含多个字段之一的消息。在示例响应对象中,键
VmZWD
具有值
false
,但表示值
Value
false
消息将表示为
{ boolValue: false }
。所有地图条目都有同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.