protoc-gen-typescript-http
protoc-gen-typescript-http copied to clipboard
support build options for filed type
This is a wonderful plugin! thank you for open source. I have a little issue.this is my proto file hello.proto:
syntax = "proto3";
message Reply {
string id = 1;
}
this lib generate filed id type to string | undefined, but field id is define.
If id is optional, i think string | undefined is ok, now id is default required, the generate field type should be string, not string | undefined
version: latest
@BeatsKitano This is intentional, as the default value fields (for example 0 on an int) may be omitted from the json serialization of protobuf message.
See https://developers.google.com/protocol-buffers/docs/proto3#json
If the required option is set, undefined should not be required, right? Now there will be undefined whether it is set or not
syntax = "proto3";
import "google/api/field_behavior.proto";
message Reply {
string id = 1 [
(google.api.field_behavior) = REQUIRED
];
}
We've been discussing this internally, and landed on always setting type to T | undefined, as it will be consistent across proto versions and behavior.
For example REQUIRED might mean the value must be set on input, but does it require it to be set on output?
- Perhaps you're right. Described at https://google.aip.dev/203
- It is again required when generating openapi documentation at protoc-gen-openapi
- There is no field to indicate that the response is required, which makes it very difficult for front-end personnel
Regarding 3, there is a comment including the field behavior, for example:
// The resource name of the origin site of the shipment.
// Format: shippers/{shipper}/sites/{site}
//
// Behaviors: REQUIRED
originSite: string | undefined;
I'd be open to having build options for controlling this. Feel free to create a PR.