Skip to content

Support type modifiers in type id name resolution. #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static TypeNameIdResolver construct(MapperConfig<?> config,
@Override
public String idFromValue(Object value)
{
Class<?> cls = value.getClass();
Class<?> cls = _typeFactory.constructType(value.getClass()).getRawClass();
final String key = cls.getName();
String name;
synchronized (_typeToId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fasterxml.jackson.databind.module;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeBindings;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.type.TypeModifier;
import com.fasterxml.jackson.test.BaseTest;

import java.lang.reflect.Type;

public class TestTypeModifierNameResolution extends BaseTest {

interface MyType {
String getData();
void setData(String data);
}

static class MyTypeImpl implements MyType {
private String data;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}

static class CustomTypeModifier extends TypeModifier {
@Override
public JavaType modifyType(JavaType type, Type jdkType, TypeBindings context, TypeFactory typeFactory) {
if (type.getRawClass().equals(MyTypeImpl.class)) {
return typeFactory.constructType(MyType.class);
}
return type;
}
}

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
public interface Mixin { }

// Expect that the TypeModifier kicks in when the type id is written.
public void testTypeModiferNameResolution() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.setTypeFactory(mapper.getTypeFactory().withModifier(new CustomTypeModifier()));
mapper.addMixInAnnotations(MyType.class, Mixin.class);

MyType obj = new MyTypeImpl();
obj.setData("something");

String s = mapper.writer().writeValueAsString(obj);
assertTrue(s.startsWith("{\"TestTypeModifierNameResolution$MyType\":"));
}
}