-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore ConstructorDetector.SingleArgConstructor.PROPERTIES preference…
… for Records.
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
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
75 changes: 75 additions & 0 deletions
75
...xml/jackson/databind/records/RecordImplicitSingleValueUsePropertiesBasedCreatorsTest.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,75 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.cfg.ConstructorDetector; | ||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; | ||
|
||
public class RecordImplicitSingleValueUsePropertiesBasedCreatorsTest extends BaseMapTest | ||
{ | ||
|
||
private final ObjectMapper MAPPER = jsonMapperBuilder() | ||
.annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector()) | ||
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED) | ||
.build(); | ||
|
||
record RecordWithMultiValueCanonAndSingleValueAltConstructor(int id, String name) { | ||
|
||
public RecordWithMultiValueCanonAndSingleValueAltConstructor(int id) { | ||
this(id, "AltConstructor"); | ||
} | ||
} | ||
|
||
record RecordWithSingleValueCanonAndMultiValueAltConstructor(String name) { | ||
|
||
public RecordWithSingleValueCanonAndMultiValueAltConstructor(String name, String email) { | ||
this("AltConstructor"); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, multi-value canonical constructor + single-value alt constructor | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testDeserializeMultipleConstructors_UsingMultiValueCanonicalConstructor() throws Exception { | ||
RecordWithMultiValueCanonAndSingleValueAltConstructor value = MAPPER.readValue( | ||
a2q("{'id':123,'name':'Bob'}"), | ||
RecordWithMultiValueCanonAndSingleValueAltConstructor.class); | ||
|
||
assertEquals(new RecordWithMultiValueCanonAndSingleValueAltConstructor(123, "Bob"), value); | ||
} | ||
|
||
public void testDeserializeMultipleConstructors_WillIgnoreSingleValueAltConstructor() throws Exception { | ||
RecordWithMultiValueCanonAndSingleValueAltConstructor value = MAPPER.readValue( | ||
a2q("{'id':123}"), | ||
RecordWithMultiValueCanonAndSingleValueAltConstructor.class); | ||
|
||
assertEquals(new RecordWithMultiValueCanonAndSingleValueAltConstructor(123, null), value); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, single-value canonical constructor + multi-value alt constructor | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testDeserializeMultipleConstructors_UsingSingleValueCanonicalConstructor() throws Exception { | ||
RecordWithSingleValueCanonAndMultiValueAltConstructor value = MAPPER.readValue( | ||
a2q("{'name':'Bob'}"), | ||
RecordWithSingleValueCanonAndMultiValueAltConstructor.class); | ||
|
||
assertEquals(new RecordWithSingleValueCanonAndMultiValueAltConstructor("Bob"), value); | ||
} | ||
|
||
public void testDeserializeMultipleConstructors_WillIgnoreMultiValueAltConstructor() throws Exception { | ||
try { | ||
MAPPER.readValue(a2q("{'name':'Bob','email':'[email protected]'}"), RecordWithSingleValueCanonAndMultiValueAltConstructor.class); | ||
} catch (UnrecognizedPropertyException e) { | ||
verifyException(e, "Unrecognized"); | ||
verifyException(e, "\"email\""); | ||
verifyException(e, "RecordWithSingleValueCanonAndMultiValueAltConstructor"); | ||
} | ||
} | ||
} |