Skip to content

Commit

Permalink
Fix #959
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 8, 2015
1 parent 7e4e115 commit c8de5aa
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 23 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project: jackson-databind
#948: Support leap seconds, any number of millisecond digits for ISO-8601 Dates.
(contributed by Jesse W)
#957: Merge `datatype-jdk7` stuff in (java.nio.file.Path handling)
#959: Schema generation: consider active view, discard non-included properties
- Make `JsonValueFormat` (self-)serializable, deserializable, to/from valid external
value (as per JSON Schema spec)

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/fasterxml/jackson/databind/BeanProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,17 @@ public interface BeanProperty extends Named
* Note that not all implementations support traversal with this
* method; those that do not should throw
* {@link UnsupportedOperationException}.
*<p>
* NOTE: Starting with 2.7, takes explicit {@link SerializerProvider}
* argument to reduce the need to rely on provider visitor may or may not
* have assigned.
*
* @param objectVisitor Visitor to used as the callback handler
*
* @since 2.2
* @since 2.2 (although signature did change in 2.7)
*/
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor)
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
SerializerProvider provider)
throws JsonMappingException;

/*
Expand Down Expand Up @@ -256,7 +261,8 @@ public JsonFormat.Value findFormatOverrides(AnnotationIntrospector intr) {
* visited. Rather, other implementations should handle it.
*/
@Override
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor) {
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
SerializerProvider provider) {
throw new UnsupportedOperationException("Instances of "+getClass().getName()+" should not get visited");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.lang.annotation.Annotation;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.impl.FailingDeserializer;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
Expand Down Expand Up @@ -347,7 +346,8 @@ public <A extends Annotation> A getContextAnnotation(Class<A> acls) {
}

@Override
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor)
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
SerializerProvider provider)
throws JsonMappingException
{
if (isRequired()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import java.util.HashMap;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.SerializedString;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.introspect.*;
Expand Down Expand Up @@ -750,7 +748,8 @@ public void serializeAsPlaceholder(Object bean, JsonGenerator gen, SerializerPro

// Also part of BeanProperty implementation
@Override
public void depositSchemaProperty(JsonObjectFormatVisitor v)
public void depositSchemaProperty(JsonObjectFormatVisitor v,
SerializerProvider provider)
throws JsonMappingException
{
if (v != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public abstract void serializeAsPlaceholder(Object value, JsonGenerator jgen, Se
* POJO introspection.
*/
@Override
public abstract void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor)
public abstract void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
SerializerProvider provider)
throws JsonMappingException;


/**
* Legacy method called for JSON Schema generation; should not be called by new code
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.fasterxml.jackson.databind.ser.impl;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.util.NameTransformer;

Expand Down Expand Up @@ -81,6 +83,16 @@ public void serializeAsElement(Object bean, JsonGenerator jgen, SerializerProvid
_delegate.serializeAsPlaceholder(bean, jgen, prov);
}
}

@Override
public void depositSchemaProperty(JsonObjectFormatVisitor v,
SerializerProvider provider) throws JsonMappingException
{
Class<?> activeView = provider.getActiveView();
if (activeView == null || _view.isAssignableFrom(activeView)) {
super.depositSchemaProperty(v, provider);
}
}
}

private final static class MultiView
Expand Down Expand Up @@ -151,5 +163,22 @@ public void serializeAsElement(Object bean, JsonGenerator jgen, SerializerProvid
}
_delegate.serializeAsElement(bean, jgen, prov);
}

@Override
public void depositSchemaProperty(JsonObjectFormatVisitor v,
SerializerProvider provider) throws JsonMappingException
{
Class<?> activeView = provider.getActiveView();
if (activeView != null) {
int i = 0, len = _views.length;
for (; i < len; ++i) {
if (_views[i].isAssignableFrom(activeView)) break;
}
if (i == len) { // not match? Just don't deposit
return;
}
}
super.depositSchemaProperty(v, provider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void depositSchemaProperty(BeanPropertyWriter writer,
throws JsonMappingException
{
if (include(writer)) {
writer.depositSchemaProperty(objectVisitor);
writer.depositSchemaProperty(objectVisitor, provider);
}
}

Expand Down Expand Up @@ -238,7 +238,7 @@ public void depositSchemaProperty(PropertyWriter writer,
SerializerProvider provider) throws JsonMappingException
{
if (include(writer)) {
writer.depositSchemaProperty(objectVisitor);
writer.depositSchemaProperty(objectVisitor, provider);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ public void assignSerializer(JsonSerializer<Object> ser)
*/

@Override
public void depositSchemaProperty(final JsonObjectFormatVisitor visitor)
throws JsonMappingException {
SerializerProvider provider = visitor.getProvider();
public void depositSchemaProperty(final JsonObjectFormatVisitor visitor,
SerializerProvider provider) throws JsonMappingException
{
JsonSerializer<Object> ser = provider
.findValueSerializer(this.getType(), this)
.unwrappingSerializer(_nameTransformer);
Expand All @@ -169,7 +169,7 @@ public JsonObjectFormatVisitor expectObjectFormat(JavaType type)
}
}, this.getType());
} else {
super.depositSchemaProperty(visitor);
super.depositSchemaProperty(visitor, provider);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ protected final String _customTypeId(Object bean)
*/

protected void serializeFields(Object bean, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonGenerationException
throws IOException
{
final BeanPropertyWriter[] props;
if (_filteredProps != null && provider.getActiveView() != null) {
Expand Down Expand Up @@ -790,15 +790,28 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
if (objectVisitor == null) {
return;
}
final SerializerProvider provider = visitor.getProvider();
if (_propertyFilterId != null) {
PropertyFilter filter = findPropertyFilter(visitor.getProvider(),
_propertyFilterId, null);
for (int i = 0; i < _props.length; i++) {
filter.depositSchemaProperty(_props[i], objectVisitor, visitor.getProvider());
for (int i = 0, end = _props.length; i < end; ++i) {
filter.depositSchemaProperty(_props[i], objectVisitor, provider);
}
} else {
for (int i = 0; i < _props.length; i++) {
_props[i].depositSchemaProperty(objectVisitor);
Class<?> view = ((_filteredProps == null) || (provider == null))
? null : provider.getActiveView();
final BeanPropertyWriter[] props;
if (view != null) {
props = _filteredProps;
} else {
props = _props;
}

for (int i = 0, end = props.length; i < end; ++i) {
BeanPropertyWriter prop = props[i];
if (prop != null) { // may be filtered out unconditionally
prop.depositSchemaProperty(objectVisitor, provider);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class MapProperty extends PropertyWriter
{
private static final long serialVersionUID = 1L;

protected final TypeSerializer _typeSerializer;

protected final BeanProperty _property;
Expand Down Expand Up @@ -122,11 +124,12 @@ public void serializeAsPlaceholder(Object value, JsonGenerator gen,
*/

@Override
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor)
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
SerializerProvider provider)
throws JsonMappingException
{
if (_property != null) {
_property.depositSchemaProperty(objectVisitor);
_property.depositSchemaProperty(objectVisitor, provider);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.fasterxml.jackson.databind.views;

import java.util.*;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;

public class ViewsWithSchemaTest extends BaseMapTest
{
interface ViewBC { }
interface ViewAB { }

@JsonPropertyOrder({ "a", "b", "c" })
static class POJO {
@JsonView({ ViewAB.class })
public int a;

@JsonView({ ViewAB.class, ViewBC.class })
public int b;

@JsonView({ ViewBC.class })
public int c;
}

static class ListingVisitor extends JsonFormatVisitorWrapper.Base
{
public final List<String> names = new ArrayList<String>();

@Override
public JsonObjectFormatVisitor expectObjectFormat(JavaType type) {
return new JsonObjectFormatVisitor.Base() {
@Override
public void optionalProperty(BeanProperty writer) {
names.add(writer.getName());
}

@Override
public void optionalProperty(String name,
JsonFormatVisitable handler, JavaType propertyTypeHint) {
names.add(name);
}
};
}
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final ObjectMapper MAPPER = new ObjectMapper();

public void testSchemaWithViews() throws Exception
{
ListingVisitor v = new ListingVisitor();
MAPPER.writerWithView(ViewBC.class)
.acceptJsonFormatVisitor(POJO.class, v);
assertEquals(Arrays.asList("b", "c"), v.names);

v = new ListingVisitor();
MAPPER.writerWithView(ViewAB.class)
.acceptJsonFormatVisitor(POJO.class, v);
assertEquals(Arrays.asList("a", "b"), v.names);
}

public void testSchemaWithoutViews() throws Exception
{
ListingVisitor v = new ListingVisitor();
MAPPER.acceptJsonFormatVisitor(POJO.class, v);
assertEquals(Arrays.asList("a", "b", "c"), v.names);
}
}

0 comments on commit c8de5aa

Please sign in to comment.