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

Errors when message & oneof share field names

Open wsp-repo opened this issue 2 years ago • 3 comments

If the name of the properties inside the oneof and one level above matches, the result of the work of the methods becomes erroneous and irreversible

PROTO

message ExampleType {
  optional string propName1 = 1;
  optional oneof propName2 {
    string propName0 = 2;
    string propName1 = 3;
    string propName3 = 4;
  }
  optional string propName3 = 5;
}

TS

Error1

const value: ExampleType = {
  propName1: "string1"
  propName2: {
    $case: "propName1",
    propName1: "string2"
  }
  propName3: "string3"
}

ExampleType.toJSON(value);

{
  propName1: "string2", // incorrect
  propName3: "string3"
}

Error2

const value: ExampleType = {
  propName1: "string1"
  propName2: "string2",
  propName3: "string3"
}

ExampleType.fromJSON(value);

{
  propName1: "string1",
  propName2: {              // incorrect
    $case: "propName1",
    propName1: "string1",
  }
  propName3: "string3"
}

Error3

const value: ExampleType = {
  propName3: "string3"
}

ExampleType.fromJSON(value);

{
  propName2: {             // incorrect
    $case: "propName3",
    propName1: "string3",
  }
  propName3: "string3"
}

Consequence

const value1 = { ..... };
const value2 = ExampleType.fromJSON(ExampleType.toJSON(value1));

value1 !== value2; // !!!!!

wsp-repo avatar May 18 '23 14:05 wsp-repo

Ah yeah, that looks interesting. I'm not surprised this doesn't work/overlaps.

Honestly I'm not sure/don't remember what the canonical proto3 JSON encoding of your schema would be.

Do you know?

If you figure it out, a pull request would be great. :-) Thanks!

stephenh avatar May 21 '23 18:05 stephenh

Push > ERROR: Permission to stephenh/ts-proto.git denied to wsp-repo

wsp-repo avatar May 22 '23 15:05 wsp-repo

Ah yeah, @wsp-repo I think you can make a fork of stephenh/ts-proto with github, and push to your fork, then make a pull request from there.

stephenh avatar May 22 '23 15:05 stephenh