Skip to content

perf: project scalar nullable anyOf responses - #943

Open
rvitaliy wants to merge 1 commit into
fastify:mainfrom
rvitaliy:fix/native-nullable-anyof
Open

rvitaliy wants to merge 1 commit into
fastify:mainfrom
rvitaliy:fix/native-nullable-anyof

Conversation

@rvitaliy

@rvitaliy rvitaliy commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #942.

Summary

Allow objectMechanism: 'native' to project anyOf with one supported scalar branch and one null branch. The projection calls the same Ajv branch validators in the same order as the ordinary serializer. Constraints, coercion, and mismatch errors remain observable.

The patch keeps the ordinary path when branch merging can change the result. The runtime projection preserves the ordinary string conversion for custom toString() results. The eligibility checks also keep the configured large-array bypass and the ordinary byte order when integer-named keys conflict with required-property order.

Verification

  • npm test passed on Node 20.20.2, 22.23.4, 24.22.3, and 26.13.1 on macOS ARM64. Each ran 730 unit tests at 100% reported coverage and 15 type assertions.
  • npm run lint and npm run benchmark --if-present passed.
  • Regression tests compare output and errors against the ordinary serializer, including Ajv constraints and coercion order, conflicting defaults, custom conversion results, array access, and standalone mode.
  • In a separate benchmark on one paginated API response schema, median serialization time including Buffer.byteLength fell from 205.07 ms to 149.02 ms over 300,000 one-row responses and from 413.53 ms to 265.70 ms over 40,000 25-row responses. Nine alternating samples per workload on Node 26.13.1, macOS ARM64. Both serializers emitted identical bytes for all fixture payloads.

Checklist

Reuse ordered Ajv branch validation in the native projection. Preserve conversion output and keep the string codegen where schema merging or serialization options require it.

Refs fastify#942

This branch has not been deployed

No deployments
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.

Support scalar nullable anyOf in the native object mechanism

1 participant