Skip to content

Commit

Permalink
Merge pull request #64 from igreenfield/master
Browse files Browse the repository at this point in the history
Add support to oneOf
  • Loading branch information
cowtowncoder committed Mar 29, 2015
2 parents 8ed26e9 + e17d0b6 commit 8d79756
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,32 @@ of enum values uses the same algorithm as defined in "uniqueItems"
*/
@JsonProperty(value = "enum", required = true)
private Set<String> enums;
//instance initializer block

//instance initializer block
{
enums = new HashSet<String>();
}

/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.jsonSchema.types.JsonSchema#asContainerSchema()
/**
* This provides an enumeration of all possible values that are valid
for the instance property. This MUST be an array, and each item in
the array represents a possible value for the instance value. If
this attribute is defined, the instance value MUST be one of the
values in the array in order for the schema to be valid. Comparison
of enum values uses the same algorithm as defined in "uniqueItems"
(Section 5.15).
*/
@JsonProperty(value = "oneOf", required = true)
private Set<Object> oneOf;
//instance initializer block
{
oneOf = new HashSet<Object>();
}



/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.jsonSchema.types.JsonSchema#asContainerSchema()
*/
@Override
public ContainerTypeSchema asContainerSchema() { return this; }

Expand Down Expand Up @@ -60,4 +77,12 @@ public Set<String> getEnums() {
public void setEnums(Set<String> enums) {
this.enums = enums;
}

public Set<Object> getOneOf() {
return oneOf;
}

public void setOneOf(Set<Object> oneOf) {
this.oneOf = oneOf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
import com.fasterxml.jackson.module.jsonSchema.types.ArraySchema;
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema;
import junit.framework.Assert;

/**
* Trivial test to ensure {@link JsonSchema} can be also deserialized
Expand Down Expand Up @@ -126,7 +127,7 @@ public void _testSimple(String name, JsonSchema jsonSchema) throws Exception
String json2 = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(finalNode);

// assertEquals(node, finalNode);
assertEquals("Schemas for "+name+" differ",
assertEquals("Schemas for " + name + " differ",
json1, json2);
}

Expand Down Expand Up @@ -155,4 +156,49 @@ public void testDeserializeTrueAdditionalProperties() throws Exception
ObjectSchema schema = MAPPER.readValue(schemaStr, ObjectSchema.class);
assertNull(schema.getAdditionalProperties());
}

/**
* Verifies that a true-valued additional property is deserialized properly
*/
public void testOneOf() throws Exception
{
String schemaStr = "{\n" +
" \"id\": \"http://some.site.somewhere/entry-schema#\",\n" +
" \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" +
" \"description\": \"schema for an fstab entry\",\n" +
" \"type\": \"object\",\n" +
//" \"required\": [ \"storage\" ],\n" +
" \"properties\": {\n" +
" \"storage\": {\n" +
" \"type\": \"object\",\n" +
" \"oneOf\": [\n" +
" { \"$ref\": \"#/definitions/diskDevice\" },\n" +
" { \"$ref\": \"#/definitions/diskUUID\" },\n" +
" { \"$ref\": \"#/definitions/nfs\" },\n" +
" { \"$ref\": \"#/definitions/tmpfs\" }\n" +
" ]\n" +
" },\n" +
" \"fstype\": {\n" +
" \"type\": \"object\",\n" +
" \"enum\": [ \"ext3\", \"ext4\", \"btrfs\" ]\n" +
" },\n" +
" \"options\": {\n" +
" \"type\": \"array\",\n" +
" \"minItems\": 1,\n" +
" \"items\": { \"type\": \"string\" },\n" +
" \"uniqueItems\": true\n" +
" },\n" +
" \"readonly\": { \"type\": \"boolean\" }\n" +
" }\n" +
// " \"definitions\": {\n" +
// " \"diskDevice\": {},\n" +
// " \"diskUUID\": {},\n" +
// " \"nfs\": {},\n" +
// " \"tmpfs\": {}\n" +
// " }\n" +
"}";
ObjectSchema schema = MAPPER.readValue(schemaStr, ObjectSchema.class);
assertNotNull(schema.getProperties().get("storage").asObjectSchema().getOneOf());
assertEquals(4,schema.getProperties().get("storage").asObjectSchema().getOneOf().size());
}
}

0 comments on commit 8d79756

Please sign in to comment.