• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.networknt.schema;
2 
3 import static org.junit.jupiter.api.Assertions.assertLinesMatch;
4 
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.List;
8 import java.util.Optional;
9 import java.util.Set;
10 
11 import org.junit.jupiter.api.Test;
12 
13 import com.fasterxml.jackson.core.JsonProcessingException;
14 import com.fasterxml.jackson.databind.JsonNode;
15 import com.fasterxml.jackson.databind.ObjectMapper;
16 import com.networknt.schema.SpecVersion.VersionFlag;
17 import com.networknt.schema.walk.JsonSchemaWalkListener;
18 import com.networknt.schema.walk.WalkEvent;
19 import com.networknt.schema.walk.WalkFlow;
20 
21 class Issue724Test {
22 
23     @Test
test()24     void test() throws JsonProcessingException {
25         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
26         StringCollector stringCollector = new StringCollector();
27         config.addKeywordWalkListener(stringCollector);
28 
29         String schema =
30             "{\n"
31                 + "  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n"
32                 + "  \"type\" : \"object\",\n"
33                 + "  \"properties\" : {\n"
34                 + "    \"credit_card\": {\n"
35                 + "      \"type\" : \"string\"\n"
36                 + "    }\n"
37                 + "  },\n"
38                 + "  \"dependentSchemas\": {\n"
39                 + "    \"credit_card\": {\n"
40                 + "      \"properties\": {\n"
41                 + "        \"billing_address\": {\n"
42                 + "          \"type\" : \"string\"\n"
43                 + "        }\n"
44                 + "      },\n"
45                 + "      \"required\": [\"billing_address\"]\n"
46                 + "    }\n"
47                 + "  }\n"
48                 + "}\n";
49         String data =
50             "{\n"
51                 + "  \"credit_card\" : \"my_credit_card\",\n"
52                 + "  \"billing_address\" : \"my_billing_address\"\n"
53                 + "}\n";
54 
55         JsonSchema jsonSchema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schema, config);
56         jsonSchema.walk(new ObjectMapper().readTree(data), /* shouldValidateSchema= */ false);
57 
58         System.out.println(stringCollector.strings);
59         assertLinesMatch(Arrays.asList("my_credit_card", "my_billing_address"), stringCollector.strings);
60     }
61 
62     static class StringCollector implements JsonSchemaWalkListener {
63         final List<String> strings = new ArrayList<>();
64 
65         @Override
onWalkStart(WalkEvent walkEvent)66         public WalkFlow onWalkStart(WalkEvent walkEvent) {
67             boolean isString =
68                 Optional.of(walkEvent.getSchema().getSchemaNode())
69                     .map(jsonNode -> jsonNode.get("type"))
70                     .map(JsonNode::asText)
71                     .map(type -> type.equals("string"))
72                     .orElse(false);
73 
74             if (isString) {
75                 this.strings.add(walkEvent.getInstanceNode().asText());
76             }
77 
78             return WalkFlow.CONTINUE;
79         }
80 
81         @Override
onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages)82         public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages) {
83             // nothing to do here
84         }
85     }
86 
87 }
88