Skip to content

Conversation

@steveharter
Copy link
Contributor

Adds tests to cover #31464.

Previously the polymorphic tests only focused on the string-based serialization methods (not Stream, byte[] and Writer scenarios) and the issue above occurred during Stream scenarios.

The actual issue was recently fixed with #32669.

The "SerializationWrapper" helper class that was added here should be used in the future for other cases to increase code coverage, especially for Stream cases. At that time we will likely also add a "DeserializationWrapper" -- that deserialization wrapper wasn't added here since polymorphism is currently only supported during serialization.

@steveharter steveharter added area-System.Text.Json increase-code-coverage Tracking need to add more test and increase code coverage of a component labels Mar 9, 2020
@steveharter steveharter requested review from jozkee and layomia March 9, 2020 18:29
@steveharter steveharter requested a review from ahsonkhan as a code owner March 9, 2020 18:29
@steveharter steveharter self-assigned this Mar 9, 2020
@layomia layomia added this to the 5.0 milestone Mar 10, 2020
Copy link
Contributor

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

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

Nice test additions.


public abstract class PolymorphicTests
{
private SerializationWrapper Serializer { get; }
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use a more verbose name rather than the general Serializer (helps with future find/replace refactorings).

Something like: CustomAllSerializer

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe SerializeMethodOverload.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can still search for "Serializer." via whole word, same case. i.e. it doesn't collide with "JsonSerializer."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the tests look good like this (maybe a bit confusing for first 5 seconds however)

json = Serializer.Serialize<object>(array);

versus

json = CustomAllSerializer.Serialize<object>(array);

or

json = Serializer.SerializeMethodOverload<object>(array);

var value = new { x = 1, y = true };

// Strongly-typed.
string json = Serializer.Serialize(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice test. Could this apply on deserialization too? What about using value.GetType() to call the non-generic method?

Copy link
Contributor

@layomia layomia Mar 13, 2020

Choose a reason for hiding this comment

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

This is not supported as anonymous types don't have parameterless ctors (only a single parameterized ctor).

This is an area to test in the ctors PR #33444, although these types would have to be special cased as they violate the requirement that ctor parameter names should be the camelCase equivalent of the property names. Instead, they are the same string i.e a property named Prop would map to a ctor parameter named Prop, while prop would map to prop.

namespace System.Text.Json.Serialization.Tests
{
public static class PolymorphicTests
public class PolymorphicTests_String : PolymorphicTests
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to also add the method overload permutations? Generic call vs one where we pass in type directly?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like all the string and Stream overloads are tested here.

@steveharter, how about adding the byte[] overloads as well (and return Encoding.UTF8.GetString(result) in the Serialize[<T>] overloads)?

@steveharter
Copy link
Contributor Author

@layomia @ahsonkhan any further changes requested? Otherwise please approve.

Copy link
Contributor

@layomia layomia left a comment

Choose a reason for hiding this comment

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

Great serializer method abstraction that'll be super useful for other test areas.

@steveharter steveharter merged commit b3a5e54 into dotnet:master Mar 13, 2020
@steveharter steveharter deleted the AsyncSyncBoxing branch March 13, 2020 22:12
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Text.Json increase-code-coverage Tracking need to add more test and increase code coverage of a component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants