@DataProvider public Iterator<Object[]> testData() throws ProcessingException, JsonPointerException { final List<Object[]> list = Lists.newArrayList(); JsonNode digest; String memberName; List<JsonPointer> ret; for (final JsonNode node: testNode) { digest = node.get("digest"); memberName = node.get("memberName").textValue(); ret = Lists.newArrayList(); for (final JsonNode element: node.get("ret")) ret.add(new JsonPointer(element.textValue())); list.add(new Object[]{ digest, memberName, ret }); } return list.iterator(); }
final JsonNode schemas = tree.getNode().get(keyword); final int size = schemas.size(); final ObjectNode fullReport = FACTORY.objectNode(); subReport = new ListProcessingReport(report.getLogLevel(), LogLevel.FATAL); ptr = schemaPointer.append(JsonPointer.of(keyword, index)); newData = data.withSchema(tree.setPointer(ptr)); processor.process(subReport, newData); fullReport.put(ptr.toString(), subReport.asJson()); if (subReport.isSuccess()) nrSuccess++; report.error(newMsg(data, bundle, "err.draftv4.allOf.fail") .putArgument("matched", nrSuccess) .putArgument("nrSchemas", size) .put("reports", fullReport));
/** * Build an instance validator tied to a schema * * <p>Note that the validity of the schema is <b>not</b> checked. Use {@link * #getSyntaxValidator()} if you are not sure.</p> * * @param schema the schema * @return a {@link JsonSchema} * @throws ProcessingException schema is a {@link MissingNode} * @throws NullPointerException schema is null */ public JsonSchema getJsonSchema(final JsonNode schema) throws ProcessingException { BUNDLE.checkNotNull(schema, "nullSchema"); return validator.buildJsonSchema(schema, JsonPointer.empty()); }
final int size = node.size(); ldo = getNode(tree).get(index); type = NodeType.getNodeType(ldo); if (type != NodeType.OBJECT) { report.error(LDOMsg(tree, bundle, "draftv4.ldo.incorrectType", index) .put("expected", NodeType.OBJECT) .putArgument("found", type)); continue; set = Sets.newHashSet(ldo.fieldNames()); list = Lists.newArrayList(REQUIRED_LDO_PROPERTIES); list.removeAll(set); final ProcessingMessage msg = LDOMsg(tree, bundle, "draftv4.ldo.missingRequired", index); report.error(msg.put("required", REQUIRED_LDO_PROPERTIES) .putArgument("missing", list)); continue; if (ldo.has("schema")) pointers.add(JsonPointer.of(keyword, index, "schema")); if (ldo.has("targetSchema")) pointers.add(JsonPointer.of(keyword, index, "targetSchema")); checkLDO(report, bundle, tree, index);
@Override protected void checkKoKo(final ProcessingReport report) throws ProcessingException { final ArgumentCaptor<ProcessingMessage> captor = ArgumentCaptor.forClass(ProcessingMessage.class); verify(report).error(captor.capture()); final ProcessingMessage message = captor.getValue(); final ObjectNode reports = FACTORY.objectNode(); final ArrayNode oneReport = FACTORY.arrayNode(); oneReport.add(MSG.asJson()); reports.put(ptr1.toString(), oneReport); reports.put(ptr2.toString(), oneReport); assertMessage(message) .isValidationError(keyword, BUNDLE.printf("err.common.schema.noMatch", 2)) .hasField("reports", reports); }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final SchemaTree tree = data.getSchema(); final JsonNode node = tree.getNode().get(keyword); FullData newData; if (node.isObject()) { newData = data.withSchema(tree.append(JsonPointer.of(keyword))); processor.process(report, newData); return; } /* * Not an object? An array */ final int size = node.size(); JsonPointer pointer; for (int index = 0; index < size; index++) { pointer = JsonPointer.of(keyword, index); newData = data.withSchema(tree.append(pointer)); processor.process(report, newData); } }
@DataProvider public Iterator<Object[]> testData() throws ProcessingException, JsonPointerException { final List<Object[]> list = Lists.newArrayList(); JsonNode digest; int elementIndex; List<JsonPointer> ret; for (final JsonNode node: testNode) { digest = node.get("digest"); elementIndex = node.get("elementIndex").intValue(); ret = Lists.newArrayList(); for (final JsonNode element: node.get("ret")) ret.add(new JsonPointer(element.textValue())); list.add(new Object[]{ digest, elementIndex, ret }); } return list.iterator(); }
private static void computeArray(final Map<JsonPointer, JsonNode> ret, final JsonPointer pointer, final JsonNode source, final JsonNode target) { final int size = Math.min(source.size(), target.size()); for (int i = 0; i < size; i++) computeUnchanged(ret, pointer.append(i), source.get(i), target.get(i)); } }
if (path.path(node).isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); final JsonNode replacement = value.deepCopy(); if (path.isEmpty()) return replacement; final JsonNode ret = node.deepCopy(); final JsonNode parent = path.parent().get(ret); final String rawToken = Iterables.getLast(path).getToken().getRaw(); if (parent.isObject()) ((ObjectNode) parent).put(rawToken, replacement); else ((ArrayNode) parent).set(Integer.parseInt(rawToken), replacement);
/** * Apply a migrator to the node at the current pointer * * <p>It is assumed here that the current node is a JSON Object.</p> * * @param migrator the migrator to apply * @throws SwaggerMigrationException current node is not an object, or the * migrator failed to apply */ public void applyMigrator(final SwaggerMigrator migrator) throws SwaggerMigrationException { final JsonPointer parent = currentPointer.parent(); if (!parent.get(baseNode).isObject()) { throw new SwaggerMigrationException(); } final ObjectNode parentNode = (ObjectNode) parent.get(baseNode); final JsonNode patched = migrator.migrate(currentNode); if (currentPointer.isEmpty()) { baseNode = currentNode = patched; return; } final String memberName = Iterables.getLast(currentPointer) .getToken().getRaw(); parentNode.put(memberName, patched); }
@Nonnull @Override public JsonNode migrate(@Nonnull final JsonNode input) throws SwaggerMigrationException { Objects.requireNonNull(input); if (!input.has("allowableValues")) { return input; } final String valueType = JsonPointer.of("allowableValues", "valueType") .get(input).textValue(); if ("LIST".equals(valueType)) { return SwaggerMigrators.fromPatch(PATCH).migrate(input); } // TODO: unsupported type of allowableValues! return input; }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (path.isEmpty()) return MissingNode.getInstance(); if (path.path(node).isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); final JsonNode ret = node.deepCopy(); final JsonNode parentNode = path.parent().get(ret); final String raw = Iterables.getLast(path).getToken().getRaw(); if (parentNode.isObject()) ((ObjectNode) parentNode).remove(raw); else ((ArrayNode) parentNode).remove(Integer.parseInt(raw)); return ret; }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (path.isEmpty()) return value; /* * Check the parent node: it must exist and be a container (ie an array * or an object) for the add operation to work. */ final JsonNode parentNode = path.parent().path(node); if (parentNode.isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchParent")); if (!parentNode.isContainerNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.parentNotContainer")); return parentNode.isArray() ? addToArray(path, node) : addToObject(path, node); }
@Override protected void checkValue(final Collection<JsonPointer> pointers, final MessageBundle bundle, final ProcessingReport report, final SchemaTree tree) throws ProcessingException { final int size = getNode(tree).size(); if (size == 0) { report.error(newMsg(tree, bundle, "common.array.empty")); return; } for (int index = 0; index < size; index++) pointers.add(JsonPointer.of(keyword, index)); } }
@Override public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final String value = data.getInstance().getNode().textValue(); try { new JsonPointer(value); } catch (JsonPointerException ignored) { report.error(newMsg(data, bundle, "err.format.jsonpointer.invalid") .putArgument("value", value)); } } }
private void processArray(final ProcessingReport report, final FullData input) throws ProcessingException { final SchemaTree tree = input.getSchema(); final JsonTree instance = input.getInstance(); final JsonNode schema = tree.getNode(); final JsonNode node = instance.getNode(); final JsonNode digest = ArraySchemaDigester.getInstance() .digest(schema); final ArraySchemaSelector selector = new ArraySchemaSelector(digest); final int size = node.size(); FullData data; JsonTree newInstance; for (int index = 0; index < size; index++) { newInstance = instance.append(JsonPointer.of(index)); data = input.withInstance(newInstance); for (final JsonPointer ptr: selector.selectSchemas(index)) { data = data.withSchema(tree.append(ptr)); process(report, data); } } }
private void doSetPointer(final JsonPointer pointer) { currentPointer = pointer; currentNode = currentPointer.path(baseNode); Preconditions.checkArgument(!currentNode.isMissingNode(), "base node has no element at pointer " + currentPointer); }
final Set<String> fields = Sets.newHashSet(instance.fieldNames()); set.removeAll(fields); if (!set.isEmpty()) report.error(newMsg(data, bundle, "err.common.dependencies.missingPropertyDeps") .putArgument("property", field) .putArgument("required", toArrayNode(collection)) .putArgument("missing", toArrayNode(set))); if (!fields.contains(field)) continue; pointer = JsonPointer.of(keyword, field); newData = data.withSchema(tree.append(pointer)); processor.process(report, newData);
private void processObject(final ProcessingReport report, final FullData input) throws ProcessingException { final SchemaTree tree = input.getSchema(); final JsonTree instance = input.getInstance(); final JsonNode schema = tree.getNode(); final JsonNode node = instance.getNode(); final JsonNode digest = ObjectSchemaDigester.getInstance() .digest(schema); final ObjectSchemaSelector selector = new ObjectSchemaSelector(digest); final List<String> fields = Lists.newArrayList(node.fieldNames()); Collections.sort(fields); FullData data; JsonTree newInstance; for (final String field: fields) { newInstance = instance.append(JsonPointer.of(field)); data = input.withInstance(newInstance); for (final JsonPointer ptr: selector.selectSchemas(field)) { data = data.withSchema(tree.append(ptr)); process(report, data); } } }