Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialisation of enums does not respect ordering #117

Closed
tapina opened this issue Oct 17, 2016 · 3 comments
Closed

Deserialisation of enums does not respect ordering #117

tapina opened this issue Oct 17, 2016 · 3 comments
Milestone

Comments

@tapina
Copy link

tapina commented Oct 17, 2016

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());
    }
tapina added a commit to cambridgeweblab/jackson-module-jsonSchema that referenced this issue Oct 17, 2016
@cowtowncoder
Copy link
Member

Seems reasonable to improve this to keep ordering.

@tapina
Copy link
Author

tapina commented Oct 17, 2016

I created a PR with the test and the (one liner) fix.

@cowtowncoder
Copy link
Member

Thank you -- I applied the fix for 2.8(.5).

nealeu added a commit to cambridgeweblab/common-java that referenced this issue Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants