Skip to content

Commit

Permalink
Add ERXRest.includeNullValues property to show/hide null values on Re…
Browse files Browse the repository at this point in the history
…st responses

Using the boolean ERXRest.includeNullValues it`s either possible to return null values or simply not showing them, use Properties file to set it. Supported formats are json, xml and plist formats.
  • Loading branch information
luizalfredo23 committed Apr 18, 2016
1 parent 283fcfe commit f62139c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
15 changes: 9 additions & 6 deletions Frameworks/EOF/ERRest/Sources/er/rest/ERXRestRequestNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import er.extensions.eof.ERXKey;
import er.extensions.eof.ERXKeyFilter;
import er.extensions.foundation.ERXArrayUtilities;
import er.extensions.foundation.ERXProperties;
import er.rest.format.ERXRestFormat;
import er.rest.format.ERXWORestResponse;
import er.rest.format.IERXRestWriter;
Expand All @@ -37,6 +38,8 @@
* etc), we needed a document model that is more abstract than just an org.w3c.dom. Or, rather, one that isn't obnoxious
* to use.
*
* @property <code>ERXRest.includeNullValues</code> Boolean property to enable null values in return. Defaults
* to true.
* @author mschrag
*/
public class ERXRestRequestNode implements NSKeyValueCoding, NSKeyValueCodingAdditions {
Expand Down Expand Up @@ -203,9 +206,9 @@ else if (_value != null) {
// MS: name has to be after toJavaCollection, because the naming delegate could rename it ... little
// sketchy, i know
String name = child.name();
// if (value != null) {
dict.put(name, value);
// }
if (value != null || ERXProperties.booleanForKeyWithDefault("ERXRest.includeNullValues", true)) {
dict.put(name, value);
}
}
if (dict.isEmpty()) {
result = null;
Expand Down Expand Up @@ -276,9 +279,9 @@ else if (_value != null) {
for (ERXRestRequestNode child : _children) {
String name = child.name();
Object value = child.toNSCollection(delegate, associatedObjects);
// if (value != null) {
dict.put(name, value);
// }
if (value != NSKeyValueCoding.NullValue || ERXProperties.booleanForKeyWithDefault("ERXRest.includeNullValues", true) == true) {
dict.put(name, value);
}
}
if (dict.isEmpty()) {
result = NSKeyValueCoding.NullValue;
Expand Down
22 changes: 13 additions & 9 deletions Frameworks/EOF/ERRest/Sources/er/rest/format/ERXXmlRestWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/**
*
* @property ERXRest.suppressTypeAttributesForSimpleTypes (default "false") If set to true, primitive types, like type = "datetime", won't be added to the output
* @property <code>er.rest.ERXRest.includeNullValues</code> Boolean property to enable null values in return. Defaults
* to true.
*/
public class ERXXmlRestWriter extends ERXRestWriter {
public void appendToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestFormat.Delegate delegate, ERXRestContext context) {
Expand Down Expand Up @@ -47,16 +49,18 @@ protected void appendValueToResponse(ERXRestRequestNode node, IERXRestResponse r
String name = node.name();
Object value = node.value();
String formattedValue = coerceValueToString(value, context);

if (formattedValue == null) {
indent(response, indent);
response.appendContentString("<");
response.appendContentString(name);
appendAttributesToResponse(node, response, context);
appendTypeToResponse(value, response);
response.appendContentString("/>");
response.appendContentString("\n");
}
else {
if(ERXProperties.booleanForKeyWithDefault("ERXRest.includeNullValues", true)) {
indent(response, indent);
response.appendContentString("<");
response.appendContentString(name);
appendAttributesToResponse(node, response, context);
appendTypeToResponse(value, response);
response.appendContentString("/>");
response.appendContentString("\n");
}
} else {
indent(response, indent);
response.appendContentString("<");
response.appendContentString(name);
Expand Down

0 comments on commit f62139c

Please sign in to comment.