1

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;

1 Answers1

1

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

n4n5
  • 66
  • 4