diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/converter/ContractsToYaml.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/converter/ContractsToYaml.java index 74fc841024..3b4499d40c 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/converter/ContractsToYaml.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/converter/ContractsToYaml.java @@ -176,27 +176,19 @@ else if (value instanceof RegexProperty || value instanceof Pattern) { } private void mapRequestMatchersUrl(YamlContract.Request yamlContractRequest, Request request) { - Object url = Optional.ofNullable(request.getUrl()).map(Url::getClientValue).orElse(null); + Object url = Optional.ofNullable(request.getUrl()).map(Url::getClientValue) + .orElse(Optional.ofNullable(request.getUrlPath()).map(Url::getClientValue).orElse(null)); YamlContract.KeyValueMatcher keyValueMatcher = new YamlContract.KeyValueMatcher(); if (url instanceof RegexProperty) { keyValueMatcher.regex = ((RegexProperty) url).pattern(); yamlContractRequest.matchers.url = keyValueMatcher; } - else if (url instanceof ExecutionProperty) { - keyValueMatcher.command = url.toString(); + else if (url instanceof Pattern) { + keyValueMatcher.regex = ((Pattern) url).pattern(); yamlContractRequest.matchers.url = keyValueMatcher; } - else { - yamlContractRequest.matchers.url = null; - } - - Object urlPath = Optional.ofNullable(request.getUrlPath()).map(Url::getClientValue).orElse(null); - if (urlPath instanceof RegexProperty) { - keyValueMatcher.regex = ((RegexProperty) urlPath).pattern(); - yamlContractRequest.matchers.url = keyValueMatcher; - } - else if (urlPath instanceof ExecutionProperty) { - keyValueMatcher.command = urlPath.toString(); + else if (url instanceof ExecutionProperty) { + keyValueMatcher.command = url.toString(); yamlContractRequest.matchers.url = keyValueMatcher; } else { @@ -557,7 +549,7 @@ protected YamlContract.TestMatcherType testMatcherType(MatchingType matchingType protected YamlContract.StubMatcherType stubMatcherType(MatchingType matchingType) { if (matchingType == MatchingType.COMMAND || matchingType == MatchingType.TYPE) { - throw new UnsupportedOperationException("No type or command for client side"); + return null; } return STUB_MATCHER_TYPE.getOrDefault(matchingType, null); } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy index 7909e89faf..fcb29e51f4 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/converter/YamlContractConverterSpec.groovy @@ -389,6 +389,41 @@ class YamlContractConverterSpec extends Specification { urlPropertyName << ['url', 'urlPath'] } + @Issue('#1921') + def 'should convert YAML to Contract and back to YAML without losing url information'() { + given: + File ymlMatchers = File.createTempFile('contract_matchers_url', '.yml').with { + write YamlContractConverterSpec.getResource('/yml/contract_matchers_url.yml') + .text + return it + } + expect: + converter.isAccepted(ymlMatchers) + when: + Collection contracts = converter.convertFrom(ymlMatchers) + List convertedBack = converter.convertTo(contracts) + then: + convertedBack.size() == 1 + convertedBack[0].request.matchers.url.regex == "/get/[0-9]" + } + + @Issue('#1921') + def 'should convert YAML to Contract and back to YAML without exceptions'() { + given: + File ymlMatchers = File.createTempFile('contract_matchers', '.yml').with { + write YamlContractConverterSpec.getResource('/yml/contract_matchers.yml') + .text + return it + } + expect: + converter.isAccepted(ymlMatchers) + when: + Collection contracts = converter.convertFrom(ymlMatchers) + List convertedBack = converter.convertTo(contracts) + then: + convertedBack.size() == 1 + } + protected Object assertQueryParam(QueryParameters queryParameters, String queryParamName, Object serverValue, MatchingStrategy.Type clientType, Object clientValue) { if (clientType == MatchingStrategy.Type.ABSENT) { diff --git a/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers_url.yml b/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers_url.yml new file mode 100644 index 0000000000..c36bdbb7fb --- /dev/null +++ b/spring-cloud-contract-verifier/src/test/resources/yml/contract_matchers_url.yml @@ -0,0 +1,8 @@ +request: + method: GET + urlPath: /get/1 + matchers: + url: + regex: /get/[0-9] +response: + status: 200 \ No newline at end of file