Skip to content

Deserialisation of enums does not respect ordering #117

@tapina

Description

@tapina

Deserialisation of enums is to a Set field on ValueTypeSchema. This is created as a HashSet which leads to unpredictable ordering. Here is a (failing) test case:

    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());
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions