From dc1c230b3d21c1fdbfa1e4193dec7442260ae3c9 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 9 Feb 2017 15:07:31 +0100 Subject: [PATCH] Disabling support for size checks when path contains [*] we'll need to flatten the array before checking the size. That logic will end up in our custom assertions. related to #217 --- .../verifier/builder/MethodBodyBuilder.groovy | 5 +++ ...vcMethodBodyBuilderWithMatchersSpec.groovy | 43 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/MethodBodyBuilder.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/MethodBodyBuilder.groovy index 8a1e21a7ff..95025d5d28 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/MethodBodyBuilder.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/MethodBodyBuilder.groovy @@ -328,6 +328,11 @@ abstract class MethodBodyBuilder { } else { Object elementFromBody = value(copiedBody, it) if (it.minTypeOccurrence() != null || it.maxTypeOccurrence() != null) { + if (it.path().contains("[*]")) { + throw new UnsupportedOperationException("Version 1.0.x doesn't support checking sizes when JSON Path contains [*]. " + + "For more information check out https://github.com/spring-cloud/spring-cloud-contract/issues/217 . " + + "Please upgrade to the latest version of Spring Cloud Contract for this feature.") + } checkType(bb, it, elementFromBody) String method = "assertThat(parsedJson.read(${quotedAndEscaped(it.path())}, java.util.Collection.class).size()).${sizeCheckMethod(it)}" bb.addLine(postProcessJsonPathCall(method)) diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderWithMatchersSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderWithMatchersSpec.groovy index 08f66302f7..b5f6836329 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderWithMatchersSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/MockMvcMethodBodyBuilderWithMatchersSpec.groovy @@ -204,13 +204,12 @@ class MockMvcMethodBodyBuilderWithMatchersSpec extends Specification implements ]) testMatchers { jsonPath('$.phoneNumbers', byType { - minOccurrence(0) // min occurrence of 1 - maxOccurrence(4) // max occurrence of 3 + minOccurrence(0) // min occurrence of 0 + maxOccurrence(4) // max occurrence of 4 }) jsonPath('$.phoneNumbers[*].number', byRegex("^[0-9]{3} [0-9]{3}-[0-9]{4}\$")) jsonPath('$..number', byRegex("^[0-9]{3} [0-9]{3}-[0-9]{4}\$")) } - headers { contentType('application/json') } @@ -242,4 +241,42 @@ class MockMvcMethodBodyBuilderWithMatchersSpec extends Specification implements "JaxRsClientJUnitMethodBodyBuilder" | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties) } | '$' } + @Issue('#217') + def "should not allow matcher with jsonpath containing [*] for [#methodBuilderName]"() { + given: + Contract contractDsl = Contract.make { + request { + method 'GET' + url 'person' + } + response { + status 200 + body([ + "phoneNumbers": [ + number: "foo" + ] + ]) + testMatchers { + jsonPath('$.phoneNumbers[*].number', byType { + minOccurrence(0) // min occurrence of 0 + maxOccurrence(4) // max occurrence of 4 + }) + } + } + } + MethodBodyBuilder builder = methodBuilder(contractDsl) + BlockBuilder blockBuilder = new BlockBuilder(" ") + when: + builder.appendTo(blockBuilder) + then: + UnsupportedOperationException e = thrown(UnsupportedOperationException) + e.message.contains("Version 1.0.x doesn't support checking sizes when JSON Path contains [*]") + where: + methodBuilderName | methodBuilder | rootElement + "MockMvcSpockMethodBuilder" | { Contract dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | '\\$' + "MockMvcJUnitMethodBuilder" | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties) } | '$' + "JaxRsClientSpockMethodRequestProcessingBodyBuilder" | { Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | '\\$' + "JaxRsClientJUnitMethodBodyBuilder" | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties) } | '$' + } + }