|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.networknt.schema.walk.JsonSchemaWalkListener; |
| 6 | +import com.networknt.schema.walk.WalkEvent; |
| 7 | +import com.networknt.schema.walk.WalkFlow; |
| 8 | +import org.junit.jupiter.api.Assertions; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +/** |
| 17 | + * Validating anyOf walker |
| 18 | + */ |
| 19 | +public class Issue451Test { |
| 20 | + protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) { |
| 21 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); |
| 22 | + SchemaValidatorsConfig svc = new SchemaValidatorsConfig(); |
| 23 | + svc.addPropertyWalkListener(new CountingWalker()); |
| 24 | + return factory.getSchema(schemaContent, svc); |
| 25 | + } |
| 26 | + |
| 27 | + protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { |
| 28 | + ObjectMapper mapper = new ObjectMapper(); |
| 29 | + return mapper.readTree(content); |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + @Test |
| 34 | + public void shouldWalkAnyOfProperties() throws Exception { |
| 35 | + String schemaPath = "/schema/issue451-v7.json"; |
| 36 | + InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath); |
| 37 | + JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream); |
| 38 | + |
| 39 | + schema.walk(null, false); |
| 40 | + |
| 41 | + Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451"); |
| 42 | + Assertions.assertEquals(2, collector.get("#/definitions/definition1/properties/a")); |
| 43 | + Assertions.assertEquals(2, collector.get("#/definitions/definition2/properties/x")); |
| 44 | + } |
| 45 | + |
| 46 | + private static class CountingWalker implements JsonSchemaWalkListener { |
| 47 | + @Override |
| 48 | + public WalkFlow onWalkStart(WalkEvent walkEvent) { |
| 49 | + String path = walkEvent.getSchemaPath(); |
| 50 | + collector().compute(path, (k, v) -> v == null ? 1 : v + 1); |
| 51 | + return WalkFlow.CONTINUE; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages) { |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + private Map<String, Integer> collector() { |
| 60 | + Map<String, Integer> collector = (Map<String, Integer>) CollectorContext.getInstance().get("collector-451"); |
| 61 | + if(collector == null) { |
| 62 | + collector = new HashMap<>(); |
| 63 | + CollectorContext.getInstance().add("collector-451", collector); |
| 64 | + } |
| 65 | + |
| 66 | + return collector; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
0 commit comments