Enhance type and nullability handling for collection items in schema import#126630
Open
StephenMolloy wants to merge 2 commits intodotnet:mainfrom
Open
Enhance type and nullability handling for collection items in schema import#126630StephenMolloy wants to merge 2 commits intodotnet:mainfrom
StephenMolloy wants to merge 2 commits intodotnet:mainfrom
Conversation
…ation - Introduced CollectionItemIsNullable property in ContractCodeDomInfo. - Added GetCollectionItemNullability method to determine item type nullability. - Updated related methods in CodeExporter and SchemaImportHelper for improved nullability checks. - Implemented CacheCollectionItemNullability to cache nullability information during schema import. - Enhanced RoundTripTest to validate new collection item nullability scenarios.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves schema-import code generation for collection item nullability by caching item nillable information during import and using it during code export, with updated tests to validate the new shapes.
Changes:
- Cache collection item nullability from the imported
XmlSchemaSetintoContractCodeDomInfo.CollectionItemIsNullable. - Update
CodeExporterto prefer the cached nullability over heuristics, and rename the helper toIsCollectionItemNullable. - Expand round-trip tests to assert correct generated shapes and to ensure legacy “redirection” types are not generated.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Runtime.Serialization.Schema/tests/System/Runtime/Serialization/Schema/RoundTripTest.cs | Updates assertions to validate collection nullability codegen and absence of legacy redirection types. |
| src/libraries/System.Runtime.Serialization.Schema/src/System/Runtime/Serialization/Schema/XsdDataContractImporter.cs | Adds caching of collection item nullability by inspecting schema types during import. |
| src/libraries/System.Runtime.Serialization.Schema/src/System/Runtime/Serialization/Schema/SchemaImportHelper.cs | Renames and expands collection item nullability heuristic logic. |
| src/libraries/System.Runtime.Serialization.Schema/src/System/Runtime/Serialization/Schema/ContractCodeDomInfo.cs | Adds CollectionItemIsNullable cache slot for codegen decisions. |
| src/libraries/System.Runtime.Serialization.Schema/src/System/Runtime/Serialization/Schema/CodeExporter.cs | Uses cached collection item nullability during export and adjusts helper method structure. |
| src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/ClassDataContract.cs | Skips heavy initialization for schema-defined placeholder types during schema import. |
...m.Runtime.Serialization.Schema/src/System/Runtime/Serialization/Schema/SchemaImportHelper.cs
Outdated
Show resolved
Hide resolved
...time.Serialization.Schema/src/System/Runtime/Serialization/Schema/XsdDataContractImporter.cs
Outdated
Show resolved
Hide resolved
This was referenced Apr 8, 2026
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.
This pull request improves the accuracy of collection item nullability detection when importing schemas and generating code, leading to more correct handling of collections in generated data contracts. It introduces logic to inspect schema information for collection item types and nullability, updates internal APIs for clarity, and enhances test coverage for these scenarios.
Schema Import and Nullability Detection Improvements:
XsdDataContractImporterto cache and track the nullability of collection items by inspecting schema definitions, storing this information inContractCodeDomInfo.CollectionItemIsNullable. [1] [2] [3]CodeExporter) to use the cached nullability information when determining if collection items are nullable, improving accuracy over previous heuristics. [1] [2] [3] [4] [5]API and Naming Consistency:
IsItemTypeNullabletoIsCollectionItemNullableand improved its logic for determining item nullability, including handling of non-standard XML naming and fallback for schema-defined types.Schema Import Bypass in Special Cases:
ClassDataContractCriticalHelperto avoid unnecessary initialization when importing from schemas, improving efficiency and correctness for schema-defined types.Test Enhancements:
Fixes #98240