Skip to content
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

Jackson databind changes for draft v4 schema generator #838

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,14 +1,16 @@
package com.fasterxml.jackson.databind.jsontype.impl;

import java.util.*;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.jsontype.NamedType;

import java.util.Collection;
import java.util.HashMap;
import java.util.TreeSet;

public class TypeNameIdResolver extends TypeIdResolverBase
{
protected final MapperConfig<?> _config;
Expand Down Expand Up @@ -80,7 +82,16 @@ public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType base
@Override
public String idFromValue(Object value)
{
Class<?> cls = _typeFactory.constructType(value.getClass()).getRawClass();
return idFromClass(value.getClass());
}


protected String idFromClass(Class<?> clazz)
{
if(clazz==null){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this actually work? Caller assumes value is not null by calling value.getClass()? Is this method called directly with null from some place?
Alternatively call above could pass null for null instance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point albeit it is backward compatible :-) Previous impl:

    @Override
    public String idFromValue(Object value)
    {
        Class<?> cls = _typeFactory.constructType(value.getClass()).getRawClass();

Please let me know if you want this changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I am just trying to understand what is the intended change -- not saying it breaks anything that wasn't already broken. But I guess the answer is seen below... so never mind.

I am ok without changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologise if I was not clear enough, let me try to explain.

As you are very well aware when Jackson generates JSON documents out of java objects the generated JSON document itself might not have enough type information to turn that JSON document back to a Java object. In that case an optional Typer can be provided which will enrich the JSON document with additional type information so the deserializer knows which class to instantiate.

Now from a schema generator point of view if the schema generator does not take this additional meta-data into consideration than the JSON document will no longer be valid against the generated schema. To solve this problem the schema generator will need to know exactly how Jackson will generate JSON. To avoid duplication I am allowing the same typer to be provided to the schema mapper as what the original mapper was using. Now when in schema generation mode I do not have access to any instances of the class for which schema is being generated, I purely need to operate on the class itself. Without this change I won't be able to extract the typeId used. The added test case shows how the generator will use this to decide what type of metadata is added and if the typeId known it will also restrict the allowed values to it.

Please let me know if you need further information.

Zoltan

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the need, from perspective that while traversal is based on serialization, in many aspects it really relates more to deserialization because there is no access to instances just types.
I am just trying to see how pieces fit together: looking at code I can see how it relates to JsonSerializer, which is the entrypoint. I didn't think visitors exposed information directly.

return null;
}
Class<?> cls = _typeFactory.constructType(clazz).getRawClass();
final String key = cls.getName();
String name;
synchronized (_typeToId) {
Expand Down Expand Up @@ -108,7 +119,7 @@ public String idFromValueAndType(Object value, Class<?> type) {
* it seems; nothing much we can figure out that way.
*/
if (value == null) {
return null;
return idFromClass(type);
}
return idFromValue(value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fasterxml.jackson.databind.jsontype;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

/**
* Created by zoliszel on 22/06/2015.
*/
public class TestTypeIdResolver {

private ObjectMapper mapper;

@JsonTypeName(TestClass.NAME)
public static class TestClass{
public static final String NAME = "TEST_CLASS";
}

@Before
public void setup(){
mapper = new ObjectMapper();

TypeResolverBuilder<?> typerBuilder = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
typerBuilder = typerBuilder.init(JsonTypeInfo.Id.NAME, null);
typerBuilder = typerBuilder.inclusion(JsonTypeInfo.As.PROPERTY);
mapper.setDefaultTyping(typerBuilder);
}

@Test
public void testTypeIdResolutionForClasses(){
SerializationConfig config= mapper.getSerializationConfig();
BeanDescription beanDescription = config.introspectClassAnnotations(TestClass.class);
Assert.assertNotNull("BeanDescription should not be null",beanDescription);

TypeResolverBuilder<?> typer = config.getDefaultTyper(mapper.constructType(TestClass.class));
Assert.assertNotNull("Typer should not be null",typer);

TypeSerializer typeSerializer = typer.buildTypeSerializer(config, mapper.constructType(TestClass.class), Collections.EMPTY_LIST);

Assert.assertNotNull("TypeSerializer should not be null",typeSerializer);
Assert.assertNotNull("TypeId resolver should not be null",typeSerializer.getTypeIdResolver() );

String typeName = typeSerializer.getTypeIdResolver().idFromValueAndType(null, TestClass.class);
Assert.assertEquals("TypeName was not resolved correctly",TestClass.NAME,typeName);
}
}