Skip to content

Commit

Permalink
[GEOS-9204] Complex GeoJSON reports a @datatype for complex types wit…
Browse files Browse the repository at this point in the history
…h simple contents, when the content is missing
  • Loading branch information
aaime committed May 8, 2019
1 parent eca5d44 commit 516f4e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;


import net.sf.json.JSON; import net.sf.json.JSON;
Expand Down Expand Up @@ -79,7 +80,6 @@ public void testGetGeoJsonResponseWfs11() throws Exception {
getAsJSON( getAsJSON(
"wfs?request=GetFeature&version=1.1.0" "wfs?request=GetFeature&version=1.1.0"
+ "&typename=st_gml31:Station_gml31&outputFormat=application/json"); + "&typename=st_gml31:Station_gml31&outputFormat=application/json");
print(response);
// validate the obtained response // validate the obtained response
checkStation1Exists(response); checkStation1Exists(response);
} }
Expand All @@ -93,6 +93,16 @@ public void testGetGeoJsonResponseWfs20() throws Exception {
+ "&typenames=st_gml32:Station_gml32&outputFormat=application/json"); + "&typenames=st_gml32:Station_gml32&outputFormat=application/json");
// validate the obtained response // validate the obtained response
checkStation1Exists(response); checkStation1Exists(response);
// check complex types with simple content that miss their value do not get a datatype
JSONObject station = getFeaturePropertiesById(response, "st.2");
assertThat(station, notNullValue());
JSONObject contact = station.getJSONObject("contact");
assertThat(contact.size(), is(3));
assertThat(contact.get("@mail"), is("st2@stations.org"));
JSONObject phone = contact.getJSONObject("phone");
assertThat(phone.size(), is(1));
assertFalse(phone.has("value"));
assertThat(phone.get("@timeZone"), is(""));
} }


/** Helper method that station 1 exists and was correctly encoded in the GeoJSON response. */ /** Helper method that station 1 exists and was correctly encoded in the GeoJSON response. */
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<sourceExpression> <sourceExpression>
<OCQL>PHONE</OCQL> <OCQL>PHONE</OCQL>
</sourceExpression> </sourceExpression>
<encodeIfEmpty>true</encodeIfEmpty>
<ClientProperty> <ClientProperty>
<name>timeZone</name> <name>timeZone</name>
<value>TIMEZONE</value> <value>TIMEZONE</value>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
Expand All @@ -28,6 +27,7 @@
import org.opengis.feature.ComplexAttribute; import org.opengis.feature.ComplexAttribute;
import org.opengis.feature.Feature; import org.opengis.feature.Feature;
import org.opengis.feature.Property; import org.opengis.feature.Property;
import org.opengis.feature.type.AttributeType;
import org.opengis.feature.type.ComplexType; import org.opengis.feature.type.ComplexType;
import org.opengis.feature.type.GeometryDescriptor; import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.feature.type.Name; import org.opengis.feature.type.Name;
Expand Down Expand Up @@ -402,9 +402,11 @@ private void encodeProperty(
// check if we have a simple content // check if we have a simple content
ComplexAttribute complexAttribute = (ComplexAttribute) property; ComplexAttribute complexAttribute = (ComplexAttribute) property;


Object simpleValue = getSimpleContent(complexAttribute); if (isSimpleContent(complexAttribute.getType())) {
if (simpleValue != null) { Object value = getSimpleContentValue(complexAttribute);
encodeSimpleAttribute(attributeName, simpleValue, attributes); if (value != null || (attributes != null && !attributes.isEmpty())) {
encodeSimpleAttribute(attributeName, value, attributes);
}
} else { } else {
// skip the property/element nesting found in GML, if possible // skip the property/element nesting found in GML, if possible
if (isGMLPropertyType(complexAttribute)) { if (isGMLPropertyType(complexAttribute)) {
Expand Down Expand Up @@ -530,7 +532,24 @@ private List<Feature> getFeatures(Attribute attribute) {
* Helper method that try to extract a simple content from a complex attribute, NULL is returned * Helper method that try to extract a simple content from a complex attribute, NULL is returned
* if no simple content is present. * if no simple content is present.
*/ */
private Object getSimpleContent(ComplexAttribute property) { private boolean isSimpleContent(AttributeType type) {
if ("http://www.w3.org/2001/XMLSchema".equals(type.getName().getNamespaceURI())
&& type.getName().getLocalPart().equals("anySimpleType")) {
return true;
}

AttributeType superType = type.getSuper();
if (superType == null) {
return false;
}
return isSimpleContent(superType);
}

/**
* Helper method that try to extract a simple content from a complex attribute, NULL is returned
* if no simple content is present.
*/
private Object getSimpleContentValue(ComplexAttribute property) {
Collection<Property> properties = property.getProperties(); Collection<Property> properties = property.getProperties();
if (properties.isEmpty() || properties.size() > 1) { if (properties.isEmpty() || properties.size() > 1) {
// no properties or more than property mean no simple content // no properties or more than property mean no simple content
Expand All @@ -546,16 +565,7 @@ private Object getSimpleContent(ComplexAttribute property) {
// not a simple content node // not a simple content node
return null; return null;
} }
Object value = simpleContent.getValue(); return simpleContent.getValue();
if (value instanceof Number
|| value instanceof String
|| value instanceof Character
|| value instanceof Date) {
// the extract value is a simple Java type
return value;
}
// not a valid simple content type
return null;
} }


/** Encode a complex attribute as a JSON object. */ /** Encode a complex attribute as a JSON object. */
Expand Down Expand Up @@ -610,7 +620,9 @@ private void encodeSimpleAttribute(
} }
// we need to encode a list of attributes, let's first encode the main value // we need to encode a list of attributes, let's first encode the main value
jsonWriter.key(name).object(); jsonWriter.key(name).object();
jsonWriter.key("value").value(value); if (value != null) {
jsonWriter.key("value").value(value);
}
// encode the attributes list // encode the attributes list
encodeAttributes(attributes); encodeAttributes(attributes);
// close the values \ attributes object // close the values \ attributes object
Expand Down

0 comments on commit 516f4e5

Please sign in to comment.