Merge branch '2.0.x'
This commit is contained in:
@@ -84,8 +84,11 @@ class JsonToJsonPathsConverter {
|
|||||||
if (bodyMatchers?.hasMatchers()) {
|
if (bodyMatchers?.hasMatchers()) {
|
||||||
bodyMatchers.matchers().each { BodyMatcher matcher ->
|
bodyMatchers.matchers().each { BodyMatcher matcher ->
|
||||||
try {
|
try {
|
||||||
context.delete(matcher.path())
|
def entry = entry(context, matcher.path())
|
||||||
removeTrailingContainers(matcher, context)
|
if (entry != null) {
|
||||||
|
context.delete(matcher.path())
|
||||||
|
removeTrailingContainers(matcher, context)
|
||||||
|
}
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Exception occurred while trying to delete path [${matcher.path()}]", e)
|
log.debug("Exception occurred while trying to delete path [${matcher.path()}]", e)
|
||||||
@@ -96,6 +99,17 @@ class JsonToJsonPathsConverter {
|
|||||||
return jsonCopy
|
return jsonCopy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static def entry(DocumentContext context, String path) {
|
||||||
|
try {
|
||||||
|
return context.read(path)
|
||||||
|
} catch (Exception ex) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Exception occurred while trying to retrieve element via path [${path}]", ex)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the value from JSON via json path
|
* Retrieves the value from JSON via json path
|
||||||
*
|
*
|
||||||
@@ -118,22 +132,26 @@ class JsonToJsonPathsConverter {
|
|||||||
* defining body...
|
* defining body...
|
||||||
*/
|
*/
|
||||||
private static void removeTrailingContainers(BodyMatcher matcher, DocumentContext context) {
|
private static void removeTrailingContainers(BodyMatcher matcher, DocumentContext context) {
|
||||||
if (matcher.path().contains(ANY_ARRAY_NOTATION_IN_JSONPATH)) {
|
String pathWithoutAnyArray = matcher.path().contains(ANY_ARRAY_NOTATION_IN_JSONPATH) ? matcher.path().substring(0, matcher.path().lastIndexOf(ANY_ARRAY_NOTATION_IN_JSONPATH)) : matcher.path()
|
||||||
String pathWithoutAnyArray = matcher.path().substring(0, matcher.path().lastIndexOf(ANY_ARRAY_NOTATION_IN_JSONPATH))
|
def object = entry(context, pathWithoutAnyArray)
|
||||||
def object = context.read(pathWithoutAnyArray)
|
// object got removed and it was the only element
|
||||||
if (object instanceof Iterable && containsOnlyEmptyElements(object)) {
|
// let's get its parent and see if it contains an empty element
|
||||||
String pathToDelete = pathToDelete(pathWithoutAnyArray)
|
if (isIterable(object) && containsOnlyEmptyElements(object)) {
|
||||||
context.delete(pathToDelete)
|
String pathToDelete = pathToDelete(pathWithoutAnyArray)
|
||||||
} else {
|
context.delete(pathToDelete)
|
||||||
String lastParent = matcher.path().substring(0, matcher.path().lastIndexOf("."))
|
} else {
|
||||||
def lastParentObject = context.read(lastParent)
|
String lastParent = matcher.path().substring(0, matcher.path().lastIndexOf("."))
|
||||||
if (lastParentObject instanceof Iterable && containsOnlyEmptyElements(lastParentObject)) {
|
def lastParentObject = context.read(lastParent)
|
||||||
context.delete(lastParent)
|
if (isIterable(lastParentObject) && containsOnlyEmptyElements(lastParentObject)) {
|
||||||
}
|
context.delete(lastParent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isIterable(Object object) {
|
||||||
|
return object instanceof Iterable || object instanceof Map
|
||||||
|
}
|
||||||
|
|
||||||
private static String pathToDelete(String pathWithoutAnyArray) {
|
private static String pathToDelete(String pathWithoutAnyArray) {
|
||||||
// we can't remove root
|
// we can't remove root
|
||||||
return pathWithoutAnyArray == '$' ? '$[*]' : pathWithoutAnyArray
|
return pathWithoutAnyArray == '$' ? '$[*]' : pathWithoutAnyArray
|
||||||
|
|||||||
@@ -1174,4 +1174,49 @@ DocumentContext parsedJson = JsonPath.parse(json);
|
|||||||
"JaxRsClientJUnitMethodBodyBuilder" | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) }
|
"JaxRsClientJUnitMethodBodyBuilder" | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties, classDataForMethod) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Issue("#727")
|
||||||
|
def "should not leave empty arrays [#methodBuilderName]"() {
|
||||||
|
given:
|
||||||
|
Contract contractDsl = Contract.make {
|
||||||
|
request {
|
||||||
|
method 'GET'
|
||||||
|
url '/list'
|
||||||
|
}
|
||||||
|
response {
|
||||||
|
status 200
|
||||||
|
body(
|
||||||
|
[
|
||||||
|
content: [
|
||||||
|
one: "two",
|
||||||
|
two: "two",
|
||||||
|
three: [
|
||||||
|
six: "seven"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
bodyMatchers {
|
||||||
|
jsonPath('$.content.three.six', byRegex(".*seven.*"))
|
||||||
|
jsonPath('$.content.one', byRegex(".*two.*"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MethodBodyBuilder builder = methodBuilder(contractDsl)
|
||||||
|
BlockBuilder blockBuilder = new BlockBuilder(" ")
|
||||||
|
when:
|
||||||
|
builder.appendTo(blockBuilder)
|
||||||
|
then:
|
||||||
|
String test = blockBuilder.toString()
|
||||||
|
SyntaxChecker.tryToCompileWithoutCompileStatic(methodBuilderName, test)
|
||||||
|
!test.contains('''.isEmpty()''')
|
||||||
|
and:
|
||||||
|
stubMappingIsValidWireMockStub(contractDsl)
|
||||||
|
where:
|
||||||
|
methodBuilderName | methodBuilder
|
||||||
|
"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) }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user