From 3d3c579e5bd91626045c424e42a338c9a00acd78 Mon Sep 17 00:00:00 2001 From: Gareth Boden Date: Mon, 17 Oct 2016 12:02:51 +0100 Subject: [PATCH] Fix #117: Use LinkedHashSet to deserialize enums on ValueTypeSchema. --- .../jsonSchema/types/ValueTypeSchema.java | 10 +++-- .../module/jsonSchema/EnumSchemaTest.java | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/module/jsonSchema/types/ValueTypeSchema.java b/src/main/java/com/fasterxml/jackson/module/jsonSchema/types/ValueTypeSchema.java index 98d24b5d..2e8fa6e9 100644 --- a/src/main/java/com/fasterxml/jackson/module/jsonSchema/types/ValueTypeSchema.java +++ b/src/main/java/com/fasterxml/jackson/module/jsonSchema/types/ValueTypeSchema.java @@ -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 { @@ -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 enums = new LinkedHashSet(); - + /** * This property defines the type of data, content type, or microformat to * be expected in the instance property values. A format attribute MAY be @@ -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 *

diff --git a/src/test/java/com/fasterxml/jackson/module/jsonSchema/EnumSchemaTest.java b/src/test/java/com/fasterxml/jackson/module/jsonSchema/EnumSchemaTest.java index 823cfc0f..3d1c9bf5 100644 --- a/src/test/java/com/fasterxml/jackson/module/jsonSchema/EnumSchemaTest.java +++ b/src/test/java/com/fasterxml/jackson/module/jsonSchema/EnumSchemaTest.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.module.jsonSchema; +import java.util.Iterator; import java.util.Set; import com.fasterxml.jackson.databind.*; @@ -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 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()); + } }