forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordFailingSetter3938Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class RecordFailingSetter3938Test extends BaseMapTest | ||
{ | ||
private final static String ERROR_3938_PREFIX = "Non-null 'options' not allowed for "; | ||
|
||
// [databind#3938] | ||
interface NoOptionsCommand { | ||
@JsonProperty("options") | ||
default void setOptions(JsonNode value) { | ||
if (value.isNull()) { | ||
return; | ||
} | ||
throw new IllegalArgumentException(ERROR_3938_PREFIX+getClass().getName()); | ||
} | ||
} | ||
|
||
public record Command3938(int id, String filter) implements NoOptionsCommand { } | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
// [databind#3938]: Should detect and use setters too | ||
public void testFailingSetter3939() throws Exception | ||
{ | ||
final ObjectReader R = MAPPER.readerFor(Command3938.class); | ||
|
||
// First, missing value and `null` are fine, as long as we have all fields | ||
assertNotNull(R.readValue(a2q("{'id':1, 'filter':'abc'}"))); | ||
assertNotNull(R.readValue(a2q("{'id':2, 'filter':'abc', 'options':null}"))); | ||
|
||
// But then failure for non-empty Array (f.ex) | ||
try { | ||
R.readValue(a2q("{'id':2,'options':[123]}}")); | ||
fail("Should not pass"); | ||
} catch (DatabindException e) { | ||
verifyException(e, ERROR_3938_PREFIX); | ||
} | ||
} | ||
} |