Skip to content

oneOf and discriminator value mismatch doesn't fail #1225

@ragboyjr

Description

@ragboyjr

This is kind of similar to #1087

Here's an example test case that highlights the problem:

    @Test
    void oneOfDiscriminatorEnabledWithDiscriminatorMismatch() {
        String schemaData = "{\r\n"
                + "  \"discriminator\": {\r\n"
                + "    \"propertyName\": \"type\",\r\n"
                + "    \"mapping\": {\r\n"
                + "      \"string\": \"#/$defs/string\",\r\n"
                + "      \"number\": \"#/$defs/number\"\r\n"
                + "    }\r\n"
                + "  },\r\n"
                + "  \"oneOf\": [\r\n"
                + "    {\r\n"
                + "      \"$ref\": \"#/$defs/string\"\r\n"
                + "    },\r\n"
                + "    {\r\n"
                + "      \"$ref\": \"#/$defs/number\"\r\n"
                + "    }\r\n"
                + "  ],\r\n"
                + "  \"$defs\": {\r\n"
                + "    \"string\": {\r\n"
                + "      \"properties\": {\r\n"
                + "        \"type\": {\r\n"
                + "          \"type\": \"string\"\r\n"
//                + "          \"enum\": [\"string\"]\r\n"
                + "        },\r\n"
                + "        \"value\": {\r\n"
                + "          \"type\": \"string\"\r\n"
                + "        }\r\n"
                + "      }\r\n"
                + "    },\r\n"
                + "    \"number\": {\r\n"
                + "      \"properties\": {\r\n"
                + "        \"type\": {\r\n"
                + "          \"type\": \"string\"\r\n"
//                + "          \"enum\": [\"number\"]\r\n"
                + "        },\r\n"
                + "        \"value\": {\r\n"
                + "          \"type\": \"number\"\r\n"
                + "        }\r\n"
                + "      }\r\n"
                + "    }\r\n"
                + "  }\r\n"
                + "}";
        Schema schema = SchemaRegistry.withDialect(Dialects.getOpenApi31()).getSchema(schemaData);
        // intended to be invalid because mapping points to string, but it's validating against number type
        String inputData = "{\r\n"
                + "  \"type\": \"string\",\r\n"
                + "  \"value\": 1\r\n"
                + "}";

        // this passes
        assertEquals(Collections.emptyList(), schema.validate(inputData, InputFormat.JSON));
    }

In this example, passing in {"type": "string", "value": 1} passes because this technically is a valid number schema and an invalid string schema. So the oneOf validation correctly passes (only one schema matches), but it's the wrong schema that matches.

I couldn't find anything definitive when reviewing the open api spec to see if this situation was addressed. it feels like this should be an error? but not sure.

This can be resolved by just adding an enum to each schema's discriminator type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions