This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new dd5e2e967 fix(javascript): accept user type id 0 in struct, ext, and
enum factories (#4014)
dd5e2e967 is described below
commit dd5e2e9676136b27b1a21aaeb60157d5cfc20aa7
Author: Ayush Kumar <[email protected]>
AuthorDate: Thu Sep 3 12:13:49 2026 +0530
fix(javascript): accept user type id 0 in struct, ext, and enum factories
(#4014)
## What was the error
`Type.struct(0, ...)`, `Type.ext(0)`, and `Type.enum(0, ...)` threw
`type name and type id should be set at least one` because a truthiness
check treated the numeric id 0 as absent, although `TypeInfo` allows
user type ids in `[0, 0xfffffffe]`. A peer runtime registering a type
under id 0 could not interoperate with JavaScript.
## What this PR fixes
The three factories now check `typeId === undefined` instead of
truthiness, so id 0 registers normally. A regression test covers all
three factories and a full serialize/deserialize round trip under id 0.
---------
Co-authored-by: Shawn Yang <[email protected]>
---
.github/workflows/ci.yml | 2 +-
javascript/packages/core/lib/typeInfo.ts | 18 ++++++------------
javascript/test/object.test.ts | 13 +++++++++++++
3 files changed, 20 insertions(+), 13 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 60f070d46..dde5dd926 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -346,7 +346,7 @@ jobs:
restore-keys: |
${{ runner.os }}-maven-
- name: Set up Gradle
- uses:
gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e
+ uses:
gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5.0.2
with:
gradle-version: "8.13"
- name: Install Fory Java and Kotlin JSON artifacts
diff --git a/javascript/packages/core/lib/typeInfo.ts
b/javascript/packages/core/lib/typeInfo.ts
index 809d7efcb..95ff4a0a4 100644
--- a/javascript/packages/core/lib/typeInfo.ts
+++ b/javascript/packages/core/lib/typeInfo.ts
@@ -251,10 +251,8 @@ export class TypeInfo<T = unknown> extends
ExtensibleFunction {
if (typeId !== undefined && typeName !== undefined) {
throw new Error(`type name ${typeName} and id ${typeId} should not be
set at the same time`);
}
- if (!typeId) {
- if (!typeName) {
- throw new Error(`type name and type id should be set at least one`);
- }
+ if (typeId === undefined && !typeName) {
+ throw new Error(`type name and type id should be set at least one`);
}
if (typeId === undefined) {
const resolved = resolveNameParts(namespace, typeName!);
@@ -313,10 +311,8 @@ export class TypeInfo<T = unknown> extends
ExtensibleFunction {
if (typeId !== undefined && typeName !== undefined) {
throw new Error(`type name ${typeName} and id ${typeId} should not be
set at the same time`);
}
- if (!typeId) {
- if (!typeName) {
- throw new Error(`type name and type id should be set at least one`);
- }
+ if (typeId === undefined && !typeName) {
+ throw new Error(`type name and type id should be set at least one`);
}
if (typeId === undefined) {
const resolved = resolveNameParts(namespace, typeName!);
@@ -378,10 +374,8 @@ export class TypeInfo<T = unknown> extends
ExtensibleFunction {
if (typeId !== undefined && typeName !== undefined) {
throw new Error(`type name ${typeName} and id ${typeId} should not be
set at the same time`);
}
- if (!typeId) {
- if (!typeName) {
- throw new Error(`type name and type id should be set at least one`);
- }
+ if (typeId === undefined && !typeName) {
+ throw new Error(`type name and type id should be set at least one`);
}
if (typeId === undefined) {
const resolved = resolveNameParts(namespace, typeName!);
diff --git a/javascript/test/object.test.ts b/javascript/test/object.test.ts
index 0e9a4b8b1..3f91159e0 100644
--- a/javascript/test/object.test.ts
+++ b/javascript/test/object.test.ts
@@ -248,4 +248,17 @@ describe("object", () => {
expect(evolvingSerializer.deserialize(evolvingPayload)).toEqual({ f1:
"payload" });
expect(fixedSerializer.deserialize(fixedPayload)).toEqual({ f1: "payload"
});
});
+
+ test("should user type id 0 work", () => {
+ // TypeInfo allows user type ids in [0, 0xfffffffe]; a truthiness check on
+ // the numeric id treated 0 as "no id given" and threw.
+ const structInfo = Type.struct(0, { a: Type.int32() });
+ expect(structInfo.userTypeId).toBe(0);
+ expect(() => Type.enum(0, { A: 0 })).not.toThrow();
+ expect(() => Type.ext(0)).not.toThrow();
+
+ const fory = new Fory({ compatible: false });
+ const { serialize, deserialize } = fory.register(structInfo);
+ expect(deserialize(serialize({ a: 7 }))).toEqual({ a: 7 });
+ });
});
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]