@Test public void testReferenceIsObjectInCompositeString() throws IOException { final String FIELD_NAME = "testField"; JsonNode rootNode = objectMapper.readTree(String.format("{\"%1$s\":\"-@<sampleValue::/testField>-\"}", FIELD_NAME)); Map<String, JsonNode> referenceMap = new HashMap<>(); referenceMap.put("sampleValue", objectMapper.readTree(String.format("{\"%1$s\":{ \"theField\":\"theValue\"}}", FIELD_NAME))); ObjectNodeReplacer objectNodeReplacer = new ObjectNodeReplacer() .configureField(rootNode, FIELD_NAME); JsonNode fieldNode = rootNode.get(FIELD_NAME); ReferenceReplacementResult replacementResult = new ReferenceReplacementResult(); jsonReferenceReplacer.performTokenReplacement(fieldNode, objectNodeReplacer, referenceMap, replacementResult); Assert.assertTrue(replacementResult.getInvalidReferences().contains("sampleValue cannot be used in composite string")); }
protected void performTokenReplacement(JsonNode node, JsonNodeReplacer jsonNodeReplacer, Map<String, JsonNode> parameterMap, ReferenceReplacementResult report) { final String nodeValue = node.asText(); if (nodeValue != null) { boolean fullNodeReplace = false; Matcher matcher = getMatcher(nodeValue); if (matcher.matches()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { fullNodeReplace = true; if(parameterValue.isObject() || parameterValue.isArray() || parameterValue.isTextual() || parameterValue.isNumber() || parameterValue == NullNode.getInstance()) { jsonNodeReplacer.updateValue(parameterValue); } } } if(!fullNodeReplace) { matcher.reset(); Map<String, String> replacementValues = new HashMap<>(); while(matcher.find()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { if(!parameterValue.isValueNode()) { report.addInvalidReference(String.format("%1$s cannot be used in composite string", referenceName)); } else if(parameterValue.isValueNode()) { replacementValues.put(matcher.group(0), parameterValue.asText()); } } } if(replacementValues.size() > 0) { String newValue = nodeValue; for(Map.Entry<String,String> kvp : replacementValues.entrySet()) { newValue = StringUtils.replace(newValue, kvp.getKey(), kvp.getValue()); } jsonNodeReplacer.updateValue(newValue); } } } }
JsonReferenceReplacer { protected void performTokenReplacement(JsonNode node, JsonNodeReplacer jsonNodeReplacer, Map<String, JsonNode> parameterMap, ReferenceReplacementResult report) { final String nodeValue = node.asText(); if (nodeValue != null) { boolean fullNodeReplace = false; Matcher matcher = getMatcher(nodeValue); if (matcher.matches()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { fullNodeReplace = true; if(parameterValue.isObject() || parameterValue.isArray() || parameterValue.isTextual() || parameterValue.isNumber() || parameterValue == NullNode.getInstance()) { jsonNodeReplacer.updateValue(parameterValue); } } } if(!fullNodeReplace) { matcher.reset(); Map<String, String> replacementValues = new HashMap<>(); while(matcher.find()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { if(!parameterValue.isValueNode()) { report.addInvalidReference(String.format("%1$s cannot be used in composite string", referenceName)); } else if(parameterValue.isValueNode()) { replacementValues.put(matcher.group(0), parameterValue.asText()); } } } if(replacementValues.size() > 0) { String newValue = nodeValue; for(Map.Entry<String,String> kvp : replacementValues.entrySet()) { newValue = StringUtils.replace(newValue, kvp.getKey(), kvp.getValue()); } jsonNodeReplacer.updateValue(newValue); } } } } }
JsonReferenceReplacer { protected void performTokenReplacement(JsonNode node, JsonNodeReplacer jsonNodeReplacer, Map<String, JsonNode> parameterMap, ReferenceReplacementResult report) { final String nodeValue = node.asText(); if (nodeValue != null) { boolean fullNodeReplace = false; Matcher matcher = getMatcher(nodeValue); if (matcher.matches()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { fullNodeReplace = true; if(parameterValue.isObject() || parameterValue.isArray() || parameterValue.isTextual() || parameterValue.isNumber() || parameterValue == NullNode.getInstance()) { jsonNodeReplacer.updateValue(parameterValue); } } } if(!fullNodeReplace) { matcher.reset(); Map<String, String> replacementValues = new HashMap<>(); while(matcher.find()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { if(!parameterValue.isValueNode()) { report.addInvalidReference(String.format("%1$s cannot be used in composite string", referenceName)); } else if(parameterValue.isValueNode()) { replacementValues.put(matcher.group(0), parameterValue.asText()); } } } if(replacementValues.size() > 0) { String newValue = nodeValue; for(Map.Entry<String,String> kvp : replacementValues.entrySet()) { newValue = StringUtils.replace(newValue, kvp.getKey(), kvp.getValue()); } jsonNodeReplacer.updateValue(newValue); } } } } JsonReferenceReplacer();  JsonReferenceReplacer(String referencePrefix, String referenceSeparator, String referenceSuffix, String fallbackValueSeparator); }
JsonReferenceReplacer { protected void performTokenReplacement(JsonNode node, JsonNodeReplacer jsonNodeReplacer, Map<String, JsonNode> parameterMap, ReferenceReplacementResult report) { final String nodeValue = node.asText(); if (nodeValue != null) { boolean fullNodeReplace = false; Matcher matcher = getMatcher(nodeValue); if (matcher.matches()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { fullNodeReplace = true; if(parameterValue.isObject() || parameterValue.isArray() || parameterValue.isTextual() || parameterValue.isNumber() || parameterValue == NullNode.getInstance()) { jsonNodeReplacer.updateValue(parameterValue); } } } if(!fullNodeReplace) { matcher.reset(); Map<String, String> replacementValues = new HashMap<>(); while(matcher.find()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { if(!parameterValue.isValueNode()) { report.addInvalidReference(String.format("%1$s cannot be used in composite string", referenceName)); } else if(parameterValue.isValueNode()) { replacementValues.put(matcher.group(0), parameterValue.asText()); } } } if(replacementValues.size() > 0) { String newValue = nodeValue; for(Map.Entry<String,String> kvp : replacementValues.entrySet()) { newValue = StringUtils.replace(newValue, kvp.getKey(), kvp.getValue()); } jsonNodeReplacer.updateValue(newValue); } } } } JsonReferenceReplacer();  JsonReferenceReplacer(String referencePrefix, String referenceSeparator, String referenceSuffix, String fallbackValueSeparator); String getPrefix(); String getSuffix(); String getSeparator(); String generateReference(String referenceName, String jsonPtrExpr); JsonReferenceComponents extractComponents(String input); ReferenceReplacementResult replaceReferences(JsonNode rootNode, Map<String, JsonNode> referenceMap); ArrayNodeReplacer getArrayNodeReplacer(); void setArrayNodeReplacer(ArrayNodeReplacer arrayNodeReplacer); ObjectNodeReplacer getObjectNodeReplacer(); void setObjectNodeReplacer(ObjectNodeReplacer objectNodeReplacer); String getReferenceName(String reference); }
JsonReferenceReplacer { protected void performTokenReplacement(JsonNode node, JsonNodeReplacer jsonNodeReplacer, Map<String, JsonNode> parameterMap, ReferenceReplacementResult report) { final String nodeValue = node.asText(); if (nodeValue != null) { boolean fullNodeReplace = false; Matcher matcher = getMatcher(nodeValue); if (matcher.matches()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { fullNodeReplace = true; if(parameterValue.isObject() || parameterValue.isArray() || parameterValue.isTextual() || parameterValue.isNumber() || parameterValue == NullNode.getInstance()) { jsonNodeReplacer.updateValue(parameterValue); } } } if(!fullNodeReplace) { matcher.reset(); Map<String, String> replacementValues = new HashMap<>(); while(matcher.find()) { String referenceName = matcher.group(REFERENCE_GROUP_NAME); String referencePath = matcher.group(PATH_GROUP_NAME); JsonNode parameterValue = getParameterValue(parameterMap, referenceName, referencePath, report); if (parameterValue != null) { if(!parameterValue.isValueNode()) { report.addInvalidReference(String.format("%1$s cannot be used in composite string", referenceName)); } else if(parameterValue.isValueNode()) { replacementValues.put(matcher.group(0), parameterValue.asText()); } } } if(replacementValues.size() > 0) { String newValue = nodeValue; for(Map.Entry<String,String> kvp : replacementValues.entrySet()) { newValue = StringUtils.replace(newValue, kvp.getKey(), kvp.getValue()); } jsonNodeReplacer.updateValue(newValue); } } } } JsonReferenceReplacer();  JsonReferenceReplacer(String referencePrefix, String referenceSeparator, String referenceSuffix, String fallbackValueSeparator); String getPrefix(); String getSuffix(); String getSeparator(); String generateReference(String referenceName, String jsonPtrExpr); JsonReferenceComponents extractComponents(String input); ReferenceReplacementResult replaceReferences(JsonNode rootNode, Map<String, JsonNode> referenceMap); ArrayNodeReplacer getArrayNodeReplacer(); void setArrayNodeReplacer(ArrayNodeReplacer arrayNodeReplacer); ObjectNodeReplacer getObjectNodeReplacer(); void setObjectNodeReplacer(ObjectNodeReplacer objectNodeReplacer); String getReferenceName(String reference); static final String DEFAULT_REFERENCE_PREFIX; static final String DEFAULT_REFERENCE_SUFFIX; static final String DEFAULT_REFERENCE_SEPARATOR; static final String DEFAULT_FALLBACK_VALUE_SEPARATOR; static final String JACKSON_PATH_SEPARATOR; static final String REFERENCE_GROUP_NAME; static final String PATH_GROUP_NAME; }