ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

using `--ts_proto_opt=useJsonName=true` breaks generated code for `google/protobuf/struct.proto`

Open srliao opened this issue 4 months ago • 0 comments

We rely on --ts_proto_opt=useJsonName=true option so that the generated typing for our types has the correct casing. However, using this option causes the generated code for google/protobuf/struct.proto to be incorrect like so:

  wrap(value: any): Value {
    const result = createBaseValue();
    if (value === null) {
      result.null_value = NullValue.NULL_VALUE;
    } else if (typeof value === "boolean") {
      result.bool_value = value;
    } else if (typeof value === "number") {
      result.number_value = value;
    } else if (typeof value === "string") {
      result.string_value = value;
    } else if (globalThis.Array.isArray(value)) {
      result.list_value = value;
    } else if (typeof value === "object") {
      result.struct_value = value;
    } else if (typeof value !== "undefined") {
      throw new globalThis.Error("Unsupported any value type: " + typeof value);
    }
    return result;
  },

But the actual fields are named nullValue, boolValue etc... as can be seen as follows:

function createBaseValue(): Value {
  return {
    nullValue: undefined,
    numberValue: undefined,
    stringValue: undefined,
    boolValue: undefined,
    structValue: undefined,
    listValue: undefined,
  };
}

Disabling --ts_proto_opt=useJsonName=true will result in all the field being named snake case for the generated google/protobuf/struct.proto code and the issues goes away but we are left with incorrect casing for our own definitions.

srliao avatar Oct 21 '24 15:10 srliao