|
| 1 | +package com.fasterxml.jackson.databind.records; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.*; |
| 6 | + |
| 7 | +public class RecordFailingSetter3938Test extends BaseMapTest |
| 8 | +{ |
| 9 | + private final static String ERROR_3938_PREFIX = "Non-null 'options' not allowed for "; |
| 10 | + |
| 11 | + // [databind#3938] |
| 12 | + interface NoOptionsCommand { |
| 13 | + @JsonProperty("options") |
| 14 | + default void setOptions(JsonNode value) { |
| 15 | + if (value.isNull()) { |
| 16 | + return; |
| 17 | + } |
| 18 | + throw new IllegalArgumentException(ERROR_3938_PREFIX+getClass().getName()); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public record Command3938(int id, String filter) implements NoOptionsCommand { |
| 23 | + } |
| 24 | + |
| 25 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 26 | + |
| 27 | + // [databind#3938]: Should detect and use setters too |
| 28 | + public void testFailingSetter3939() throws Exception |
| 29 | + { |
| 30 | + final ObjectReader R = MAPPER.readerFor(Command3938.class); |
| 31 | + |
| 32 | + // First, missing value and `null` are fine |
| 33 | + assertNotNull(R.readValue(a2q("{'id':1, 'filter':'abc'}"))); |
| 34 | + assertNotNull(R.readValue(a2q("{'id':2, 'filter':'def', 'options':null}"))); |
| 35 | + |
| 36 | + // But then failure for non-empty Array (f.ex) |
| 37 | + try { |
| 38 | + R.readValue(a2q("{'id':3, 'filter':'xyz', 'options':[ 123 ]}")); |
| 39 | + fail("Should not pass"); |
| 40 | + } catch (DatabindException e) { |
| 41 | + verifyException(e, ERROR_3938_PREFIX); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments