|
| 1 | +package com.fasterxml.jackson.databind.records; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | + |
| 10 | +import static com.fasterxml.jackson.databind.BaseMapTest.newJsonMapper; |
| 11 | +import static com.fasterxml.jackson.databind.BaseTest.a2q; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests to verify that `@JsonInclude` annotation works with Records on both constructor parameters and getters. |
| 15 | + */ |
| 16 | +public class RecordJsonIncludeTest |
| 17 | +{ |
| 18 | + |
| 19 | + record AnnotatedParamRecordClass( |
| 20 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 21 | + String omitFieldIfNull, |
| 22 | + String standardField |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + record AnnotatedGetterRecordClass( |
| 27 | + String omitFieldIfNull, |
| 28 | + String standardField |
| 29 | + ) { |
| 30 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 31 | + public String omitFieldIfNull() { |
| 32 | + return omitFieldIfNull; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private ObjectMapper MAPPER = newJsonMapper(); |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testJsonIncludeOnRecordParam() throws Exception |
| 40 | + { |
| 41 | + // Test with constructor parameter |
| 42 | + assertEquals(a2q("{'standardField':'def'}"), |
| 43 | + MAPPER.writeValueAsString(new AnnotatedParamRecordClass(null, "def"))); |
| 44 | + // Test with getter |
| 45 | + assertEquals(a2q("{'standardField':'def'}"), |
| 46 | + MAPPER.writeValueAsString(new AnnotatedGetterRecordClass(null, "def"))); |
| 47 | + } |
| 48 | +} |
0 commit comments