有没有办法仅输出原始定义的响应体中的一个字段
对于上下文,我正在调查为什么One One返回从未设定的字段的空字段。以下是我的原始定义和响应主体。我使用Proto Buf定义作为在我的应用程序中传递消息的一种方式,并在订单中将DB对象转换回原始对象,我使用转换器类转换DB->原始buf,反之亦然,
如果我明确将pcisaq saq_b设置为java生成的代码:
V1Form formObject = new V1Form();
formObject.setId("some_id");
formObject.setCreatedAt("");
formObject.setUpdatedAt("");
formObject.setSaqB("some_object");
// the response body is shown below:
api响应
{
"id": "xxxxxxxxx",
"created_at": "2022-04-26T22:57:51.671825-07:00",
"updated_at": "2022-04-26T22:57:51.671825-07:00",
"pci_saq_a": null,
"pci_saq_b": {
"name": null,
"signed_at": null,
"user_agent": null,
"ip_address": null,
"is_accepted": null
},
"pci_saq_c": null,
}
,则预期的行为应为:
{
"id": "xxxxxxxxx",
"created_at": "2022-04-26T22:57:51.671825-07:00",
"updated_at": "2022-04-26T22:57:51.671825-07:00",
"pci_saq_b": {
"name": null,
"signed_at": null,
"user_agent": null,
"ip_address": null,
"is_accepted": null
},
}
proto定义:
message Form {
string id = 1 [
(google.api.field_behavior) = OUTPUT_ONLY,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
read_only: true,
example: '"377eca8f-8cf4-42d4-bde6-17d44dc1e961"'
}
];
google.protobuf.Timestamp created_at = 2[
(google.api.field_behavior) = OUTPUT_ONLY,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
read_only: true
}
];
// The time the resource will update.
google.protobuf.Timestamp updated_at = 3[
(google.api.field_behavior) = OUTPUT_ONLY,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
read_only: true
}
];
oneof pci_form {
PciSaq pci_saq_a = 2;
PciSaq pci_saq_b = 3;
PciSaq pci_saq_c = 4;
}
}
form.java生成的代码包含字段也是如此
@JsonProperty("pci_saq_a")
private V1PciSaq pciSaqA;
@JsonProperty("pci_saq_b")
private V1PciSaq pciSaqB;
@JsonProperty("pci_saq_c")
private V1PciSaq pciSaqC;
For context, I am investigating why oneof returns null fields for fields that were never set. Below is my proto definition, and response body. I use proto buf definitions as a way to pass messages within my application, and inorder to convert db objects back to proto objects i use a converter class to convert db -> proto buf, vice versa, therafter the proto buf definition serves as a response to the api call
If I explicitly set PciSaq saq_b with java generated code:
V1Form formObject = new V1Form();
formObject.setId("some_id");
formObject.setCreatedAt("");
formObject.setUpdatedAt("");
formObject.setSaqB("some_object");
// the response body is shown below:
api response
{
"id": "xxxxxxxxx",
"created_at": "2022-04-26T22:57:51.671825-07:00",
"updated_at": "2022-04-26T22:57:51.671825-07:00",
"pci_saq_a": null,
"pci_saq_b": {
"name": null,
"signed_at": null,
"user_agent": null,
"ip_address": null,
"is_accepted": null
},
"pci_saq_c": null,
}
however, the expected behavior should be:
{
"id": "xxxxxxxxx",
"created_at": "2022-04-26T22:57:51.671825-07:00",
"updated_at": "2022-04-26T22:57:51.671825-07:00",
"pci_saq_b": {
"name": null,
"signed_at": null,
"user_agent": null,
"ip_address": null,
"is_accepted": null
},
}
proto definition:
message Form {
string id = 1 [
(google.api.field_behavior) = OUTPUT_ONLY,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
read_only: true,
example: '"377eca8f-8cf4-42d4-bde6-17d44dc1e961"'
}
];
google.protobuf.Timestamp created_at = 2[
(google.api.field_behavior) = OUTPUT_ONLY,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
read_only: true
}
];
// The time the resource will update.
google.protobuf.Timestamp updated_at = 3[
(google.api.field_behavior) = OUTPUT_ONLY,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
read_only: true
}
];
oneof pci_form {
PciSaq pci_saq_a = 2;
PciSaq pci_saq_b = 3;
PciSaq pci_saq_c = 4;
}
}
Form.java generated code contains fields as well
@JsonProperty("pci_saq_a")
private V1PciSaq pciSaqA;
@JsonProperty("pci_saq_b")
private V1PciSaq pciSaqB;
@JsonProperty("pci_saq_c")
private V1PciSaq pciSaqC;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试使用
可选
关键字?这个有关
可选
的问题看起来很有趣:如何在Protobuf 3中定义可选字段3Did you try to use the
optional
keyword ?This question about
optional
looks interesting : How to define an optional field in protobuf 3