-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix ObsoleteAttribute incorrectly causing XML serialization to ignore properties with AppContext switch and SR resources #119865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@copilot - Our xml serializers shouldn't be silently ignoring [Obsolete] members. The way this should work is if a member is marked [Obsolete(IsError=true)], then the serializer should throw an exception. If 'IsError' is not set or not true, then the member should still serialize normally. |
Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
I've updated the implementation based on your feedback. The XML serializers now handle
This preserves the intended behavior where Changes implemented in commit cf89504. |
|
@copilot - the new exceptions being thrown should use |
…ttribute behavior Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
I've updated the implementation based on your feedback:
The switch being off by default means the new, correct behavior is enabled by default, but users can opt back into the old behavior if needed for compatibility. Changes implemented in commit 90d0b03. |
…- which surface as errors in our build pipelines.
There was a problem hiding this 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 fixes an issue where properties marked with [Obsolete] were incorrectly being ignored during XML serialization, similar to how [XmlIgnore] works. The fix ensures that obsolete properties are serialized normally by default, with an option to throw exceptions for IsError=true cases and an AppContext switch for backward compatibility.
Key Changes
- Modified XML serialization attribute handling to differentiate between
ObsoleteAttributeandXmlIgnoreAttribute - Added AppContext switch
Switch.System.Xml.IgnoreObsoleteMembersto control legacy behavior - Added proper error handling with localized messages for
ObsoleteAttribute.IsError=truecases
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| SerializationTypes.RuntimeOnly.cs | Added test types with obsolete properties for different scenarios |
| XmlSerializerTests.cs | Added comprehensive tests including fixture for AppContext switch management |
| XmlAttributes.cs | Updated constructor to handle ObsoleteAttribute separately from XmlIgnoreAttribute |
| SoapAttributes.cs | Updated constructor to handle ObsoleteAttribute separately from SoapIgnoreAttribute |
| LocalAppContextSwitches.cs | Added IgnoreObsoleteMembers switch property |
| Strings.resx | Added localized error message for obsolete properties with IsError=true |
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs
Outdated
Show resolved
Hide resolved
…izerTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
mconnew
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![]()
|
Don't forget to run this by those owning the breaking changes document to see if this would class as a breaking change as it is a behavior change and this could cause data to be output which wasn't previously. I don't think it meets the bar, but worth checking. |
|
/backport to release/10.0 |
|
Started backporting to release/10.0: https://github.com/dotnet/runtime/actions/runs/17871036556 |
|
I remember this behavior going back to .Net Framework 1.0/2.0 in the early days. |
|
Added When you commit this breaking change:
Tagging @dotnet/compat for awareness of the breaking change. |
|
@rhuijben, the scenario where this was reported was when referencing a .NET type which used the Obsolete attribute (SslProtocols I think) which caused the data to disappear in the output. What used to work subsequently broke when a newer version of the runtime added the Obsolete attribute. I hope you can see that this was the wrong behavior previously. I suspect this behavior originates from the fact that XmlSerializer used to generate a source code implementation and compile it and someone decided they didn't want the compiler warnings. |
|
@StephenMolloy Even though there might not be many affected, I do still recommend creating the breaking change doc issue and sending the announcement linking to the doc issue as described in the comment: #119865 (comment). This should be done ahead of the RC2 release. |
📋 Breaking Change Documentation RequiredCreate a breaking change issue with AI-generated content Generated by Breaking Change Documentation Tool - 2025-10-03 14:13:00 |
This PR fixes an issue where properties marked with
[Obsolete]were incorrectly being ignored during XML serialization, similar to how[XmlIgnore]works.Problem
The XML serialization infrastructure was treating
ObsoleteAttributethe same asXmlIgnoreAttribute, causing properties marked as obsolete to be excluded from serialization. This behavior is unexpected sinceObsoleteAttributeis intended only to provide compile-time warnings about deprecated APIs, not to affect runtime serialization behavior.Example
Solution
Updated the
ObsoleteAttributehandling logic in bothXmlAttributes.csandSoapAttributes.csto:ObsoleteAttribute.IsError = false(default): Serialize normally (don't ignore)ObsoleteAttribute.IsError = true: Throw anInvalidOperationExceptionusing SR string resources during serializer creationSwitch.System.Xml.IgnoreObsoleteMembers(off by default) to restore legacy behavior when neededKey Features
IsError=truethrows localized exceptionSRstring resources for exception messages with proper localizationChanges Made
IgnoreObsoleteMembersswitch propertyXmlObsoleteIsErrorstring resource for localized error messagesThis ensures that:
[Obsolete]are serialized normally by default[Obsolete("message", true)]throw a localized exception to prevent runtime usage[XmlIgnore]continue to be ignored as expectedObsoleteAttributemaintains its intended purpose of providing compile-time warnings by defaultTests
Added comprehensive tests including new test classes and test methods to verify:
IsError=falseare properly serializedIsError=truethrow exceptions during serializer creation with proper SR messagesFixes #100453.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.