-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26bc62f
Fixes FasterXML/jackson-databind#831
9d524e1
Support for array typed JSON typeIDs
02d7218
Unit test to ensure custom KeySerializer is indeed invoked when seria…
059eec5
Expose SerializerFactory on SerializerProvider
3c289b0
create mahi specific version
826deea
Add in mahi seprecific repos
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 53 additions & 0 deletions
53
src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypeIdResolver.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,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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 callingvalue.getClass()
? Is this method called directly with null from some place?Alternatively call above could pass null for null instance.
There was a problem hiding this comment.
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:
Please let me know if you want this changed.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.