Skip to content

Commit d6ba346

Browse files
committed
Add failing tests for #2066
1 parent 0afc822 commit d6ba346

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ public Class<?> getValueClass() {
422422
}
423423
}
424424

425-
426425
/**
427426
* Delegating {@link ValueInstantiator} implementation meant as a base type
428427
* that by default delegates methods to specified fallback instantiator.
@@ -541,7 +540,7 @@ public Object createFromDouble(DeserializationContext ctxt, double value) throws
541540
@Override
542541
public Object createFromBoolean(DeserializationContext ctxt, boolean value) throws IOException {
543542
return delegate().createFromBoolean(ctxt, value);
544-
}
543+
}
545544

546545
/*
547546
/**********************************************************

src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypesTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ public void testURL() throws Exception
303303

304304
public void testUUID() throws Exception
305305
{
306-
final ObjectMapper mapper = objectMapper();
307-
308306
final String NULL_UUID = "00000000-0000-0000-0000-000000000000";
307+
final ObjectReader r = MAPPER.readerFor(UUID.class);
308+
309309
// first, couple of generated UUIDs:
310310
for (String value : new String[] {
311311
"76e6d183-5f68-4afa-b94a-922c1fdb83f8",
@@ -315,12 +315,10 @@ public void testUUID() throws Exception
315315
"82994ac2-7b23-49f2-8cc5-e24cf6ed77be",
316316
"00000007-0000-0000-0000-000000000000"
317317
}) {
318-
319-
mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
320-
321318
UUID uuid = UUID.fromString(value);
322319
assertEquals(uuid,
323-
mapper.readValue(quote(value), UUID.class));
320+
r.without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
321+
.readValue(quote(value)));
324322
}
325323
// then use templating; note that these are not exactly valid UUIDs
326324
// wrt spec (type bits etc), but JDK UUID should deal ok
@@ -330,13 +328,13 @@ public void testUUID() throws Exception
330328
for (int i = 0; i < chars.length(); ++i) {
331329
String value = TEMPL.replace('0', chars.charAt(i));
332330
assertEquals(UUID.fromString(value).toString(),
333-
mapper.readValue(quote(value), UUID.class).toString());
331+
r.readValue(quote(value)).toString());
334332
}
335333

336334
// also: see if base64 encoding works as expected
337335
String base64 = Base64Variants.getDefaultVariant().encode(new byte[16]);
338336
assertEquals(UUID.fromString(NULL_UUID),
339-
mapper.readValue(quote(base64), UUID.class));
337+
r.readValue(quote(base64)));
340338
}
341339

342340
public void testUUIDInvalid() throws Exception
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.UUID;
4+
5+
import com.fasterxml.jackson.annotation.JsonFormat;
6+
import com.fasterxml.jackson.annotation.OptBoolean;
7+
8+
import com.fasterxml.jackson.databind.BaseMapTest;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
11+
public class JDKStringLikeTypes2066Test extends BaseMapTest
12+
{
13+
static class UUIDWrapper {
14+
public UUID value;
15+
}
16+
17+
static class StrictUUIDWrapper {
18+
@JsonFormat(lenient=OptBoolean.FALSE)
19+
public UUID value;
20+
}
21+
22+
/*
23+
/**********************************************************
24+
/* Test methods
25+
/**********************************************************
26+
*/
27+
28+
// [databind#2066]
29+
public void testUUIDLeniencyDefault() throws Exception
30+
{
31+
final ObjectMapper MAPPER = objectMapper();
32+
33+
// By default, empty String OK
34+
assertNull(MAPPER.readValue(quote(""), UUID.class));
35+
36+
UUIDWrapper w = MAPPER.readValue("{\"value\":\"\"}", UUIDWrapper.class);
37+
assertNull(w.value);
38+
}
39+
40+
// [databind#2066]
41+
public void testUUIDLeniencyGlobal() throws Exception
42+
{
43+
final ObjectMapper STRICT_MAPPER = jsonMapperBuilder()
44+
.defaultLeniency(Boolean.FALSE)
45+
.build();
46+
try {
47+
STRICT_MAPPER.readValue(quote(""), UUID.class);
48+
fail("Should not pass");
49+
} catch (Exception e) {
50+
_verifyBadUUID(e);
51+
}
52+
53+
try {
54+
STRICT_MAPPER.readValue("{\"value\":\"\"}", UUIDWrapper.class);
55+
fail("Should not pass");
56+
} catch (Exception e) {
57+
_verifyBadUUID(e);
58+
}
59+
}
60+
61+
// [databind#2066]
62+
public void testUUIDLeniencyByType() throws Exception
63+
{
64+
final ObjectMapper STRICT_MAPPER = jsonMapperBuilder().build();
65+
STRICT_MAPPER.configOverride(UUID.class)
66+
.setFormat(JsonFormat.Value.forLeniency(false));
67+
68+
try {
69+
STRICT_MAPPER.readValue(quote(""), UUID.class);
70+
fail("Should not pass");
71+
} catch (Exception e) {
72+
_verifyBadUUID(e);
73+
}
74+
75+
try {
76+
STRICT_MAPPER.readValue("{\"value\":\"\"}", UUIDWrapper.class);
77+
fail("Should not pass");
78+
} catch (Exception e) {
79+
_verifyBadUUID(e);
80+
}
81+
}
82+
83+
// [databind#2066]
84+
public void testUUIDLeniencyByProperty() throws Exception
85+
{
86+
final ObjectMapper MAPPER = objectMapper();
87+
try {
88+
MAPPER.readValue("{\"value\":\"\"}", StrictUUIDWrapper.class);
89+
fail("Should not pass");
90+
} catch (Exception e) {
91+
_verifyBadUUID(e);
92+
}
93+
}
94+
95+
private void _verifyBadUUID(Exception e) {
96+
verifyException(e, "foobar"); // TODO: real exception message we want
97+
}
98+
}

0 commit comments

Comments
 (0)