Skip to content

Commit

Permalink
Add a failing test for #4777
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 4, 2024
1 parent 98d5eed commit aea1ccc
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.fasterxml.jackson.databind.tofix;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class JsonCreatorNoArgs4777Test extends DatabindTestUtil
{
static class Foo {
private Foo() { }

@JsonCreator
static Foo create() {
return new Foo();
}
}

static class Instantiators implements ValueInstantiators {
@Override
public ValueInstantiator findValueInstantiator(
DeserializationConfig config,
BeanDescription beanDesc,
ValueInstantiator defaultInstantiator
) {
if (beanDesc.getBeanClass() == Foo.class) {
AnnotatedWithParams dc = defaultInstantiator.getDefaultCreator();
if (!(dc instanceof AnnotatedMethod)
|| !dc.getName().equals("create")) {
throw new IllegalArgumentException("Wrong DefaultCreator: should be static-method 'create()', is: "
+dc);
}
}
return defaultInstantiator;
}
}

// For [databind#4777]
@SuppressWarnings("serial")
@Test
@JacksonTestFailureExpected
public void testCreatorDetection4777() throws Exception {
SimpleModule sm = new SimpleModule() {
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.addValueInstantiators(new Instantiators());
}
};
ObjectMapper mapper = JsonMapper.builder().addModule(sm).build();

Foo result = mapper.readValue("{}", Foo.class);
assertNotNull(result);
}
}

0 comments on commit aea1ccc

Please sign in to comment.