Skip to content

fix(protocol): refine Fory 2.0 implementation - #4249

Merged
chaokunyang merged 2 commits into
apache:mainfrom
chaokunyang:fory-2.0-refinement
Dec 4, 2026
Merged

chaokunyang merged 2 commits into
apache:mainfrom
chaokunyang:fory-2.0-refinement

Conversation

@chaokunyang

Copy link
Copy Markdown
Collaborator

Why?

Follow-up fixes for five correctness and protocol edge cases found during post-merge review of Fory 2.0 (#4245):

  1. Go named integers truncated as enums: Treating every user-defined named integer type (such as type Balance int64) as an unregistered enum silently truncated 64-bit and negative values through 32-bit unsigned enum ordinals.
  2. JavaScript conflicting registered identities: Generated TYPED_* (and non-compatible NAMED_*) readers for enum, ext, union, and non-evolving struct discarded the wire user type ID / name, allowing a known registered ID (e.g., enum A) to decode silently into a different registered target (e.g., enum B).
  3. Java Native partial-registration inheritance alignment: TypeDef.getDescriptors numbered only anonymous remote layers instead of the full layer sequence, misaligning superclass anonymous fields into a registered middle layer when shadowing fields were present or added during schema evolution.
  4. Go unbound UNION support: Unregistered Go unions had no way to supply their case table without global ID/name registration.
  5. Swift compatible skip for nested/dynamic unions: ReadContext.readSkipTypeInfo and resolveSkippedTypeInfo rejected .union, .typedUnion, and .namedUnion when skipping nested or dynamic union payloads inside removed fields.

What does this PR do?

  • Go & foryc (go/fory/, benchmarks/go/models.go, compiler/fory_compiler/generators/go.py):
    • Introduce EnumMarker (ForyEnumMarker()) so only explicitly marked Go enums (including foryc-generated enums) use unregistered ENUM ordinal encoding while ordinary named integer types preserve their underlying scalar numeric encoding and full range.
    • Validate ordinal bounds (0 .. math.MaxUint32) in enumSerializer.WriteData instead of silently truncating out-of-range values.
    • Introduce UnionDescriptor (ForyUnionCases() []UnionCase) and emit it in foryc Go union codegen so Go unions can serialize and deserialize as unregistered UNBOUND UNION values without global wire ID/name registration, while still rejecting dynamic any materialization of UNBOUND unions.
  • JavaScript (javascript/packages/core/lib/):
    • Validate wire userTypeId for TYPED_ENUM, TYPED_EXT, TYPED_UNION, and TYPED_STRUCT (and wire namespace/typeName for non-compatible NAMED_*) with a fast primitive comparison and cold error helper.
    • Reject conflicting known registered wire identities on UNBOUND targets in ReadContext.readAsymmetricNonEvolvingTypeInfo and readIdentityOnlyTypeMetaForUnboundTarget, while preserving asymmetric fallback for unknown wire identities.
  • Java (java/fory-core/src/main/java/org/apache/fory/meta/TypeDef.java):
    • Track the complete remote layer sequence in TypeDef.getDescriptors and align anonymous layers between monotonic registered/named anchor layers so superclass fields stay bound to their owning layer segment.
  • Swift (swift/Sources/Fory/):
    • Add TypeInfo.skippedUnion() backed by ReadContext.readSkippedUnion() and handle .union, .typedUnion, and .namedUnion in ReadContext.readSkipTypeInfo and resolveSkippedTypeInfo.
  • Tests:
    • Add regression tests covering all five fixes in go/fory/fory_2_0_test.go, compiler/fory_compiler/tests/test_fory_2_0_codegen.py, javascript/test/fory2.test.ts, java/fory-core/src/test/java/org/apache/fory/Fory20UnregisteredTest.java, and swift/Tests/ForyTests/Fory20Tests.swift.

Related issues

Follow-up to #4245.

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change? (Adds Go EnumMarker and UnionDescriptor opt-in marker/descriptor interfaces for unregistered Go enums and unions.)
  • Does this PR introduce any binary protocol compatibility change?

Verification

  • Go: go test -race ./... in go/fory/, Go benchmark compile, Go IDL codegen & tests, Go gRPC peer build, go fmt.
  • Compiler: pytest (584 passed), ruff check, ruff format --check.
  • JavaScript: full Jest suite (26 suites / 396 tests passed), npm run format-check.
  • Java: Fory20UnregisteredTest, TypeDefTest, NativeTypeDefEncoderTest, mvn spotless:check -pl fory-core.
  • Swift: swift test (289 tests passed), swiftlint lint.

…a native layer alignment, and Swift union skip
@chaokunyang chaokunyang changed the title fix(protocol): refine Fory 2.0 Go enum/union, JS identity checks, Java native layer alignment, and Swift union skip fix(protocol): refine Fory 2.0 implementation Dec 4, 2026
@chaokunyang
chaokunyang merged commit 9185659 into apache:main Dec 4, 2026
78 checks passed
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

Successfully merging this pull request may close these issues.

2 participants