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 3b31e22139..3f9152c1bf 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 { @@ -578,7 +570,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 d59b7e8ed1..514e19c117 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 @@ -392,6 +392,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