RehanSaeed / Schema.NET

Schema.org objects turned into strongly typed C# POCO classes for use in .NET. All classes can be serialized into JSON/JSON-LD and XML, typically used to represent structured data in the head section of html page.
MIT License
640 stars 80 forks source link

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

Closed SmithPlatts closed 8 months ago

SmithPlatts commented 8 months ago

Describe the bug

When the Values(params object[] items) or Values(IEnumerable<object?> items) constructors are used, and:

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