Skip to content

Commit e08f13f

Browse files
wrapValueWithQuotes accepts nulls
without this change NPE is thrown fixes gh-2117
1 parent 56673bd commit e08f13f

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/util/DelegatingJsonVerifiable.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ private static String stringWithEscapedQuotes(Object object) {
6868
return stringValue.replaceAll("\"", "\\\\\"");
6969
}
7070

71-
private static String wrapValueWithQuotes(Object value) {
71+
static String wrapValueWithQuotes(Object value) {
72+
if (value == null) {
73+
return null;
74+
}
7275
return value instanceof String ? "\"" + stringWithEscapedQuotes(value) + "\"" : value.toString();
7376
}
7477

@@ -198,8 +201,8 @@ public MethodBufferingJsonVerifiable isEqualTo(Number value) {
198201
readyToCheck.methodsBuffer.offer(".value()");
199202
}
200203
else {
201-
readyToCheck.appendMethodWithValue("isEqualTo",
202-
value instanceof Long ? String.valueOf(value).concat("L") : String.valueOf(value));
204+
readyToCheck.appendMethodWithValue("isEqualTo", value instanceof Long ? String.valueOf(value).concat("L")
205+
: (value == null ? null : String.valueOf(value)));
203206
}
204207
return readyToCheck;
205208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.contract.verifier.util;
18+
19+
import org.assertj.core.api.BDDAssertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
class DelegatingJsonVerifiableTests {
23+
24+
@Test
25+
void shouldReturnNullValueForNull() {
26+
String value = DelegatingJsonVerifiable.wrapValueWithQuotes(null);
27+
28+
BDDAssertions.then(value).isNull();
29+
}
30+
31+
@Test
32+
void shouldReturnQuotedValueForString() {
33+
String value = DelegatingJsonVerifiable.wrapValueWithQuotes("hello");
34+
35+
BDDAssertions.then(value).isEqualTo("\"hello\"");
36+
}
37+
38+
@Test
39+
void shouldReturnStringValueForNonString() {
40+
String value = DelegatingJsonVerifiable.wrapValueWithQuotes(5);
41+
42+
BDDAssertions.then(value).isEqualTo("5");
43+
}
44+
45+
}

0 commit comments

Comments
 (0)