diff --git a/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordFailingSetter3938Test.java b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordFailingSetter3938Test.java new file mode 100644 index 0000000000..8e263c5dae --- /dev/null +++ b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordFailingSetter3938Test.java @@ -0,0 +1,44 @@ +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 + assertNotNull(R.readValue(a2q("{'id':1, 'filter':'abc'}"))); + assertNotNull(R.readValue(a2q("{'id':2, 'filter':'def', 'options':null}"))); + + // But then failure for non-empty Array (f.ex) + try { + R.readValue(a2q("{'id':3, 'filter':'xyz', 'options':[ 123 ]}")); + fail("Should not pass"); + } catch (DatabindException e) { + verifyException(e, ERROR_3938_PREFIX); + } + } +}