A null or whitespace string passed into Values<T1, T2*> causes HasValueX to return true #675
Closed
Description
Describe the bug
When the Values(params object[] items)
or Values(IEnumerable<object?> items)
constructors are used, and:
- when the target
Tn
is a string and the value is eithernull
or a whitespace string; or - when the target
Tn
is a nullable type; then
The constructor logic will evaluate HasValueX
before assigning to ValueX
, where ValueX
returns an empty collection post-OneOrMany<Tn>
cast.
Steps to reproduce
A simple reproducible sample can be achieved using https://schema.org/Reservation:
string json = """
{
"@context": "https://schema.org",
"@type": "Reservation",
"reservationId": "1",
"totalPrice": ""
}
""";
Schema.NET.Reservation reservation = Schema.NET.SchemaSerializer.DeserializeObject<Schema.NET.Reservation>(json)!;
_ = reservation.TotalPrice.Value3.Count == 0 ? "Correct, totalPrice should be an empty collection" : throw new InvalidDataException("totalPrice should be an empty collection");
_ = !reservation.TotalPrice.HasValue3 ? "Correct, totalPrice should not have any values" : throw new InvalidDataException("totalPrice should return false for HasValue3");
- Deserialise simple
Reservation
wheretotalPrice
is an empty string. - Test that
Reservation.TotalPrice
has no values for values property 3 (string
). - Test that
Reservation.TotalPrice
returnsfalse
forHasValue3
(optionally the same test forHasValue
). - Observe that
InvalidDataException
is thrown with the messagetotalPrice should return false for HasValue3
.
Expected behaviour
Reservation.TotalPrice.HasValue3
should return false
, not true
.
Schema objects
- All/any