-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Nullable: System.Xml, part 7 (Serialization) #41261
Conversation
Marked as draft, need to figure out what's wrong with the tests since I don't recall any product changes. I'll run IL diff tomorrow on before & after to make sure there is no incidental product changes |
The failures I was seeing were related to dotnet/csharplang#3393 - I've submitted the workaround |
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.
Submitting my thoughts so far.
I am currently at XmlSerializationReader, will continue reviewing tomorrow.
src/libraries/System.Private.Xml/src/System/Xml/Schema/SchemaNamespacemanager.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchema.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/CodeIdentifier.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSchemaImporter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSchemaImporter.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSchemaImporter.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSchemas.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSchemas.cs
Show resolved
Hide resolved
private Hashtable _callbacks; | ||
private Hashtable _types; | ||
private Hashtable _typesReverse; | ||
private XmlReader _r = null!; |
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.
Why is this non-nullable? Based on the following snippet:
class MyXmlReader : XmlSerializationReader
{
protected override void InitCallbacks()
{
throw new NotImplementedException();
}
protected override void InitIDs()
{
throw new NotImplementedException();
}
}
You can access the Reader
property on a MyXmlReader
instance and see that is null
.
Even though documentation says that you should not derive from it, I think is a good thing to consider https://docs.microsoft.com/dotnet/api/system.xml.serialization.xmlserializationreader?view=net-5.0
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.
The reason this is non-null is because code is not expecting it to be null. You will get NRE on many methods. for derived classes you're supposed to call the Init method which initializes it with non-null but as you said it is not meant to be derived by users.
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.
You will get NRE on many methods. for derived classes you're supposed to call the Init method which initializes it with non-null
I think is fine for this to be nullable if it will be null
until Init()
is called, that can make users aware of needing to call the method to avoid NullReferenceException
s.
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.
at this point this will only cause lots of !
in the code for no much benefit. it's private so derived classes won't see the annotation
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.
It's private but it is exposed through the Reader
property which is protected.
OK, since it's easier to handle as not-null it the code and this is not meant to be used by real world users, you can keep it as non-nullable.
src/libraries/System.Private.Xml/src/System/Xml/Serialization/SourceInfo.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/TypeExtensions.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationGeneratedCode.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationGeneratedCode.cs
Outdated
Show resolved
Hide resolved
|
||
public XmlSchema this[int index] | ||
{ | ||
get { return (XmlSchema)List[index]; } | ||
get { return (XmlSchema)List[index]!; } |
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.
If you're going to make the return type nullable, you'd also have to remove the !
here I think
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.
See my comment #41261 (comment)
@@ -1538,11 +1539,12 @@ public bool TryGetValue(string key, out LocalBuilder value) | |||
} | |||
} | |||
|
|||
public LocalBuilder this[string key] | |||
[DisallowNull] |
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.
The setter doesn't have an explicit null check here right? Is the DisallowNull
attribute still correct?
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.
yes, if you assign null here then _locals
second generic arg will be nullable which will cause chain reaction everywhere as nothing expects that to be null... This is an internal class so not a good tradeoff 😄
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationILGen.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Outdated
Show resolved
Hide resolved
@@ -701,7 +702,7 @@ public static string GetXmlSerializerAssemblyName(Type type) | |||
return GetXmlSerializerAssemblyName(type, null); | |||
} | |||
|
|||
public static string GetXmlSerializerAssemblyName(Type type, string defaultNamespace) | |||
public static string GetXmlSerializerAssemblyName(Type type, string? defaultNamespace) | |||
{ | |||
if (type == null) | |||
{ |
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.
on line 714: can events be nullable? cc @safern
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.
not sure, I think Handler += null
would be a bit weird though 😄
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.
The concern is not so much about the add
and remove
methods but because the underlying XmlNodeEventHandler
can be null
, but I think that _events.OnUnknownNode
nullability doesn't have to reflect on the public event so this is fine as is.
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlTypeAttribute.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
Outdated
Show resolved
Hide resolved
@@ -1094,7 +1104,7 @@ internal SerializableMapping(XmlQualifiedName xsiType, XmlSchemaSet schemas) | |||
_needSchema = false; | |||
} | |||
|
|||
internal void SetBaseMapping(SerializableMapping mapping) | |||
internal void SetBaseMapping(SerializableMapping? mapping) |
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.
I think mapping
should not be nullable
here? Otherwise https://github.com/dotnet/runtime/pull/41261/files#diff-81667efb15f99a82497ac6bfe0ae7ea5R492 will be a NRE?
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.
Hmm that link didn't lead me anywhere.
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.
Ah, bad link. Try this instead: https://github.com/dotnet/runtime/pull/41261/files?file-filters%5B%5D=#diff-81667efb15f99a82497ac6bfe0ae7ea5R492. Although, now when I see it again, it is not a bug, so this change is good.
Edit: That link is also no good. It's line 492 in Mappings.cs.
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.
LGTM!
Thank you for review! |
* Nullable: System.Xml, part 7 (Serialization) * Fix build errors on Release (ifdef-ed code) * fix incidental product bug * address PR feedback * Make XmlSerializer._rootType nullable
This almost completes System.Xml with 1 more tiny PR left which will enable annotations globally and add annotations to 2 leftover files and disable annotations where we've decided not to add them (XSLT runtime and tt template files).