spring-cloud-contract icon indicating copy to clipboard operation
spring-cloud-contract copied to clipboard

Improper validation when objects contains array of objects with array inside

Open mslowiak opened this issue 1 year ago • 5 comments

For a given contract:

package contracts.consumerApi

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    request {
        method 'GET'
        url '/test-get'
        headers {
            header('Content-Type', 'application/json')
        }
    }
    response {
        status OK()
        body(
    '''
                    {
                      "fields": [
                        {
                          "id": "1",
                          "options": []
                        },
                        {
                          "id": "2",
                          "options": [
                            {
                              "x": "x",
                              "y": "y"
                            }
                          ]
                        }
                      ]
                    }
               '''
        )
        headers {
            header('Content-Type', 'application/json')
        }
    }
}

The generated test looks like this:

DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).array("['fields']").contains("['id']").isEqualTo("1");
assertThatJson(parsedJson).array("['fields']").array("['options']").isEmpty();
assertThatJson(parsedJson).array("['fields']").contains("['id']").isEqualTo("2");
assertThatJson(parsedJson).array("['fields']").array("['options']").contains("['x']").isEqualTo("x");
assertThatJson(parsedJson).array("['fields']").array("['options']").contains("['y']").isEqualTo("y");

which causes assertion to fail because the first field object has empty options array, and the second one has one element in options array

mslowiak avatar Oct 07 '22 18:10 mslowiak

@marcingrzejszczak @OlgaMaciaszek Could you take a look into that?

mslowiak avatar Oct 28 '22 11:10 mslowiak

We will look into that asap, we're in the middle of various release processes atm.

marcingrzejszczak avatar Oct 28 '22 11:10 marcingrzejszczak

Great, thank you! 👍

mslowiak avatar Oct 28 '22 13:10 mslowiak

@marcingrzejszczak I am facing similar issues. Any news on when this is planned to be fixed? The workaround that seems to be working for now is to define that array element (fields[0].options) as a bodyMatcher by jsonpath with minOccurence 0

    byType{
        minOccurrence(0)
    }

Pietraas avatar Nov 30 '22 15:11 Pietraas

No deadlines just yet. We're currently preparing for 2022 release train

marcingrzejszczak avatar Nov 30 '22 15:11 marcingrzejszczak