Hi all, Is there a way to validate nullable refer...
# pact-net
b
Hi all, Is there a way to validate nullable reference types with Pact .NET? 🙂 If we have the following two classes:
Copy code
public class ConsumerClass 
{
    public string MyProperty { get; init; }
}

public class ProviderClass 
{
    public string? MyProperty { get; init; }
}
and in my consumer test, I've set the following:
.WithJsonBody(Match.Type(new ConsumerClass { MyProperty = "test" }));
(since I don't want to validate the actual values) I'd expect the provider test to fail this, since I have marked
MyProperty
to be a nullable property.
m
Matchers aren’t there to describe how your Provider works, they are there to describe the scenarios a consumer needs, which is then checked against a provider
In your case, it seems the consumer always needs the value. This might be true of the scenarios your consumer needs, but not of all scenarios of a given provider. This is how provider states become important. They can differentiate the scenarios. Pure schema checking doesn’t allow this
As Tim Jones eloquently puts it:
Think of the matchers as saying “this test covers all cases that pass this matcher”, not “this is the response schema”
If you need to test both scenarios, see howtooptional
b
Thanks a lot for your answers, Matt 🙂 Greatly appreciated. It totally makes sense. It's not so much the property itself that makes sense to test, but rather the type. If I mark the string as nullable and set it's value to null on provider side, while the consumer expects it to be a string, the provider tests fails. That's exactly what I was after, first hand.
🙌 1
m
Perfect!