|
| 1 | +package com.fasterxml.jackson.databind.records; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.cfg.ConstructorDetector; |
| 6 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 7 | + |
| 8 | +public class RecordImplicitSingleValueUsePropertiesBasedCreatorsTest extends BaseMapTest |
| 9 | +{ |
| 10 | + |
| 11 | + record RecordWithMultiValueCanonAndSingleValueAltConstructor(int id, String name) { |
| 12 | + |
| 13 | + public RecordWithMultiValueCanonAndSingleValueAltConstructor(int id) { |
| 14 | + this(id, "AltConstructor"); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + record RecordWithSingleValueCanonAndMultiValueAltConstructor(String name) { |
| 19 | + |
| 20 | + public RecordWithSingleValueCanonAndMultiValueAltConstructor(String name, String email) { |
| 21 | + this("AltConstructor"); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private final ObjectMapper MAPPER = jsonMapperBuilder() |
| 26 | + .annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector()) |
| 27 | + .constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED) |
| 28 | + .build(); |
| 29 | + |
| 30 | + /* |
| 31 | + /********************************************************************** |
| 32 | + /* Test methods, multi-value canonical constructor + single-value alt constructor |
| 33 | + /********************************************************************** |
| 34 | + */ |
| 35 | + |
| 36 | + public void testDeserializeMultipleConstructors_UsingMultiValueCanonicalConstructor() throws Exception { |
| 37 | + RecordWithMultiValueCanonAndSingleValueAltConstructor value = MAPPER.readValue( |
| 38 | + a2q("{'id':123,'name':'Bob'}"), |
| 39 | + RecordWithMultiValueCanonAndSingleValueAltConstructor.class); |
| 40 | + |
| 41 | + assertEquals(new RecordWithMultiValueCanonAndSingleValueAltConstructor(123, "Bob"), value); |
| 42 | + } |
| 43 | + |
| 44 | + public void testDeserializeMultipleConstructors_WillIgnoreSingleValueAltConstructor() throws Exception { |
| 45 | + RecordWithMultiValueCanonAndSingleValueAltConstructor value = MAPPER.readValue( |
| 46 | + a2q("{'id':123}"), |
| 47 | + RecordWithMultiValueCanonAndSingleValueAltConstructor.class); |
| 48 | + |
| 49 | + assertEquals(new RecordWithMultiValueCanonAndSingleValueAltConstructor(123, null), value); |
| 50 | + } |
| 51 | + |
| 52 | + /* |
| 53 | + /********************************************************************** |
| 54 | + /* Test methods, single-value canonical constructor + multi-value alt constructor |
| 55 | + /********************************************************************** |
| 56 | + */ |
| 57 | + |
| 58 | + public void testDeserializeMultipleConstructors_UsingSingleValueCanonicalConstructor() throws Exception { |
| 59 | + RecordWithSingleValueCanonAndMultiValueAltConstructor value = MAPPER.readValue( |
| 60 | + a2q("{'name':'Bob'}"), |
| 61 | + RecordWithSingleValueCanonAndMultiValueAltConstructor.class); |
| 62 | + |
| 63 | + assertEquals(new RecordWithSingleValueCanonAndMultiValueAltConstructor("Bob"), value); |
| 64 | + } |
| 65 | + |
| 66 | + public void testDeserializeMultipleConstructors_WillIgnoreMultiValueAltConstructor() throws Exception { |
| 67 | + try { |
| 68 | + MAPPER. readValue( a2q( "{'name':'Bob','email':'[email protected]'}"), RecordWithSingleValueCanonAndMultiValueAltConstructor. class); |
| 69 | + } catch (UnrecognizedPropertyException e) { |
| 70 | + verifyException(e, "Unrecognized"); |
| 71 | + verifyException(e, "\"email\""); |
| 72 | + verifyException(e, "RecordWithSingleValueCanonAndMultiValueAltConstructor"); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments