Skip to content

Commit 8d79756

Browse files
committed
Merge pull request #64 from igreenfield/master
Add support to oneOf
2 parents 8ed26e9 + e17d0b6 commit 8d79756

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/main/java/com/fasterxml/jackson/module/jsonSchema/types/ContainerTypeSchema.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,32 @@ of enum values uses the same algorithm as defined in "uniqueItems"
2424
*/
2525
@JsonProperty(value = "enum", required = true)
2626
private Set<String> enums;
27-
28-
//instance initializer block
27+
28+
//instance initializer block
2929
{
3030
enums = new HashSet<String>();
3131
}
32-
33-
/* (non-Javadoc)
34-
* @see com.fasterxml.jackson.databind.jsonSchema.types.JsonSchema#asContainerSchema()
32+
/**
33+
* This provides an enumeration of all possible values that are valid
34+
for the instance property. This MUST be an array, and each item in
35+
the array represents a possible value for the instance value. If
36+
this attribute is defined, the instance value MUST be one of the
37+
values in the array in order for the schema to be valid. Comparison
38+
of enum values uses the same algorithm as defined in "uniqueItems"
39+
(Section 5.15).
3540
*/
41+
@JsonProperty(value = "oneOf", required = true)
42+
private Set<Object> oneOf;
43+
//instance initializer block
44+
{
45+
oneOf = new HashSet<Object>();
46+
}
47+
48+
49+
50+
/* (non-Javadoc)
51+
* @see com.fasterxml.jackson.databind.jsonSchema.types.JsonSchema#asContainerSchema()
52+
*/
3653
@Override
3754
public ContainerTypeSchema asContainerSchema() { return this; }
3855

@@ -60,4 +77,12 @@ public Set<String> getEnums() {
6077
public void setEnums(Set<String> enums) {
6178
this.enums = enums;
6279
}
80+
81+
public Set<Object> getOneOf() {
82+
return oneOf;
83+
}
84+
85+
public void setOneOf(Set<Object> oneOf) {
86+
this.oneOf = oneOf;
87+
}
6388
}

src/test/java/com/fasterxml/jackson/module/jsonSchema/TestReadJsonSchema.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
1010
import com.fasterxml.jackson.module.jsonSchema.types.ArraySchema;
1111
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema;
12+
import junit.framework.Assert;
1213

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

128129
// assertEquals(node, finalNode);
129-
assertEquals("Schemas for "+name+" differ",
130+
assertEquals("Schemas for " + name + " differ",
130131
json1, json2);
131132
}
132133

@@ -155,4 +156,49 @@ public void testDeserializeTrueAdditionalProperties() throws Exception
155156
ObjectSchema schema = MAPPER.readValue(schemaStr, ObjectSchema.class);
156157
assertNull(schema.getAdditionalProperties());
157158
}
159+
160+
/**
161+
* Verifies that a true-valued additional property is deserialized properly
162+
*/
163+
public void testOneOf() throws Exception
164+
{
165+
String schemaStr = "{\n" +
166+
" \"id\": \"http://some.site.somewhere/entry-schema#\",\n" +
167+
" \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" +
168+
" \"description\": \"schema for an fstab entry\",\n" +
169+
" \"type\": \"object\",\n" +
170+
//" \"required\": [ \"storage\" ],\n" +
171+
" \"properties\": {\n" +
172+
" \"storage\": {\n" +
173+
" \"type\": \"object\",\n" +
174+
" \"oneOf\": [\n" +
175+
" { \"$ref\": \"#/definitions/diskDevice\" },\n" +
176+
" { \"$ref\": \"#/definitions/diskUUID\" },\n" +
177+
" { \"$ref\": \"#/definitions/nfs\" },\n" +
178+
" { \"$ref\": \"#/definitions/tmpfs\" }\n" +
179+
" ]\n" +
180+
" },\n" +
181+
" \"fstype\": {\n" +
182+
" \"type\": \"object\",\n" +
183+
" \"enum\": [ \"ext3\", \"ext4\", \"btrfs\" ]\n" +
184+
" },\n" +
185+
" \"options\": {\n" +
186+
" \"type\": \"array\",\n" +
187+
" \"minItems\": 1,\n" +
188+
" \"items\": { \"type\": \"string\" },\n" +
189+
" \"uniqueItems\": true\n" +
190+
" },\n" +
191+
" \"readonly\": { \"type\": \"boolean\" }\n" +
192+
" }\n" +
193+
// " \"definitions\": {\n" +
194+
// " \"diskDevice\": {},\n" +
195+
// " \"diskUUID\": {},\n" +
196+
// " \"nfs\": {},\n" +
197+
// " \"tmpfs\": {}\n" +
198+
// " }\n" +
199+
"}";
200+
ObjectSchema schema = MAPPER.readValue(schemaStr, ObjectSchema.class);
201+
assertNotNull(schema.getProperties().get("storage").asObjectSchema().getOneOf());
202+
assertEquals(4,schema.getProperties().get("storage").asObjectSchema().getOneOf().size());
203+
}
158204
}

0 commit comments

Comments
 (0)