Skip to content

Commit

Permalink
Fix #117: Use LinkedHashSet to deserialize enums on ValueTypeSchema.
Browse files Browse the repository at this point in the history
  • Loading branch information
tapina authored and cowtowncoder committed Oct 21, 2016
1 parent bae539d commit a948d01
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import java.util.*;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;

/**
* This class represents a {@link JsonSchema}
* A primitive type.
* This class represents a {@link JsonSchema}
* A primitive type.
*/
public abstract class ValueTypeSchema extends SimpleTypeSchema
{
Expand All @@ -22,8 +23,9 @@ of enum values uses the same algorithm as defined in "uniqueItems"
(Section 5.15).
*/
@JsonProperty(value = "enum")
@JsonDeserialize(as = LinkedHashSet.class)
protected Set<String> enums = new LinkedHashSet<String>();

/**
* This property defines the type of data, content type, or microformat to
* be expected in the instance property values. A format attribute MAY be
Expand All @@ -32,7 +34,7 @@ of enum values uses the same algorithm as defined in "uniqueItems"
* to primitive types (string, integer, number, or boolean). Validators MAY
* (but are not required to) validate that the instance values conform to a
* format.
*
*
* Additional custom formats MAY be created. These custom formats MAY be
* expressed as an URI, and this URI MAY reference a schema of that
*<p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.module.jsonSchema;

import java.util.Iterator;
import java.util.Set;

import com.fasterxml.jackson.databind.*;
Expand Down Expand Up @@ -47,4 +48,41 @@ public void testEnumArrayDeserialization() throws Exception

assertTrue(enums.contains("FOO"));
}

public void testEnumArrayDeserializationOrdering() throws Exception {
final String jsonSchema = "{\n" +
" \"type\": \"object\",\n" +
" \"id\": \"https://foo.bar/wibble\",\n" +
" \"$schema\": \"http://json-schema.org/draft-03/schema#\",\n" +
" \"properties\": {\n" +
" \"testOptions\": {\n" +
" \"type\": \"array\",\n" +
" \"id\": \"testOptions\",\n" +
" \"required\":true,\n" +
" \"items\": {\n" +
" \"type\": \"string\",\n" +
" \"enum\": [\n" +
" \"Section 1 'Macaroni and Cheese'\",\n" +
" \"Section 2 'Spaghetti and Meatballs'\",\n" +
" \"Section 3 'Fish and Chips'\",\n" +
" \"Section 4 'Sausage and Mash'\"\n" +
" ]\n" +
" },\n" +
" \"minItems\": 1\n" +
" }\n" +
" }\n" +
"}";

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonSchema);
JsonSchema deserialized = mapper.convertValue(jsonNode, JsonSchema.class);

ArraySchema testOptionsSchema = deserialized.asObjectSchema().getProperties().get("testOptions").asArraySchema();
ValueTypeSchema testOptionItemsSchema = testOptionsSchema.getItems().asSingleItems().getSchema().asValueTypeSchema();
Iterator<String> enumSet = testOptionItemsSchema.getEnums().iterator();
assertEquals("Expect enum options in order", "Section 1 'Macaroni and Cheese'", enumSet.next());
assertEquals("Expect enum options in order", "Section 2 'Spaghetti and Meatballs'", enumSet.next());
assertEquals("Expect enum options in order", "Section 3 'Fish and Chips'", enumSet.next());
assertEquals("Expect enum options in order", "Section 4 'Sausage and Mash'", enumSet.next());
}
}

0 comments on commit a948d01

Please sign in to comment.