Repository: networknt/json-schema-validator (java)

Issue:
`JsonSchema.getSubSchema()` fails with JsonPointer containing a number
The following test fails:
```java
    /**
     * 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.

Pull request:
Fix json pointer with number in fragment
Closes #1076 

Patch:
diff --git a/src/main/java/com/networknt/schema/utils/JsonNodes.java b/src/main/java/com/networknt/schema/utils/JsonNodes.java
index f1f8947..3bfd9aa 100644
--- a/src/main/java/com/networknt/schema/utils/JsonNodes.java
+++ b/src/main/java/com/networknt/schema/utils/JsonNodes.java
@@ -67,7 +67,7 @@ public class JsonNodes {
     @SuppressWarnings("unchecked")
     public static <T extends JsonNode> T get(JsonNode node, Object propertyOrIndex) {
         JsonNode value = null;
-        if (propertyOrIndex instanceof Number) {
+        if (propertyOrIndex instanceof Number && node.isArray()) {
             value = node.get(((Number) propertyOrIndex).intValue());
         } else {
             value = node.get(propertyOrIndex.toString());


Test patch:
diff --git a/src/test/java/com/networknt/schema/oas/OpenApi30Test.java b/src/test/java/com/networknt/schema/oas/OpenApi30Test.java
index 43256d2..e00abb4 100644
--- a/src/test/java/com/networknt/schema/oas/OpenApi30Test.java
+++ b/src/test/java/com/networknt/schema/oas/OpenApi30Test.java
@@ -16,6 +16,7 @@
 package com.networknt.schema.oas;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 import java.util.List;
 import java.util.Set;
@@ -64,4 +65,16 @@ class OpenApi30Test {
         assertEquals("required", list.get(1).getType());
         assertEquals("bark", list.get(1).getProperty());
     }
+
+    /**
+     * Tests that schema location with number in fragment can resolve.
+     */
+    @Test
+    void jsonPointerWithNumberInFragment() {
+        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V7, builder -> builder
+                .metaSchema(OpenApi30.getInstance()).defaultMetaSchemaIri(OpenApi30.getInstance().getIri()));
+        JsonSchema schema = factory.getSchema(SchemaLocation.of(
+                "classpath:schema/oas/3.0/petstore.yaml#/paths/~1pet/post/responses/200/content/application~1json/schema"));
+        assertNotNull(schema);
+    }
 }