use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class JsonPatchManager method apply.
/**
* Applies a JSON patch to any valid jackson java object. It uses
* valueToTree to convert from the object into a tree like node structure,
* and this is where the patch will be applied. This means that any property
* renaming etc will be followed.
*
* @param patch JsonPatch object with the operations it should apply.
* @param object Jackson Object to apply the patch to.
* @return New instance of the object with the patch applied.
*/
@Transactional
@SuppressWarnings("unchecked")
public <T> T apply(JsonPatch patch, T object) throws JsonPatchException {
if (patch == null || object == null) {
return null;
}
Schema schema = schemaService.getSchema(object.getClass());
JsonNode node = jsonMapper.valueToTree(object);
// since valueToTree does not properly handle our deeply nested classes,
// we need to make another trip to make sure all collections are
// correctly made into json nodes.
handleCollectionUpdates(object, schema, (ObjectNode) node);
node = patch.apply(node);
return (T) jsonToObject(node, object.getClass(), jsonMapper, ex -> new JsonPatchException(ex.getMessage()));
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class BulkPatchValidatorService method checkPatchObject.
/**
* Validate given ID and {@link JsonPatch} by using
* {@link org.hisp.dhis.jsonpatch.validator.BulkPatchValidator} from given
* {@link BulkPatchParameters}.
* <p>
* The valid {@link IdentifiableObject} and {@link JsonPatch} will be added
* to given {@link PatchBundle}
*
* @return {@link ObjectReport} contains {@link ErrorReport} if any.
*/
private ObjectReport checkPatchObject(Schema schema, String id, JsonPatch jsonPatch, BulkPatchParameters patchParams, PatchBundle bundle) {
ObjectReport objectReport = new ObjectReport(schema.getKlass(), 0, id);
IdentifiableObject entity = manager.get((Class<? extends IdentifiableObject>) schema.getKlass(), id);
BulkPatchValidateParams bulkPatchValidateParams = BulkPatchValidateParams.builder().schema(schema).jsonPatch(jsonPatch).id(id).entity(entity).build();
patchParams.getValidators().forEach(validator -> validator.validate(bulkPatchValidateParams, error -> objectReport.addErrorReport(error)));
if (objectReport.hasErrorReports()) {
return objectReport;
}
bundle.addEntity(id, entity, jsonPatch);
return objectReport;
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class BulkPatchValidatorService method validate.
/**
* Validate {@link org.hisp.dhis.jsonpatch.BulkJsonPatch} with by using
* validators from given {@link org.hisp.dhis.jsonpatch.BulkPatchParameters}
*
* @param bulkJsonPatch {@link org.hisp.dhis.jsonpatch.BulkJsonPatch} for
* validating
* @param patchParameters
* {@link org.hisp.dhis.jsonpatch.BulkPatchParameters} contains
* validators
* @return {@link org.hisp.dhis.jsonpatch.PatchBundle} contains validated
* {@link IdentifiableObject} and {@link JsonPatch}
*/
public PatchBundle validate(BulkJsonPatch bulkJsonPatch, BulkPatchParameters patchParameters) {
PatchBundle bundle = new PatchBundle();
Schema schema = schemaService.getSchemaByPluralName(bulkJsonPatch.getClassName());
if (schema == null) {
patchParameters.addTypeReport(createTypeReport(new ErrorReport(JsonPatchException.class, ErrorCode.E6002, bulkJsonPatch.getClassName())));
return bundle;
}
List<ObjectReport> objectReports = new ArrayList<>();
objectReports.addAll(bulkJsonPatch.getIds().stream().map(id -> checkPatchObject(schema, id, bulkJsonPatch.getPatch(), patchParameters, bundle)).filter(objectReport -> objectReport.hasErrorReports()).collect(Collectors.toList()));
if (!objectReports.isEmpty()) {
TypeReport typeReport = new TypeReport(schema.getKlass());
typeReport.setObjectReports(objectReports);
patchParameters.addTypeReport(typeReport);
}
return bundle;
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class BulkPatchValidatorService method validate.
/**
* Validate {@link org.hisp.dhis.jsonpatch.BulkJsonPatches} with by using
* validators from given {@link BulkPatchParameters}
*
* @param patches {@link org.hisp.dhis.jsonpatch.BulkJsonPatches} for
* validating
* @param patchParameters {@link BulkPatchParameters} contains validators
* @return {@link PatchBundle} contains validated {@link IdentifiableObject}
* and {@link JsonPatch}
*/
public PatchBundle validate(BulkJsonPatches patches, BulkPatchParameters patchParameters) {
PatchBundle bundle = new PatchBundle();
for (String className : patches.getClassNames()) {
Schema schema = schemaService.getSchemaByPluralName(className);
if (schema == null) {
patchParameters.addTypeReport(createTypeReport(new ErrorReport(JsonPatchException.class, ErrorCode.E6002, className)));
continue;
}
List<ObjectReport> objectReports = new ArrayList<>();
objectReports.addAll(patches.get(className).entrySet().stream().map(entry -> checkPatchObject(schema, entry.getKey(), entry.getValue(), patchParameters, bundle)).filter(objectReport -> objectReport.hasErrorReports()).collect(Collectors.toList()));
if (!objectReports.isEmpty()) {
TypeReport typeReport = new TypeReport(schema.getKlass());
typeReport.setObjectReports(objectReports);
patchParameters.addTypeReport(typeReport);
}
}
return bundle;
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class JsonPatchManagerTest method testSimpleAddPatchNoPersist.
@Test
void testSimpleAddPatchNoPersist() throws Exception {
Constant constant = createConstant('A', 1.0d);
assertEquals("ConstantA", constant.getName());
assertEquals(constant.getValue(), 0, 1.0d);
JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/name\", \"value\": \"updated\"}," + "{\"op\": \"add\", \"path\": \"/value\", \"value\": 5.0}" + "]", JsonPatch.class);
assertNotNull(patch);
Constant patchedConstant = jsonPatchManager.apply(patch, constant);
assertEquals("ConstantA", constant.getName());
assertEquals(constant.getValue(), 0, 1.0d);
assertEquals("updated", patchedConstant.getName());
assertEquals(patchedConstant.getValue(), 0, 5.0d);
}
Aggregations