有没有办法仅输出原始定义的响应体中的一个字段

发布于 2025-01-24 23:08:22 字数 2353 浏览 4 评论 0原文

对于上下文,我正在调查为什么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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2025-01-31 23:08:22

您是否尝试使用可选关键字?

message Form {

  // other fields ...

  optional PciSaq pci_saq_a = 2;
  optional PciSaq pci_saq_b = 3;
  optional PciSaq pci_saq_c = 4;
}

这个有关可选的问题看起来很有趣:如何在Protobuf 3中定义可选字段3

Did you try to use the optional keyword ?

message Form {

  // other fields ...

  optional PciSaq pci_saq_a = 2;
  optional PciSaq pci_saq_b = 3;
  optional PciSaq pci_saq_c = 4;
}

This question about optional looks interesting : How to define an optional field in protobuf 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文