Fix improperly accessed optional values; ServerDslProperty incorrectly wrapped by OptionalProperty (#1809)

Fixes gh-1808
This commit is contained in:
Artem Makarov
2022-08-19 10:54:58 +02:00
committed by GitHub
parent 08bbdf1a2d
commit c847d1951e
4 changed files with 136 additions and 1 deletions

View File

@@ -54,6 +54,9 @@ public class OptionalProperty implements Serializable, CanBeDynamic {
else if (value instanceof ClientDslProperty) {
return valueToCheck(((ClientDslProperty) value).getClientValue());
}
else if (value instanceof ServerDslProperty) {
return valueToCheck(((ServerDslProperty) value).getServerValue());
}
else if (value instanceof RegexProperty || value instanceof Pattern) {
return new RegexProperty(value).pattern();
}

View File

@@ -322,7 +322,7 @@ class ContentUtils {
}
protected static Object transformJSONStringValue(OptionalProperty optional, Closure valueProvider) {
return String.format(JSON_VALUE_PATTERN_FOR_OPTIONAL, optional.value)
return String.format(JSON_VALUE_PATTERN_FOR_OPTIONAL, optional.value())
}
protected static Object transformJSONStringValue(ExecutionProperty property, Closure valueProvider) {

View File

@@ -2174,4 +2174,68 @@ response:
}
}
@Issue("1808")
def "should correctly process optional of DslProperty parameters"() {
given:
Contract contractDsl = Contract.make {
request {
method('GET')
url("/api/foo")
headers {
header 'Content-Type': 'application/json'
header 'Accept': 'application/json'
}
body([
key1: $(client(optional(anyOf("foo", "bar"))), server("bar")),
key2: $(client(optional(anyNonBlankString())), server("bar")),
key3: $(client(optional(anyEmail())), server("foo@bar.com")),
key4: $(optional(anyNumber()))
])
}
response {
status OK()
headers {
header 'Content-Type': 'application/json'
}
body([
key1: $(client("bar"), server(optional(anyOf("foo", "bar")))),
key2: $(client("bar"), server(optional(anyNonBlankString()))),
key3: $(client("foo@bar.com"), server(optional(anyEmail()))),
key4: $(optional(anyNumber()))
])
}
}
methodBuilder()
when:
String test = singleTestGenerator(contractDsl)
then:
SyntaxChecker.tryToCompile(methodBuilderName, test)
then:
test.contains('assertThatJson(parsedJson).field("[\'key1\']").matches("(^foo' + endOfLineRegexSymbol + '|^bar' + endOfLineRegexSymbol + ')?")')
test.contains('assertThatJson(parsedJson).field("[\'key2\']").matches("(^\\\\s*\\\\S[\\\\S\\\\s]*)?")')
test.contains('assertThatJson(parsedJson).field("[\'key3\']").matches("([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,6})?")')
test.contains('assertThatJson(parsedJson).field("[\'key4\']").matches("(-?(\\\\d*\\\\.\\\\d+|\\\\d+))?")')
and:
stubMappingIsValidWireMockStub(contractDsl)
where:
methodBuilderName | methodBuilder | endOfLineRegexSymbol
"spock" | {
properties.testFramework = TestFramework.SPOCK
} | '\\$'
"mockmvc" | {
properties.testMode = TestMode.MOCKMVC
} | '\$'
"jaxrs-spock" | {
properties.testFramework = TestFramework.SPOCK; properties.testMode = TestMode.JAXRSCLIENT
} | '\\$'
"jaxrs" | {
properties.testFramework = TestFramework.JUNIT; properties.testMode = TestMode.JAXRSCLIENT
} | '\$'
"webclient" | {
properties.testMode = TestMode.WEBTESTCLIENT
} | '\$'
"testNG" | {
properties.testFramework = TestFramework.TESTNG
} | '\$'
}
}

View File

@@ -3130,6 +3130,74 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
stubMappingIsValidWireMockStub(wireMockStub)
}
@Issue("1808")
def "should correctly process optional of DslProperty parameters"() {
given:
org.springframework.cloud.contract.spec.Contract groovyDsl = org.springframework.cloud.contract.spec.Contract.
make {
request {
method('GET')
url("/api/foo")
headers {
header("Content-Type", "application/json")
header("Accept", "application/json")
}
body(
key1: $(client(optional(anyOf("foo", "bar"))), server("bar")),
key2: $(client(optional(anyNonBlankString())), server("bar")),
key3: $(client(optional(anyEmail())), server("foo@bar.com")),
key4: $(optional(anyNumber())),
)
}
response {
status OK()
body("ok")
headers {
header 'Content-Type': 'text/plain'
}
}
}
when:
String wireMockStub = new WireMockStubStrategy("Test", new ContractMetadata(null, false, 0, null, groovyDsl), groovyDsl).
toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
"request" : {
"url" : "/api/foo",
"method" : "GET",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
},
"Accept" : {
"equalTo" : "application/json"
}
},
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(@.['key1'] =~ /(^foo$|^bar$)?/)]"
}, {
"matchesJsonPath" : "$[?(@.['key2'] =~ /(^\\\\s*\\\\S[\\\\S\\\\s]*)?/)]"
}, {
"matchesJsonPath" : "$[?(@.['key3'] =~ /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,6})?/)]"
}, {
"matchesJsonPath" : "$[?(@.['key4'] =~ /(-?(\\\\d*\\\\.\\\\d+|\\\\d+))?/)]"
} ]
},
"response" : {
"status" : 200,
"body" : "ok",
"headers" : {
"Content-Type" : "text/plain"
},
"transformers" : [ "response-template", "foo-transformer" ]
}
}
''', wireMockStub)
and:
stubMappingIsValidWireMockStub(wireMockStub)
}
WireMockConfiguration config() {
return new WireMockConfiguration().extensions(responseTemplateTransformer())
}