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

Remove DisallowNull annotation in JsonConverter.Write() #35022

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public abstract partial class JsonConverter<T> : System.Text.Json.Serialization.
protected internal JsonConverter() { }
public override bool CanConvert(System.Type typeToConvert) { throw null; }
public abstract T Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options);
public abstract void Write(System.Text.Json.Utf8JsonWriter writer, [System.Diagnostics.CodeAnalysis.DisallowNull] T value, System.Text.Json.JsonSerializerOptions options);
public abstract void Write(System.Text.Json.Utf8JsonWriter writer, T value, System.Text.Json.JsonSerializerOptions options);
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure we want to go ahead with this change without the accompanying #34439?

See #32186 (comment)

Would existing converters that don't currently check for null input need to be fixed up to handle that?

Our internal converts pass null (String converter for one)

The StringConverter is explicitly annotated to allow nullable string. Do we have other examples?

Copy link
Member Author

@steveharter steveharter Apr 17, 2020

Choose a reason for hiding this comment

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

Are we sure we want to go ahead with this change without the accompanying #34439?

I think so. Even without that, the String converter gets passed null (any internal converter that uses a reference type). Does it make sense to have DisallowNull since we override the behavior internally?

Also, for converter composition where one converter calls another converter there may be a desire to pass null as the value -- especially if the target converter does something special with null.

Would existing converters that don't currently check for null input need to be fixed up to handle that?

There is no behavior change with the PR. When #34439 is implemented, null will not be passed to any converters (except internal) unless they opt-in through the new virtual method.

}
[System.AttributeUsageAttribute(System.AttributeTargets.Constructor, AllowMultiple = false)]
public sealed partial class JsonConstructorAttribute : System.Text.Json.Serialization.JsonAttribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
// Fast path that avoids validation and extra indirection.
for (; index < array.Length; index++)
{
// TODO: https://github.com/dotnet/runtime/issues/32523
elementConverter.Write(writer, array[index]!, options);
elementConverter.Write(writer, array[index], options);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ protected internal override bool OnWriteResume(
{
string key = GetKeyName(enumerator.Current.Key, ref state, options);
writer.WritePropertyName(key);
// TODO: https://github.com/dotnet/runtime/issues/32523
converter.Write(writer, enumerator.Current.Value!, options);
converter.Write(writer, enumerator.Current.Value, options);
} while (enumerator.MoveNext());
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value,
// Fast path that avoids validation and extra indirection.
for (; index < list.Count; index++)
{
// TODO: https://github.com/dotnet/runtime/issues/32523
elementConverter.Write(writer, list[index]!, options);
elementConverter.Write(writer, list[index], options);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ internal sealed override bool TryWriteAsObject(Utf8JsonWriter writer, object? va
// Provide a default implementation for value converters.
internal virtual bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
{
// TODO: https://github.com/dotnet/runtime/issues/32523
Write(writer, value!, options);
Write(writer, value, options);
return true;
}

Expand Down Expand Up @@ -254,8 +253,7 @@ internal bool TryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions opt

int originalPropertyDepth = writer.CurrentDepth;

// TODO: https://github.com/dotnet/runtime/issues/32523
Write(writer, value!, options);
Write(writer, value, options);
VerifyWrite(originalPropertyDepth, writer);

return true;
Expand Down Expand Up @@ -409,6 +407,6 @@ internal void VerifyWrite(int originalDepth, Utf8JsonWriter writer)
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write to.</param>
/// <param name="value">The value to convert.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
public abstract void Write(Utf8JsonWriter writer, [DisallowNull] T value, JsonSerializerOptions options);
public abstract void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options);
Copy link
Member

@stephentoub stephentoub Apr 15, 2020

Choose a reason for hiding this comment

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

Do we have tests that pass in null successfully?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. It'd be good to have null tests for both value types and reference types.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have tests that pass in null successfully?

Null strings use this path.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there any special guidance for using these attributes on abstract or non-sealed types\members? A derived class, for example, may want\need different behavior.

Is it OK to place [DisallowNull] on an abstract base class but then still allow null and use a nullable Type (like we do in the String converter).

}
}