Skip to content
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

Merged
merged 5 commits into from
Aug 27, 2020
Merged

Conversation

krwq
Copy link
Member

@krwq krwq commented Aug 24, 2020

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).

@krwq krwq requested review from jozkee and a team August 24, 2020 13:18
@ghost
Copy link

ghost commented Aug 24, 2020

Tagging subscribers to this area: @buyaa-n, @krwq
See info in area-owners.md if you want to be subscribed.

@krwq krwq marked this pull request as draft August 24, 2020 20:39
@krwq
Copy link
Member Author

krwq commented Aug 24, 2020

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

@krwq
Copy link
Member Author

krwq commented Aug 25, 2020

The failures I was seeing were related to dotnet/csharplang#3393 - I've submitted the workaround

@krwq krwq marked this pull request as ready for review August 25, 2020 05:04
Copy link
Member

@jozkee jozkee left a 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.

private Hashtable _callbacks;
private Hashtable _types;
private Hashtable _typesReverse;
private XmlReader _r = null!;
Copy link
Member

@jozkee jozkee Aug 25, 2020

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

Copy link
Member Author

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.

Copy link
Member

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 NullReferenceExceptions.

Copy link
Member Author

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

Copy link
Member

@jozkee jozkee Aug 26, 2020

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.


public XmlSchema this[int index]
{
get { return (XmlSchema)List[index]; }
get { return (XmlSchema)List[index]!; }
Copy link

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

Copy link
Member Author

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]
Copy link

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?

Copy link
Member Author

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 😄

@@ -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)
{
Copy link
Member

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

Copy link
Member Author

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 😄

Copy link
Member

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.

@@ -1094,7 +1104,7 @@ internal SerializableMapping(XmlQualifiedName xsiType, XmlSchemaSet schemas)
_needSchema = false;
}

internal void SetBaseMapping(SerializableMapping mapping)
internal void SetBaseMapping(SerializableMapping? mapping)
Copy link

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?

Copy link
Member

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.

Copy link

@pgovind pgovind Aug 27, 2020

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.

Copy link

@pgovind pgovind left a comment

Choose a reason for hiding this comment

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

LGTM!

@krwq
Copy link
Member Author

krwq commented Aug 27, 2020

Thank you for review!

@krwq krwq merged commit 53f4c08 into dotnet:master Aug 27, 2020
carlossanlop pushed a commit to carlossanlop/runtime that referenced this pull request Aug 28, 2020
* 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
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants