-
Notifications
You must be signed in to change notification settings - Fork 138
Deserialize ArrayItems and JsonValueFormat correctly #81
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f5928c
Deserialize JsonValueFormat
georgewfraser eda54d2
Deserialize ArraySchema
georgewfraser 0f22a28
Format attributes are lowercase http://json-schema.org/latest/json-sc…
georgewfraser b2e04c8
Remove bad toString method
georgewfraser ae77533
Serialize JsonValueFormat using toString
georgewfraser 77d55ee
Test reading of array schemas
georgewfraser d235b4a
Too many cycles of write / read / write loses the order of Set elements
georgewfraser 70648be
ReferenceSchema needs "type" property to deserialize correctly
georgewfraser b52333a
@JsonUnwrapped was on the wrong property
georgewfraser ecaddc0
Need to cycles of write-string / read-JsonSchema to get $ref to be co…
georgewfraser 87da47e
Tests are no longer failing
georgewfraser 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 hidden or 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 |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package com.fasterxml.jackson.module.jsonSchema.types; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.ObjectCodec; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/* | ||
* This attribute defines the allowed items in an instance array, and | ||
MUST be a jsonSchema or an array of jsonSchemas. The default value is an | ||
|
@@ -27,6 +35,7 @@ public class ArraySchema extends ContainerTypeSchema | |
* see {@link Items} | ||
*/ | ||
@JsonProperty | ||
@JsonDeserialize(using = ItemsDeserializer.class) | ||
protected ArraySchema.Items items; | ||
|
||
/**This attribute defines the maximum number of values in an array*/ | ||
|
@@ -133,6 +142,15 @@ && equals(getMinItems(), that.getMinItems()) | |
&& equals(getUniqueItems(), that.getUniqueItems()) | ||
&& super._equals(that); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
try { | ||
return new ObjectMapper().writeValueAsString(this); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* This provides a definition for additional items in an array instance | ||
|
@@ -159,10 +177,14 @@ public static Items jsonCreator(Map<String,Object> props) { | |
public static class ArrayItems extends ArraySchema.Items { | ||
@JsonProperty | ||
private JsonSchema[] jsonSchemas; | ||
|
||
|
||
public ArrayItems(JsonSchema[] jsonSchemas) { | ||
this.jsonSchemas = jsonSchemas; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see com.fasterxml.jackson.databind.jsonSchema.types.ArraySchema.Items#asArrayItems() | ||
*/ | ||
* @see com.fasterxml.jackson.databind.jsonSchema.types.ArraySchema.Items#asArrayItems() | ||
*/ | ||
@Override | ||
public ArrayItems asArrayItems() { return this; } | ||
|
||
|
@@ -189,6 +211,24 @@ public JsonSchema[] getJsonSchemas() { | |
@Override | ||
public boolean isArrayItems() { return true; } | ||
} | ||
|
||
public static class ItemsDeserializer extends JsonDeserializer<Items> { | ||
|
||
@Override | ||
public Items deserialize(JsonParser parser, | ||
DeserializationContext context) throws IOException, JsonProcessingException { | ||
ObjectCodec mapper = parser.getCodec(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume it is legal to use array, or a single item here? |
||
JsonNode node = parser.readValueAs(JsonNode.class); | ||
|
||
if (node.isArray()) { | ||
JsonSchema[] schemas = mapper.treeToValue(node, JsonSchema[].class); | ||
|
||
return new ArrayItems(schemas); | ||
} | ||
else | ||
return new SingleItems(mapper.treeToValue(node, JsonSchema.class)); | ||
} | ||
} | ||
|
||
/** | ||
* This attribute defines the allowed items in an instance array, and | ||
|
@@ -205,21 +245,6 @@ public static abstract class Items { | |
|
||
public SingleItems asSingleItems() { return null; } | ||
public ArrayItems asArrayItems() { return null; } | ||
|
||
@JsonCreator | ||
public static Items jsonCreator(Map<String,Object> props) { | ||
//for now only support deserialization of singleItems | ||
Object typeFound = props.get("type"); | ||
if (typeFound == null || ! (typeFound instanceof String)) { | ||
return null; | ||
} | ||
String type = (String) typeFound; | ||
JsonSchema schema = JsonSchema.minimalForFormat(JsonFormatTypes.forValue(type)); | ||
//KNOWN ISSUE: pending https://github.com/FasterXML/jackson-databind/issues/43 | ||
//only deserialize items as minimal schema for type | ||
return new SingleItems(schema); | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
25 changes: 25 additions & 0 deletions
25
src/main/java/com/fasterxml/jackson/module/jsonSchema/types/JsonValueFormatDeserializer.java
This file contains hidden or 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,25 @@ | ||
package com.fasterxml.jackson.module.jsonSchema.types; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat; | ||
|
||
import java.io.IOException; | ||
|
||
public class JsonValueFormatDeserializer extends JsonDeserializer<JsonValueFormat> { | ||
@Override | ||
public JsonValueFormat deserialize(JsonParser jsonParser, | ||
DeserializationContext deserializationContext) throws IOException, JsonProcessingException { | ||
String string = jsonParser.getValueAsString(); | ||
|
||
for (JsonValueFormat f : JsonValueFormat.values()) { | ||
if (f.toString().equals(string)) | ||
return f; | ||
} | ||
|
||
throw new JsonMappingException("Expected JsonValueFormat but found " + string); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
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.
Constructing a new
ObjectMapper
from withintoString()
is not something to do, for multiple reasons (extra slow performance, non-configurability, for example).As a practical consequence, none of Jackson components serialize full valid JSON (or, rather, is guaranteed to do so) from
toString()
, and users should not assume results are useful for anything other than debugging.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.
Sorry this was something I added for debugging and I intended to remove it.