Skip to content

JsonSchema.getSubSchema() fails with JsonPointer containing a number #1076

@Mahoney

Description

@Mahoney

The following test fails:

    /**
     * Test can validate against a schema located under a response code.
     */
    @Test
    void canValidateSchemaUnderAResponse() {
        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
                builder -> builder.metaSchema(OpenApi31.getInstance())
                        .metaSchemaFactory(DisallowUnknownJsonMetaSchemaFactory.getInstance()));
        JsonSchema schema = factory
                .getSchema(SchemaLocation.of("classpath:schema/oas/3.0/petstore.yaml#/paths/~1pet/post/responses/200/content/application~1json/schema"));
        String input = "{\r\n"
                + "  \"petType\": \"dog\""
                + "}";
        Set<ValidationMessage> messages = schema.validate(input, InputFormat.JSON);
        assertEquals(4, messages.size());
    }

Stacktrace:

com.networknt.schema.InvalidSchemaRefException: /paths/~1pet/post/responses: Reference /paths/~1pet/post/responses/200/content/application~1json/schema cannot be resolved

	at com.networknt.schema.JsonSchema.getSubSchema(JsonSchema.java:415)
	at com.networknt.schema.JsonSchema.getRefSchema(JsonSchema.java:330)
	at com.networknt.schema.JsonSchemaFactory.getMappedSchema(JsonSchemaFactory.java:693)
	at com.networknt.schema.JsonSchemaFactory.loadSchema(JsonSchemaFactory.java:639)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:617)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:743)
	at com.networknt.schema.oas.OpenApi30Test.canValidateSchemaUnderAResponse(OpenApi30Test.java:77)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

The problem is that JsonNodePath.getElement(int index) is returning an Integer for 200, and treating it as an array index, when it should also treat it as a valid String key name to look up a value on an object.

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