protoc-gen-typescript-http icon indicating copy to clipboard operation
protoc-gen-typescript-http copied to clipboard

support build options for filed type

Open BeatsKitano opened this issue 3 years ago • 5 comments

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 avatar Aug 14 '22 10:08 BeatsKitano

@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

ericwenn avatar Aug 22 '22 12:08 ericwenn

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
    ];  
}

shuqingzai avatar Feb 13 '23 03:02 shuqingzai

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?

ericwenn avatar Mar 14 '23 09:03 ericwenn

  1. Perhaps you're right. Described at https://google.aip.dev/203
  2. It is again required when generating openapi documentation at protoc-gen-openapi
  3. There is no field to indicate that the response is required, which makes it very difficult for front-end personnel

shuqingzai avatar Mar 14 '23 10:03 shuqingzai

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.

ericwenn avatar Mar 15 '23 09:03 ericwenn