diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 60f070d46d..dde5dd926a 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 809d7efcbc..95ff4a0a4d 100644 --- a/javascript/packages/core/lib/typeInfo.ts +++ b/javascript/packages/core/lib/typeInfo.ts @@ -251,10 +251,8 @@ export class TypeInfo 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 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 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 0e9a4b8b1c..3f91159e01 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 }); + }); });