|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.databind.*; |
| 7 | +import com.fasterxml.jackson.databind.deser.*; |
| 8 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; |
| 9 | +import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams; |
| 10 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 11 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 12 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 13 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | + |
| 17 | +public class JsonCreatorNoArgs4777Test extends DatabindTestUtil |
| 18 | +{ |
| 19 | + static class Foo { |
| 20 | + private Foo() { } |
| 21 | + |
| 22 | + @JsonCreator |
| 23 | + static Foo create() { |
| 24 | + return new Foo(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static class Instantiators implements ValueInstantiators { |
| 29 | + @Override |
| 30 | + public ValueInstantiator findValueInstantiator( |
| 31 | + DeserializationConfig config, |
| 32 | + BeanDescription beanDesc, |
| 33 | + ValueInstantiator defaultInstantiator |
| 34 | + ) { |
| 35 | + if (beanDesc.getBeanClass() == Foo.class) { |
| 36 | + AnnotatedWithParams dc = defaultInstantiator.getDefaultCreator(); |
| 37 | + if (!(dc instanceof AnnotatedMethod) |
| 38 | + || !dc.getName().equals("create")) { |
| 39 | + throw new IllegalArgumentException("Wrong DefaultCreator: should be static-method 'create()', is: " |
| 40 | + +dc); |
| 41 | + } |
| 42 | + } |
| 43 | + return defaultInstantiator; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // For [databind#4777] |
| 48 | + @SuppressWarnings("serial") |
| 49 | + @Test |
| 50 | + @JacksonTestFailureExpected |
| 51 | + public void testCreatorDetection4777() throws Exception { |
| 52 | + SimpleModule sm = new SimpleModule() { |
| 53 | + @Override |
| 54 | + public void setupModule(SetupContext context) { |
| 55 | + super.setupModule(context); |
| 56 | + context.addValueInstantiators(new Instantiators()); |
| 57 | + } |
| 58 | + }; |
| 59 | + ObjectMapper mapper = JsonMapper.builder().addModule(sm).build(); |
| 60 | + |
| 61 | + Foo result = mapper.readValue("{}", Foo.class); |
| 62 | + assertNotNull(result); |
| 63 | + } |
| 64 | +} |
0 commit comments