spring-cloud / spring-cloud-contract

Support for Consumer Driven Contracts in Spring
https://cloud.spring.io/spring-cloud-contract
Apache License 2.0
720 stars 439 forks source link

wrapValueWithQuotes throws NullPointer when value is null (Cannot invoke "Object.toString()" because "value" is null) #2117

Closed fireplace009 closed 4 months ago

fireplace009 commented 5 months ago

Java 21 <spring-cloud-contract.version>4.1.3</spring-cloud-contract.version>

Describe the bug

When running the maven plugin, my contract.yaml has a bodyFromFile value. This file is a .json containing (amongst other things) an array with values. One of these value can be null

Sample

{
  "rows": [
    {
      "values": [
        "2023-10-19 08:39:55",
        "B",
        "N",
        "",
        "100",
        "01",
        1,
        1369,
        "100",
        null,
        null,
        null
      ],
  ]
}

In the class DelegatingJsonVerifiable this method:

    private static String wrapValueWithQuotes(Object value) {
        return value instanceof String ? "\"" + stringWithEscapedQuotes(value) + "\"" : value.toString();
    }

does not take into account that the value can be null - (value instanceof String is false => value.toString throws NPE

fireplace009 commented 4 months ago

Awesome !!! Thank you

marcingrzejszczak commented 4 months ago

Thank you for filing the issue :)