Skip to content

Enhance type and nullability handling for collection items in schema import#126630

Open
StephenMolloy wants to merge 2 commits intodotnet:mainfrom
StephenMolloy:98240_DCS-Collection-Import
Open

Enhance type and nullability handling for collection items in schema import#126630
StephenMolloy wants to merge 2 commits intodotnet:mainfrom
StephenMolloy:98240_DCS-Collection-Import

Conversation

@StephenMolloy
Copy link
Copy Markdown
Member

@StephenMolloy StephenMolloy commented Apr 8, 2026

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:

  • Added logic in XsdDataContractImporter to cache and track the nullability of collection items by inspecting schema definitions, storing this information in ContractCodeDomInfo.CollectionItemIsNullable. [1] [2] [3]
  • Updated the code generation pipeline (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:

  • Renamed the extension method IsItemTypeNullable to IsCollectionItemNullable and 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:

  • Modified ClassDataContractCriticalHelper to avoid unnecessary initialization when importing from schemas, improving efficiency and correctness for schema-defined types.

Test Enhancements:

  • Expanded round-trip tests to assert correct code generation for nullable collections and to ensure that legacy redirection types are not generated, reflecting the improved schema import behavior. [1] [2] [3]…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.

Fixes #98240

…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.
Copilot AI review requested due to automatic review settings April 8, 2026 06:12
@StephenMolloy StephenMolloy self-assigned this Apr 8, 2026
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Apr 8, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 XmlSchemaSet into ContractCodeDomInfo.CollectionItemIsNullable.
  • Update CodeExporter to prefer the cached nullability over heuristics, and rename the helper to IsCollectionItemNullable.
  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug in XsdDataContractImporter when importing ComplexType

2 participants