Skip to content

Commit

Permalink
Add additional tests to illustrate FieldMask serialization failures (#…
Browse files Browse the repository at this point in the history
…490)

This adds additional unit tests to test the failure scenarios when
serializing a `FieldMask` with invalid path names and when deserializing
JSON with invalid field names in the string.

---------

Co-authored-by: Timo Stamm <ts@timostamm.de>
  • Loading branch information
smaye81 and timostamm committed May 22, 2023
1 parent b82ad15 commit 4f34e99
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions packages/protobuf-test/src/google/protobuf/field_mask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,23 @@ describe("google.protobuf.FieldMask", () => {
});
expect(FieldMask.fromJson(json)).toStrictEqual(want);
});
test("toJson fails on invalid fieldmask paths", () => {
const fieldMask = new FieldMask({
paths: ["user.displayName", "photo"],
});
expect(() => {
fieldMask.toJson();
}).toThrow(
'cannot encode google.protobuf.FieldMask to JSON: lowerCamelCase of path name "user.displayName" is irreversible'
);
});
test("fromJson fails on invalid json", () => {
const json = "user.display_name,photo";
expect(() => {
FieldMask.fromJson(json);
}).toThrow(
"cannot decode google.protobuf.FieldMask from JSON: path names must be lowerCamelCase"
);
});
});
});
2 changes: 1 addition & 1 deletion packages/protobuf/src/google/protobuf/field_mask_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/protoc-gen-es/src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
f.print(` text += "." + nanosStr;`)
f.print(" if (this.", localName(ref.nanos), " < 0 && this.", localName(ref.seconds), " === ", protoInt64, ".zero) {");
f.print(` text = "-" + text;`);
f.print(` }`);
f.print(` }`);
f.print(" }")
f.print(` return text + "s";`)
f.print("};");
Expand Down Expand Up @@ -486,7 +486,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
f.print(` }`)
f.print(` return this.`, localName(ref.paths), `.map(p => {`)
f.print(` if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {`)
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: lowerCamelCase of path name \\"" + p + "\\" is irreversible");`)
f.print(` throw new Error("cannot encode `, message.typeName, ` to JSON: lowerCamelCase of path name \\"" + p + "\\" is irreversible");`)
f.print(` }`)
f.print(` return protoCamelCase(p);`)
f.print(` }).join(",");`)
Expand Down
4 changes: 2 additions & 2 deletions packages/protoc-gen-es/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
f.print(` text += "." + nanosStr;`)
f.print(" if (this.", localName(ref.nanos), " < 0 && this.", localName(ref.seconds), " === ", protoInt64, ".zero) {");
f.print(` text = "-" + text;`);
f.print(` }`);
f.print(` }`);
f.print(" }")
f.print(` return text + "s";`)
f.print(" }")
Expand Down Expand Up @@ -513,7 +513,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
f.print(` }`)
f.print(` return this.`, localName(ref.paths), `.map(p => {`)
f.print(` if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {`)
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: lowerCamelCase of path name \\"" + p + "\\" is irreversible");`)
f.print(` throw new Error("cannot encode `, message.typeName, ` to JSON: lowerCamelCase of path name \\"" + p + "\\" is irreversible");`)
f.print(` }`)
f.print(` return protoCamelCase(p);`)
f.print(` }).join(",");`)
Expand Down

0 comments on commit 4f34e99

Please sign in to comment.