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

Proposed patch to issue #24 #25

Merged
merged 5 commits into from
Oct 15, 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 @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonMapFormatVisitor;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema;

/**
Expand All @@ -19,6 +20,8 @@ public class MapVisitor extends JsonMapFormatVisitor.Base

protected SerializerProvider provider;

private WrapperFactory wrapperFactory = new WrapperFactory();

public MapVisitor(SerializerProvider provider, ObjectSchema schema)
{
this.provider = provider;
Expand Down Expand Up @@ -58,10 +61,21 @@ public void keyFormat(JsonFormatVisitable handler, JavaType keyType)
// JSON Schema only allows String types so let's not bother too much
}

@Override
@Override
public void valueFormat(JsonFormatVisitable handler, JavaType valueType)
throws JsonMappingException {
/* Also... not sure what to do with value type either.
*/

// ISSUE #24: https://github.com/FasterXML/jackson-module-jsonSchema/issues/24

JsonSchema valueSchema = propertySchema(handler, valueType);
ObjectSchema.AdditionalProperties ap = new ObjectSchema.SchemaAdditionalProperties(valueSchema.asSimpleTypeSchema());
this.schema.setAdditionalProperties(ap);
}

protected JsonSchema propertySchema(JsonFormatVisitable handler, JavaType propertyTypeHint)
throws JsonMappingException {
SchemaFactoryWrapper visitor = wrapperFactory.getWrapper(getProvider());
handler.acceptJsonFormatVisitor(visitor, propertyTypeHint);
return visitor.finalSchema();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2013 FasterXML.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fasterxml.jackson.module.jsonSchema.types;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import java.io.IOException;

/**
*
* @author Ignacio del Valle Alles
*/
public class AdditionalPropertiesDeserializer extends JsonDeserializer<ObjectSchema.AdditionalProperties> {

@Override
public ObjectSchema.AdditionalProperties deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
TreeNode node = mapper.readTree(jp);
String nodeStr = mapper.writeValueAsString(node);
if (node instanceof ObjectNode) {
JsonSchema innerSchema = mapper.readValue(nodeStr, JsonSchema.class);
return new ObjectSchema.SchemaAdditionalProperties(innerSchema);
} else if (node instanceof BooleanNode) {
BooleanNode booleanNode = (BooleanNode) node;
if (booleanNode.booleanValue()) {
return null; // "additionalProperties":true is the default
} else {
return ObjectSchema.NoAdditionalProperties.instance;
}
} else {
throw new JsonMappingException("additionalProperties nodes can only be of "
+ "type boolean or object: " + nodeStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.annotation.JsonValue;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;

import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
Expand Down Expand Up @@ -184,6 +185,7 @@ public void setProperties(Map<String, JsonSchema> properties) {
this.properties = properties;
}

@JsonDeserialize(using = AdditionalPropertiesDeserializer.class)
public static abstract class AdditionalProperties {
@JsonCreator
public AdditionalProperties jsonCreator() {
Expand Down Expand Up @@ -222,6 +224,7 @@ public Boolean value() {
public static final NoAdditionalProperties instance = new NoAdditionalProperties();
}


public static class SchemaAdditionalProperties extends AdditionalProperties {

@JsonProperty
Expand All @@ -237,6 +240,7 @@ public boolean equals(Object obj) {
getJsonSchema().equals(((SchemaAdditionalProperties)obj).getJsonSchema());
}

@JsonValue
public JsonSchema getJsonSchema() {
return jsonSchema;
}
Expand Down Expand Up @@ -333,4 +337,4 @@ public String getDependsOn() {
}
}

}
}
Loading