Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FieldMask type mistake with TypeScript #847

Open
fengpeng opened this issue Jun 9, 2023 · 2 comments
Open

FieldMask type mistake with TypeScript #847

fengpeng opened this issue Jun 9, 2023 · 2 comments

Comments

@fengpeng
Copy link

fengpeng commented Jun 9, 2023

Hi~
I am using FieldMask to encounter some problems

// google/protobuf/field_mask.proto
message FieldMask {
  repeated string paths = 1;
}

generated typeScript code:

export interface PatchMeta_Request {
  fieldMask: string[] | undefined;
}

This request will call its own fromJson:

{
  key: isSet(object.key) ? FileId.fromJSON(object.key) : undefined,
  // 
  fieldMask: isSet(object.fieldMask) ? FieldMask.unwrap(FieldMask.fromJSON(a)) : undefined,
  //
  meta: isSet(object.meta) ? FileMeta.fromJSON(object.meta) : undefined,
}

But FieldMask.fromJSON doesn't support string[]

fromJSON(object: any): FieldMask {
    return {
      paths: typeof (object) === "string"
        ? object.split(",").filter(Boolean)
        : Array.isArray(object?.paths)
        ? object.paths.map(String)
        : [],
    };
 }

So, I read the source code, maybe that's the problem?

// ts-proto/src/types.ts
export function valueTypeName(ctx: Context, typeName: string): Code | undefined {
....
  case ".google.protobuf.FieldMask":
      return ctx.options.useJsonWireFormat
        ? code`string`
        : ctx.options.useReadonlyTypes
        ? code`readonly string[]`
        : code`string[]`;  // to code`{paths: string[]}`?
...
}

Thanks!

@fengpeng
Copy link
Author

fengpeng commented Jun 9, 2023

oh, I made a mistake. It's better to start with fromObject

  const canonicalFromJson: { [key: string]: { [field: string]: (from: string) => Code } } = {
    ["google.protobuf.FieldMask"]: {
      paths: (from: string) => code`typeof(${from}) === 'string'
        ? ${from}.split(",").filter(Boolean)
        : Array.isArray(${from})
        ? ${from}
        : Array.isArray(${from}?.paths)
        ? ${from}.paths.map(String)
        : []`,
    },
  };

@stephenh
Copy link
Owner

stephenh commented Jul 1, 2023

Hi @fengpeng ! Thanks for digging into the source... Fwiw I can't tell if someone is still wrong, or you figured it out. We have a unit test that shows FieldMask is working for our current set of tests:

https://github.com/stephenh/ts-proto/blob/main/integration/fieldmask/fieldmask-test.ts#L8

If you've found a bug, could you work on a reproduction, using that test as a starting point?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants