Repository navigation
support isSequence in reflection xml serializer - #129493
StephenMolloy merged 10 commits into
Conversation
The reflection-based XmlSerializer deserializer hardcoded IsSequence() to false, so types with explicitly ordered members ([XmlElement(Order = N)], i.e. xsd:sequence) were read as unordered. Members that map to the same element name and are distinguished only by order were misassigned. Implement the same ordering state machine the IL/CodeGen based reader uses: track the current sequence position and only match the member expected at that position, advancing per ILGen semantics (non-array members advance after each read/non-match; array-like members advance only on a non-match). Uncomment XML_TypeWithFieldsOrdered so it runs against the reflection serializer and drop the duplicate copy kept in XmlSerializerTests.Internal.cs. Fixes dotnet#1402 Co-authored-by: Copilot <[email protected]>
Address review feedback: rather than splitting WriteMemberElementsIf into separate sequence and non-sequence branches, keep the original single foreach over the members and gate the ordering logic on the sequence state inside it. The non-sequence path is now identical to the original; the sequence path skips members until it reaches the one at the current position and advances the position with the same array-like semantics. Co-authored-by: Copilot <[email protected]>
Add a comment to WriteMemberElementsIf explaining that the single-loop structure mirrors XmlSerializationReaderILGen.WriteMemberElementsIf (which also uses one member loop with inline isSequence branches, not two separate loops), and justifying the one reflection-specific deviation: scanning to the member at the current sequence position instead of emitting an else-if chain keyed on a state local. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR updates the reflection-based XmlSerializer reader to honor explicitly ordered members (xsd:sequence semantics via [XmlElement(Order = N)]) by tracking a sequence position and only matching the member expected at that position. It also re-enables the existing ordered-fields regression test for the reflection serializer and removes the duplicate copy previously kept in the internal test file.
Changes:
- Implement sequence-aware member matching in
ReflectionXmlSerializationReaderusing a per-read-loop sequence state. - Re-enable
XML_TypeWithFieldsOrderedinXmlSerializerTests.cs. - Remove the duplicate
XML_TypeWithFieldsOrderedtest fromXmlSerializerTests.Internal.cs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs | Adds sequence-state tracking and sequence-aware matching logic for ordered members in the reflection reader. |
| src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs | Uncomments/enables the ordered-fields regression test so it runs against the reflection serializer. |
| src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs | Removes the now-redundant duplicate test. |
sequenceState is non-null only for sequence types and always travels with the same member array that produced it, so 'sequenceState != null' already implies IsSequence(expectedMembers). Drop the redundant per-node IsSequence rescan in the reflection-based read loop. Co-authored-by: Copilot <[email protected]>
The reflection reader's sequence state machine advances past an array-like member only when a non-matching element is seen, allowing it to absorb repeated elements before later ordered members are read. Add TypeWithArrayLikeFieldsOrdered and a round-trip test that exercises this branch (repeated <num> elements followed by ambiguous ordered <strfld> members), guarding against drift from the IL/CodeGen reader semantics. The test passes on both the reflection and IL-based serializer paths. Co-authored-by: Copilot <[email protected]>
Relocate XML_TypeWithFieldsOrdered and XML_TypeWithArrayLikeFieldsOrdered to XmlSerializerTests.RuntimeOnly.cs, and the new TypeWithArrayLikeFieldsOrdered type to SerializationTypes.RuntimeOnly.cs. The RuntimeOnly files are compiled only into the runtime (IL and reflection) test assemblies, not the SerializableAssembly consumed by the pre-gen serializer (sgen) tests. This keeps the new ordered-sequence test type out of the pre-gen serializer surface so its Expected.SerializableAssembly.XmlSerializers.cs support file does not need regenerating. TypeWithFieldsOrdered remains in SerializationTypes.cs since it already has a generated serializer there. Co-authored-by: Copilot <[email protected]>
Add back [Fact] attribute for Xml_TypeWithArrayLikeChoiceElement test
|
Workflow state for the Holistic Review Orchestrator. {
"version": 5,
"last_dispatched_commit": "ec113bc3ddf4e961bd4a6e643f64d5723632a11b",
"last_dispatched_base_ref": "main",
"last_dispatched_base_sha": "8dd1f60f48798789aea5967a7df682e7803468e7",
"last_reviewed_commit": "ec113bc3ddf4e961bd4a6e643f64d5723632a11b",
"last_reviewed_base_ref": "main",
"last_reviewed_base_sha": "8dd1f60f48798789aea5967a7df682e7803468e7",
"last_recorded_worker_run_id": "29684007436",
"review_attempt_commit": "",
"review_attempt_base_ref": "",
"review_attempt_count": 0,
"max_review_attempts": 5,
"review_history_format": "holistic-review-disclosure-v1",
"review_history": [
{
"commit": "ec113bc3ddf4e961bd4a6e643f64d5723632a11b",
"review_id": 4730636507
}
]
} |
There was a problem hiding this comment.
Holistic Review
Motivation: The reflection-based XmlSerializer reader hardcoded IsSequence() to false, so types with explicitly ordered members ([XmlElement(Order = N)], i.e. xsd:sequence) were read as unordered. Members mapping to the same element name and distinguished only by order were misassigned. This is a real, long-standing correctness gap (issue #1402) between the reflection reader and the IL/CodeGen reader, easily reproduced by the added tests.
Approach: The fix ports the ordering state machine from XmlSerializationReaderILGen.WriteMemberElementsIf into ReflectionXmlSerializationReader. IsSequence(Member[]) now matches the ILGen predicate (IsParticle && IsSequence), and a sequenceState cursor is threaded through WriteMembers -> WriteMemberElements -> WriteMemberElementsIf. Only the member at the current position is a candidate; non-array members advance after one read, array-like members advance only on a non-match, and a non-match advances the cursor without consuming the element -- mirroring the IL else-if/state chain. The dead IsSequence() no-op stubs are removed. This is the right approach: reusing the established ILGen semantics rather than inventing new ordering logic minimizes behavioral divergence, and the observable behavior maps cleanly from the emitted-IL control flow to the interpreted scan.
Summary: LGTM. The change is well-scoped, faithfully reproduces the ILGen state machine (including the ele.Any && ele.AnyNamespaces == null sequence-match branch and the array-like advance rule), and removes stale dead code. Tests are strong: XML_TypeWithFieldsOrdered moves into the runtime-executed path and a new XML_TypeWithArrayLikeFieldsOrdered covers an array-like member embedded mid-sequence -- both exercise the ambiguous shared strfld element name so ordering is genuinely required, and both run under the ReflectionOnly test configuration that gates the fixed path. No new public API surface. One area a human may wish to spot-check given XML serialization's subtlety: interaction of the new sequence cursor with text/attribute and choice members in a sequence (these are continue-skipped, matching ILGen), but I found no concrete defect there.
Detailed Findings
Correctness -- State machine matches ILGen (verified)
The reflection logic in WriteMemberElementsIf corresponds in intent with XmlSerializationReaderILGen.WriteMemberElementsIf: the currentCase++ != sequenceState[0] skip replaces the emitted state == cases guard; the array-like vs. non-array advance mirrors the member.IsArrayLike ? Else : EndIf + state = cases emission; and the non-match sequenceState[0]++; return; mirrors resuming the outer read loop at the next state. IsSequence(Member[]) is identical to the ILGen predicate.
Scope / Dead code -- Stubs removed cleanly (verified)
The four #1402 placeholder comments/no-op blocks are removed, including the empty IsSequenceAllMembers branch in the collection path. Nothing else in that path depended on them.
Test quality -- Meaningfully exercises the fix (verified)
Both tests use out-of-declaration-order Order values and a shared ambiguous element name, so a correct result is impossible without honoring sequence order -- they would have failed before this change and pass after. The duplicate copy previously parked in XmlSerializerTests.Internal.cs is removed and the commented-out copy in XmlSerializerTests.cs is deleted, avoiding divergence. TypeWithArrayLikeFieldsOrdered is added to the shared SerializationTypes.RuntimeOnly.cs consistent with sibling test types.
Observation -- sequenceState as int[1]
Using a single-element int[] as a mutable by-reference cursor threaded through optional parameters is slightly unusual but is a reasonable, allocation-cheap way to share mutable position across the call chain without changing method signatures to ref, and the added comments explain the invariant (non-null iff sequence). Not a blocker.
Note
This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.
Generated by Holistic Review · 130.4 AIC · ⌖ 15 AIC · ⊞ 10K
|
/ba-g failed tests unrelated |
The reflection-based XmlSerializer deserializer hardcoded IsSequence() to
false, so types with explicitly ordered members ([XmlElement(Order = N)],
i.e. xsd:sequence) were read as unordered. Members that map to the same
element name and are distinguished only by order were misassigned.
Implement the same ordering state machine the IL/CodeGen based reader uses:
track the current sequence position and only match the member expected at
that position, advancing per ILGen semantics (non-array members advance
after each read/non-match; array-like members advance only on a non-match).
Uncomment XML_TypeWithFieldsOrdered so it runs against the reflection
serializer and drop the duplicate copy kept in XmlSerializerTests.Internal.cs.
Fixes #1402