Skip to content

A null or whitespace string passed into Values<T1, T2*> causes HasValueX to return true #675

Closed
@SmithPlatts

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 either null 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");
  1. Deserialise simple Reservation where totalPrice is an empty string.
  2. Test that Reservation.TotalPrice has no values for values property 3 (string).
  3. Test that Reservation.TotalPrice returns false for HasValue3 (optionally the same test for HasValue).
  4. Observe that InvalidDataException is thrown with the message totalPrice should return false for HasValue3.

Expected behaviour

Reservation.TotalPrice.HasValue3 should return false, not true.

Schema objects

  • All/any

Metadata

Assignees

No one assigned

    Labels

    bugIssues describing a bug or pull requests fixing a bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions