Fix serializeRecord to handle null/undefined additionalProperties#3575
Merged
Fix serializeRecord to handle null/undefined additionalProperties#3575
Conversation
…rties Co-authored-by: v-jiaodi <80496810+v-jiaodi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix SerializeRecord to support null and undefined
Fix serializeRecord to handle null/undefined additionalProperties
Nov 4, 2025
Member
|
@copilot Run smoke test to fix ci under folder |
Co-authored-by: v-jiaodi <80496810+v-jiaodi@users.noreply.github.com>
Contributor
Author
MaryGao
approved these changes
Nov 4, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix SerializeRecord to handle null and undefined for additional properties
Summary
This PR fixes an issue where generated serializer functions would fail when
additionalPropertiesisnullorundefined.Problem
When TypeSpec generates TypeScript serializer functions for models with additional properties, it calls
serializeRecord(item.additionalProperties). TheserializeRecordfunction internally callsObject.keys(item), which throws a TypeError ifitemisnullorundefined:Solution
Modified the code generation in
buildSerializerFunction.tsto add a nullish coalescing operator (?? {}), so the generated code becomes:This ensures that an empty object is passed to
serializeRecordwhenadditionalPropertiesisnullorundefined, preventing the runtime error.Changes Made
packages/typespec-ts/src/modular/serialization/buildSerializerFunction.tsto add?? {}when generating serializeRecord calls (line 476)packages/typespec-ts/test/modularUnit/scenarios/models/serialization/additionalProperties.md(7 test cases)packages/typespec-ts/test/modularUnit/scenarios/modelsGenerator/modelsGenerator.md(1 test case)Validation
rush formatnpm run lintFix Verification
The fix follows the suggestion from @MaryGao in the original issue and ensures that:
additionalPropertiesis defined: normal behavior (serialize the properties)additionalPropertiesisnullorundefined: safely pass an empty object toserializeRecord, which returns{}Generated Code Example
The smoke test regenerated files showing the fix working correctly:
Original prompt
nullandundefinedfor additional properties #3574✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.