[OLINGO-1437]Add additional properties to OData Server Error

This commit is contained in:
ramya vasanth 2020-03-19 10:37:08 +05:30
parent d4c057b80c
commit a3318beed3
5 changed files with 124 additions and 1 deletions

View File

@ -31,6 +31,7 @@ public class ODataError {
private String target;
private List<ODataErrorDetail> details;
private Map<String, String> innerError;
private Map<String, Object> additionalProperties;
/**
* The value for the code name/value pair is a language-independent string. Its value is a service-defined error code.
@ -129,4 +130,22 @@ public class ODataError {
this.innerError = innerError;
return this;
}
/**
* Sets server defined additional properties
* @param additionalProperties
* @return this for method chaining.
*/
public ODataError setAdditionalProperties(final Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
return this;
}
/**
* Gets server defined additional properties.
* @return a pair representing server defined object.
*/
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
}

View File

@ -18,6 +18,8 @@
*/
package org.apache.olingo.commons.api.ex;
import java.util.Map;
/**
* OData details for example <tt>{ "error": {..., "details":[
* {"code": "301","target": "$search" ,"message": "$search query option not supported"}
@ -28,6 +30,7 @@ public class ODataErrorDetail {
private String code;
private String message;
private String target;
private Map<String, Object> additionalProperties;
/**
* Gets error code.
@ -76,4 +79,22 @@ public class ODataErrorDetail {
this.target = target;
return this;
}
/**
* Sets server defined additional properties
* @param additionalProperties additionalProperties
* @return this ODataErrorDetail instance (fluent builder)
*/
public ODataErrorDetail setAdditionalProperties(final Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
return this;
}
/**
* Gets server defined additional properties.
* @return a pair representing server defined object.
*/
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
}

View File

@ -140,4 +140,13 @@ public class ODataServerError extends ODataError {
return this;
}
/**
* Sets server defined key-value pairs.
* @return this for method chaining.
*/
@Override
public ODataServerError setAdditionalProperties(final Map<String, Object> additionalProperties) {
super.setAdditionalProperties(additionalProperties);
return this;
}
}

View File

@ -19,6 +19,9 @@
package org.apache.olingo.server.core.serializer.json;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.ex.ODataError;
@ -40,12 +43,14 @@ public class ODataErrorSerializer {
json.writeStartObject();
writeODataError(json, error.getCode(), error.getMessage(), error.getTarget());
writeODataAdditionalProperties(json, error.getAdditionalProperties());
if (error.getDetails() != null) {
json.writeArrayFieldStart(Constants.ERROR_DETAILS);
for (ODataErrorDetail detail : error.getDetails()) {
json.writeStartObject();
writeODataError(json, detail.getCode(), detail.getMessage(), detail.getTarget());
writeODataAdditionalProperties(json, detail.getAdditionalProperties());
json.writeEndObject();
}
json.writeEndArray();
@ -55,7 +60,31 @@ public class ODataErrorSerializer {
json.writeEndObject();
}
private void writeODataError(final JsonGenerator json, final String code, final String message, final String target)
@SuppressWarnings("unchecked")
private void writeODataAdditionalProperties(JsonGenerator json,
Map<String, Object> additionalProperties) throws IOException {
if (additionalProperties != null) {
for (Entry<String, Object> additionalProperty : additionalProperties.entrySet()) {
Object value = additionalProperty.getValue();
if (value instanceof List) {
List<Map<String, Object>> list = (List<Map<String, Object>>) value;
json.writeArrayFieldStart(additionalProperty.getKey());
for (Map<String, Object> entry : list) {
json.writeStartObject();
writeODataAdditionalProperties(json, entry);
json.writeEndObject();
}
json.writeEndArray();
} else if (value instanceof Map) {
writeODataAdditionalProperties(json, (Map<String, Object>) value);
} else {
json.writeObjectField(additionalProperty.getKey(), value);
}
}
}
}
private void writeODataError(final JsonGenerator json, final String code, final String message, final String target)
throws IOException {
json.writeFieldName(Constants.ERROR_CODE);
if (code == null) {

View File

@ -24,7 +24,9 @@ import static org.junit.Assert.assertNotNull;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.olingo.commons.api.ex.ODataErrorDetail;
@ -132,4 +134,47 @@ public class ServerErrorSerializerTest {
assertEquals("detailMessage", tree.get("message").textValue());
assertEquals("detailTarget", tree.get("target").textValue());
}
@Test
public void testErrorObjectWithAdditionalProperties() throws Exception {
Map<String, Object> innerError = new HashMap<>();
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("targetDetail", "targetDetail");
map.put("@Common.numericSeverity", 4);
list.add(map);
innerError.put("@Common.additionalTargets", list);
innerError.put("@Common.longtextUrl", "url");
ODataServerError error =
new ODataServerError().setCode("Code").setMessage("Message").setTarget("Target")
.setAdditionalProperties(innerError)
.setDetails(Collections.singletonList(
new ODataErrorDetail().setCode("detailCode").setMessage("detailMessage")
.setTarget("detailTarget").setAdditionalProperties(innerError)));
InputStream stream = ser.error(error).getContent();
JsonNode tree = new ObjectMapper().readTree(stream);
assertNotNull(tree);
tree = tree.get("error");
assertNotNull(tree);
assertEquals("Code", tree.get("code").textValue());
assertEquals("Message", tree.get("message").textValue());
assertEquals("Target", tree.get("target").textValue());
assertEquals(1, tree.get("@Common.additionalTargets").size());
assertEquals("targetDetail", tree.get("@Common.additionalTargets").get(0).get("targetDetail").textValue());
assertEquals(4, tree.get("@Common.additionalTargets").get(0).get("@Common.numericSeverity").asInt());
assertEquals("url", tree.get("@Common.longtextUrl").textValue());
tree = tree.get("details");
assertNotNull(tree);
assertEquals(JsonNodeType.ARRAY, tree.getNodeType());
tree = tree.get(0);
assertNotNull(tree);
assertEquals("detailCode", tree.get("code").textValue());
assertEquals("detailMessage", tree.get("message").textValue());
assertEquals("detailTarget", tree.get("target").textValue());
assertEquals(1, tree.get("@Common.additionalTargets").size());
assertEquals("targetDetail", tree.get("@Common.additionalTargets").get(0).get("targetDetail").textValue());
assertEquals("url", tree.get("@Common.longtextUrl").textValue());
}
}