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 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
34 changes: 27 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@

<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0-rc3-SNAPSHOT</version>
<version>2.6.0-rc3-mahi</version>
<name>jackson-databind</name>
<packaging>bundle</packaging>
<description>General data-binding functionality for Jackson: works on core streaming API</description>
<description>General data-binding functionality for Jackson: works on core streaming API - MahiFX</description>
<url>http://github.com/FasterXML/jackson</url>

<scm>
<connection>scm:git:[email protected]:FasterXML/jackson-databind.git</connection>
<developerConnection>scm:git:[email protected]:FasterXML/jackson-databind.git</developerConnection>
<url>http://github.com/FasterXML/jackson-databind</url>
<connection>scm:git:[email protected]:MahiFX/jackson-databind.git</connection>
<developerConnection>scm:git:[email protected]:MahiFX/jackson-databind.git</developerConnection>
<url>http://github.com/MahiFX/jackson-databind</url>
<tag>HEAD</tag>
</scm>

Expand Down Expand Up @@ -69,12 +69,12 @@ javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0-rc3-SNAPSHOT</version>
<version>2.6.0-rc3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.0-rc3-SNAPSHOT</version>
<version>2.6.0-rc3</version>
</dependency>

<!-- and for testing we need a few libraries
Expand Down Expand Up @@ -134,6 +134,26 @@ javax.xml.datatype, javax.xml.namespace, javax.xml.parsers
</plugins>
</build>

<distributionManagement>
<repository>
<uniqueVersion>false</uniqueVersion>
<id>releases</id>
<name>releases</name>
<url>http://build.mahifx.com:8081/nexus/content/repositories/releases</url>
</repository>

<snapshotRepository>
<uniqueVersion>false</uniqueVersion>
<id>snapshots</id>
<name>snapshots</name>
<url>http://build.mahifx.com:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
<site>
<id>${project.name}</id>
<url>file://${user.dir}/all/target/site-deployed</url>
</site>
</distributionManagement>

<reporting>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package com.fasterxml.jackson.databind;

import java.io.IOException;
import java.text.DateFormat;
import java.util.*;

import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.impl.*;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
import com.fasterxml.jackson.databind.ser.SerializerCache;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.impl.FailingSerializer;
import com.fasterxml.jackson.databind.ser.impl.ReadOnlyClassToSerializerMap;
import com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer;
import com.fasterxml.jackson.databind.ser.impl.UnknownSerializer;
import com.fasterxml.jackson.databind.ser.impl.WritableObjectId;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ClassUtil;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
* Class that defines API used by {@link ObjectMapper} and
* {@link JsonSerializer}s to obtain serializers capable of serializing
Expand Down Expand Up @@ -1230,4 +1241,11 @@ protected final DateFormat _dateFormat()
}
return df;
}

/*
* Expose serializer factory
*/
public SerializerFactory getSerializerFactory() {
return _serializerFactory;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.fasterxml.jackson.databind.jsontype.impl;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.util.JsonParserSequence;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.util.TokenBuffer;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Type deserializer used with {@link As#PROPERTY}
* inclusion mechanism.
Expand Down Expand Up @@ -106,24 +113,59 @@ public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext c
@SuppressWarnings("resource")
protected final Object _deserializeTypedForId(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb) throws IOException
{
String typeId = jp.getText();
JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
if (_typeIdVisible) { // need to merge id back in JSON input?
if (tb == null) {
tb = new TokenBuffer(null, false);
if(jp.getCurrentToken() == JsonToken.VALUE_STRING) {
String typeId = jp.getText();
JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
if (_typeIdVisible) { // need to merge id back in JSON input?
if (tb == null) {
tb = new TokenBuffer(null, false);
}
tb.writeFieldName(jp.getCurrentName());
tb.writeString(typeId);
}
if (tb != null) { // need to put back skipped properties?
jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
}
tb.writeFieldName(jp.getCurrentName());
tb.writeString(typeId);
// Must point to the next value; tb had no current, jp pointed to VALUE_STRING:
jp.nextToken(); // to skip past String value
// deserializer should take care of closing END_OBJECT as well
return deser.deserialize(jp, ctxt);
}
if (tb != null) { // need to put back skipped properties?
jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
else if(jp.getCurrentToken() == JsonToken.START_ARRAY){
if(_typeIdVisible){
if(tb==null){
tb = new TokenBuffer(jp.getCodec(), false);
}
tb.writeFieldName(jp.getCurrentName());
}
Set<String> values = new HashSet<String>();
while(jp.getCurrentToken()!=JsonToken.END_ARRAY){
if(_typeIdVisible){
tb.copyCurrentEvent(jp);
}
jp.nextToken();
if(jp.getCurrentToken() == JsonToken.VALUE_STRING){
values.add(jp.getText());
}
}
if(_typeIdVisible){
tb.copyCurrentEvent(jp);
}
String typeId = Arrays.toString(values.toArray(new String[0]));
JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
if (tb != null) { // need to put back skipped properties?
jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
}
jp.nextToken();
return deser.deserialize(jp, ctxt);
}
// Must point to the next value; tb had no current, jp pointed to VALUE_STRING:
jp.nextToken(); // to skip past String value
// deserializer should take care of closing END_OBJECT as well
return deser.deserialize(jp, ctxt);

throw new IllegalStateException("Can't resolve typeid from token: " + jp.getCurrentToken());

}




// off-lined to keep main method lean and mean...
@SuppressWarnings("resource")
protected Object _deserializeTypedUsingDefaultImpl(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb) throws IOException
Expand Down
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
37 changes: 29 additions & 8 deletions src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package com.fasterxml.jackson.databind.util;

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.TreeMap;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.Base64Variant;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.base.ParserMinimalBase;
import com.fasterxml.jackson.core.json.JsonReadContext;
import com.fasterxml.jackson.core.json.JsonWriteContext;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.DeserializationContext;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.TreeMap;

/**
* Utility class used for efficient storage of {@link JsonToken}
Expand Down Expand Up @@ -1271,7 +1284,15 @@ public JsonLocation getCurrentLocation() {
}

@Override
public String getCurrentName() { return _parsingContext.getCurrentName(); }
public String getCurrentName() {
// Cloned from ParserBase.getCurrentName()
if(this._currToken != JsonToken.START_OBJECT && this._currToken != JsonToken.START_ARRAY) {
return this._parsingContext.getCurrentName();
} else {
JsonReadContext parent = this._parsingContext.getParent();
return parent.getCurrentName();
}
}

@Override
public void overrideCurrentName(String name)
Expand Down
Loading