OLINGO-825: Adding the support for odata.type such that the extended entity and complex types are correctly de-serilized

This commit is contained in:
Ramesh Reddy 2016-02-08 15:41:32 -06:00
parent 8468308aeb
commit b7005b774f
18 changed files with 320 additions and 63 deletions

View File

@ -391,7 +391,7 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
getFactory().newPrimitiveValueBuilder().buildString("Test")));
newEntity.getProperties().add(
getFactory().newComplexProperty("PropertyCompTwoPrim",
getFactory().newComplexValue("CTTwoPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTTwoPrim")
.add(getFactory().newPrimitiveProperty(
PROPERTY_INT16,
getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)))
@ -537,19 +537,19 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
entity.getProperties().add(
getFactory().newCollectionProperty("CollPropertyComp",
getFactory().newCollectionValue("CTPrimComp")
.add(getFactory().newComplexValue("CTPrimComp")
getFactory().newCollectionValue(SERVICE_NAMESPACE+".CTPrimComp")
.add(getFactory().newComplexValue(SERVICE_NAMESPACE+".CTPrimComp")
.add(getFactory().newPrimitiveProperty(PROPERTY_INT16,
getFactory().newPrimitiveValueBuilder().buildInt16((short) 42)))
.add(getFactory().newComplexProperty(PROPERTY_COMP,
getFactory().newComplexValue("CTAllPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTAllPrim")
.add(getFactory().newPrimitiveProperty(PROPERTY_STRING,
getFactory().newPrimitiveValueBuilder().buildString("42"))))))
.add(getFactory().newComplexValue("CTPrimComp")
.add(getFactory().newComplexValue(SERVICE_NAMESPACE+".CTPrimComp")
.add(getFactory().newPrimitiveProperty(PROPERTY_INT16,
getFactory().newPrimitiveValueBuilder().buildInt16((short) 43)))
.add(getFactory().newComplexProperty(PROPERTY_COMP,
getFactory().newComplexValue("CTAllPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTAllPrim")
.add(getFactory().newPrimitiveProperty(PROPERTY_STRING,
getFactory().newPrimitiveValueBuilder().buildString("43"))))))));
@ -597,22 +597,24 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
getFactory().newPrimitiveProperty(PROPERTY_STRING,
getFactory().newPrimitiveValueBuilder().buildString("Complex collection test")));
entity.getProperties().add(getFactory().newComplexProperty("PropertyCompTwoPrim",
getFactory().newComplexValue("CTTwoPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTTwoPrim")
.add(getFactory().newPrimitiveProperty(PROPERTY_INT16,
getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)))
.add(getFactory().newPrimitiveProperty(PROPERTY_STRING,
getFactory().newPrimitiveValueBuilder().buildString("1")))));
entity.getProperties().add(getFactory().newCollectionProperty("CollPropertyComp",
getFactory().newCollectionValue("CTPrimComp")
.add(getFactory().newComplexValue("CTPrimComp")
getFactory().newCollectionValue(SERVICE_NAMESPACE+".CTPrimComp")
.add(getFactory().newComplexValue(SERVICE_NAMESPACE+".CTPrimComp")
.add(getFactory().newPrimitiveProperty(PROPERTY_INT16,
getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)))
.add(getFactory().newComplexProperty(PROPERTY_COMP, getFactory().newComplexValue("CTAllPrim")
.add(getFactory().newComplexProperty(PROPERTY_COMP,
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTAllPrim")
.add(getFactory().newPrimitiveProperty(PROPERTY_STRING,
getFactory().newPrimitiveValueBuilder().buildString("1"))))))
.add(getFactory().newComplexValue("CTPrimComp")
.add(getFactory().newComplexProperty(PROPERTY_COMP, getFactory().newComplexValue("CTAllPrim")
.add(getFactory().newComplexValue(SERVICE_NAMESPACE+".CTPrimComp")
.add(getFactory().newComplexProperty(PROPERTY_COMP,
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTAllPrim")
.add(getFactory().newPrimitiveProperty(PROPERTY_STRING,
getFactory().newPrimitiveValueBuilder().buildString("2")))
.add(getFactory().newPrimitiveProperty(PROPERTY_INT16,
@ -789,7 +791,7 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
factory.newCollectionValue("Edm.String").add(
factory.newPrimitiveValueBuilder().buildString("Single entry!"))));
entity.getProperties().add(factory.newComplexProperty(PROPERTY_COMP_ALL_PRIM,
factory.newComplexValue("CTAllPrim")
factory.newComplexValue(SERVICE_NAMESPACE+".CTAllPrim")
.add(factory.newPrimitiveProperty(PROPERTY_STRING,
factory.newPrimitiveValueBuilder().buildString("Changed")))));
@ -883,7 +885,7 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
.newPrimitiveValueBuilder()
.buildString("Must not be null")));
entity.getProperties().add(factory.newComplexProperty("PropertyCompTwoPrim", factory.newComplexValue
("CTTwoPrim")
(SERVICE_NAMESPACE+".CTTwoPrim")
.add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder()
.buildString("Must not be null")))
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
@ -892,7 +894,7 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
factory.newCollectionValue("Edm.String")
.add(factory.newPrimitiveValueBuilder().buildString("Single entry!"))));
entity.getProperties().add(factory.newComplexProperty(PROPERTY_COMP_ALL_PRIM,
factory.newComplexValue("CTAllPrim").add(
factory.newComplexValue(SERVICE_NAMESPACE+".CTAllPrim").add(
factory.newPrimitiveProperty(PROPERTY_STRING,
factory.newPrimitiveValueBuilder().buildString("Changed")))));
@ -1165,7 +1167,7 @@ public class BasicITCase extends AbstractParamTecSvcITCase {
final ODataPropertyUpdateRequest requestUpdate = getEdmEnabledClient().getCUDRequestFactory()
.getPropertyComplexValueUpdateRequest(uri, UpdateType.PATCH,
getFactory().newComplexProperty(PROPERTY_COMP_ALL_PRIM,
getFactory().newComplexValue("CTAllPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+".CTAllPrim")
.add(getFactory().newPrimitiveProperty(PROPERTY_INT64,
getFactory().newPrimitiveValueBuilder().buildInt64(Long.MIN_VALUE)))
.add(getFactory().newPrimitiveProperty(PROPERTY_DECIMAL,

View File

@ -358,7 +358,7 @@ public class BatchClientITCase extends AbstractParamTecSvcITCase {
final BatchManager streamManager = request.payloadManager();
final ODataChangeset changeset = streamManager.addChangeset();
final ClientEntity entityESAllPrim = getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
final ClientEntity entityESAllPrim = getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));
entityESAllPrim.getProperties().add(getFactory().newPrimitiveProperty("PropertyDouble",
getFactory().newPrimitiveValueBuilder().buildDouble(3.1415)));
@ -446,7 +446,7 @@ public class BatchClientITCase extends AbstractParamTecSvcITCase {
URI editLink = targetURI.build();
ClientObjectFactory factory = getFactory();
ClientEntity postEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
ClientEntity postEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));
postEntity.addLink(factory.newEntityNavigationLink("NavPropertyETTwoPrimOne", getClient().newURIBuilder
(SERVICE_URI)
.appendEntitySetSegment("ESTwoPrim")
@ -467,7 +467,7 @@ public class BatchClientITCase extends AbstractParamTecSvcITCase {
targetURI = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment("ESAllPrim").appendKeySegment(0);
editLink = targetURI.build();
ClientEntity patchEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
ClientEntity patchEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));
patchEntity.setEditLink(editLink);
patchEntity.getProperties().add(factory.newPrimitiveProperty("PropertyDouble",
@ -483,7 +483,7 @@ public class BatchClientITCase extends AbstractParamTecSvcITCase {
targetURI = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment("ESAllPrim").appendKeySegment(15);
editLink = targetURI.build();
patchEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
patchEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));
patchEntity.setEditLink(editLink);
patchEntity.getProperties().add(factory.newPrimitiveProperty("PropertyDouble",

View File

@ -50,10 +50,10 @@ public class BindingITCase extends AbstractParamTecSvcITCase {
private static final String ES_TWO_KEY_NAV = "ESTwoKeyNav";
private static final String ET_KEY_NAV_NAME = "ETKeyNav";
private static final FullQualifiedName ET_KEY_NAV = new FullQualifiedName(SERVICE_NAMESPACE, ET_KEY_NAV_NAME);
private static final String CT_PRIM_COMP = "CTPrimComp";
private static final String CT_TWO_PRIM = "CTTwoPrim";
private static final String CT_ALL_PRIM = "CTAllPrim";
private static final String CT_NAV_FIVE_PROP = "CTNavFiveProp";
private static final String CT_COMP_NAV = SERVICE_NAMESPACE+"."+"CTCompNav";
private static final String CT_TWO_PRIM = SERVICE_NAMESPACE+"."+"CTTwoPrim";
private static final String CT_ALL_PRIM = SERVICE_NAMESPACE+"."+"CTAllPrim";
private static final String CT_NAV_FIVE_PROP = SERVICE_NAMESPACE+"."+"CTNavFiveProp";
private static final String PROPERTY_INT16 = "PropertyInt16";
private static final String PROPERTY_STRING = "PropertyString";
private static final String PROPERTY_COMP_NAV = "PropertyCompNav";
@ -92,7 +92,7 @@ public class BindingITCase extends AbstractParamTecSvcITCase {
42)))
.add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("42")))));
entity.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV, factory.newComplexValue(CT_PRIM_COMP)
.add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV, factory.newComplexValue(CT_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder()
.buildString("42")))
.add(factory.newComplexProperty(PROPERTY_COMP_NAV, factory.newComplexValue(CT_NAV_FIVE_PROP)

View File

@ -64,11 +64,12 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
private static final String ET_TWO_KEY_NAV_NAME = "ETTwoKeyNav";
private static final FullQualifiedName ET_KEY_NAV = new FullQualifiedName(SERVICE_NAMESPACE, ET_KEY_NAV_NAME);
private static final FullQualifiedName ET_TWO_KEY_NAV = new FullQualifiedName(SERVICE_NAMESPACE, ET_TWO_KEY_NAV_NAME);
private static final String CT_PRIM_COMP = "CTPrimComp";
private static final String CT_TWO_PRIM = "CTTwoPrim";
private static final String CT_ALL_PRIM = "CTAllPrim";
private static final String CT_NAV_FIVE_PROP = "CTNavFiveProp";
private static final String CT_BASE_PRIM_COMP_NAV = "CTBasePrimCompNav";
private static final String CT_PRIM_COMP = SERVICE_NAMESPACE+"."+"CTPrimComp";
private static final String CT_COMP_NAV = SERVICE_NAMESPACE+"."+"CTCompNav";
private static final String CT_TWO_PRIM = SERVICE_NAMESPACE+"."+"CTTwoPrim";
private static final String CT_ALL_PRIM = SERVICE_NAMESPACE+"."+"CTAllPrim";
private static final String CT_NAV_FIVE_PROP = SERVICE_NAMESPACE+"."+"CTNavFiveProp";
private static final String CT_BASE_PRIM_COMP_NAV = SERVICE_NAMESPACE+"."+"CTBasePrimCompNav";
private static final String PROPERTY_INT16 = "PropertyInt16";
private static final String PROPERTY_STRING = "PropertyString";
private static final String PROPERTY_COMP = "PropertyComp";
@ -308,7 +309,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildString("42")))));
entity.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_STRING,
factory.newPrimitiveValueBuilder().buildString("42")))
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
@ -330,7 +331,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildInt16((short) 430)))));
inlineEntitySingle.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_BASE_PRIM_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
factory.newPrimitiveValueBuilder().buildInt16((short) 431)))));
inlineEntitySingle.getProperties()
@ -351,7 +352,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
.add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("44")));
inlineEntityCol1.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_BASE_PRIM_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
factory.newPrimitiveValueBuilder().buildInt16((short) 441)))));
inlineEntityCol1.getProperties()
@ -374,7 +375,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
.add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("45")));
inlineEntityCol2.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_BASE_PRIM_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
factory.newPrimitiveValueBuilder().buildInt16((short) 451)))));
inlineEntityCol2.getProperties()
@ -538,7 +539,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildString("42")))));
entity.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_STRING,
factory.newPrimitiveValueBuilder().buildString("42")))
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
@ -580,7 +581,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildString("431")))));
innerEntity.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_STRING,
factory.newPrimitiveValueBuilder().buildString("431")))
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
@ -804,7 +805,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildString("42")))));
entity.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_STRING,
factory.newPrimitiveValueBuilder().buildString("42")))
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
@ -825,7 +826,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildInt16((short) 43)))));
inlineEntitySingle.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_BASE_PRIM_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
factory.newPrimitiveValueBuilder().buildInt16((short) 431)))));
inlineEntitySingle.getProperties()
@ -846,7 +847,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
.add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("44")));
inlineEntityCol1.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_BASE_PRIM_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
factory.newPrimitiveValueBuilder().buildInt16((short) 441)))));
inlineEntityCol1.getProperties()
@ -871,7 +872,7 @@ public class DeepInsertITCase extends AbstractParamTecSvcITCase {
factory.newPrimitiveValueBuilder().buildString("45")));
inlineEntityCol2.getProperties()
.add(factory.newComplexProperty(PROPERTY_COMP_NAV,
factory.newComplexValue(CT_PRIM_COMP)
factory.newComplexValue(CT_BASE_PRIM_COMP_NAV)
.add(factory.newPrimitiveProperty(PROPERTY_INT16,
factory.newPrimitiveValueBuilder().buildInt16((short) 451)))));
inlineEntityCol2.getProperties()

View File

@ -911,14 +911,14 @@ public class FilterSystemQueryITCase extends AbstractParamTecSvcITCase {
getFactory().newPrimitiveValueBuilder().buildString("Test")));
newEntity.getProperties().add(
getFactory().newComplexProperty("PropertyCompAllPrim",
getFactory().newComplexValue("CTAllPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+"."+"CTAllPrim")
.add(getFactory().newPrimitiveProperty(
"PropertyString",
getFactory().newPrimitiveValueBuilder().buildString("Test 3")))));
newEntity.getProperties().add(
getFactory().newComplexProperty("PropertyCompTwoPrim",
getFactory().newComplexValue("CTTwoPrim")
getFactory().newComplexValue(SERVICE_NAMESPACE+"."+"CTTwoPrim")
.add(getFactory().newPrimitiveProperty(
"PropertyInt16",
getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)))

View File

@ -131,6 +131,16 @@ public abstract class OData {
*/
public abstract ODataDeserializer createDeserializer(ContentType contentType) throws DeserializerException;
/**
* Creates a new deserializer object for reading content in the specified format.
* Deserializers are used in Processor implementations.
*
* @param contentType any content type supported by Olingo (XML, JSON ...)
* @param metadata ServiceMetada of the service
*/
public abstract ODataDeserializer createDeserializer(ContentType contentType,
ServiceMetadata metadata) throws DeserializerException;
/**
* Creates a primitive-type instance.
* @param kind the kind of the primitive type

View File

@ -145,7 +145,7 @@ public class ActionRequest extends OperationRequest {
}
public List<Parameter> getParameters() throws DeserializerException {
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType());
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType(), this.serviceMetadata);
return new ArrayList<Parameter>(deserializer.actionParameters(getPayload(), getAction()).getActionParameters()
.values());
}

View File

@ -333,7 +333,7 @@ public class DataRequest extends ServiceRequest {
}
private Entity getEntityFromClient() throws DeserializerException {
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType());
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType(), getServiceMetaData());
return deserializer.entity(getODataRequest().getBody(), getEntitySet().getEntityType()).getEntity();
}
@ -460,7 +460,7 @@ public class DataRequest extends ServiceRequest {
// /odata-json-format-v4.0-errata02-os-complete.html#_Toc403940643
// The below code reads as property and converts to an URI
private List<URI> getPayload() throws DeserializerException {
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType());
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType(), getServiceMetaData());
return deserializer.entityReferences(getODataRequest().getBody()).getEntityReferences();
}
@ -701,7 +701,7 @@ public class DataRequest extends ServiceRequest {
private org.apache.olingo.commons.api.data.Property getPropertyValueFromClient(
EdmProperty edmProperty) throws DeserializerException {
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType());
ODataDeserializer deserializer = odata.createDeserializer(getRequestContentType(), getServiceMetaData());
return deserializer.property(getODataRequest().getBody(), edmProperty).getProperty();
}

View File

@ -116,7 +116,7 @@ public class TripPinDataModel {
private EntityCollection loadEnities(String entitySetName, EdmEntityType type) {
try {
ODataJsonDeserializer deserializer = new ODataJsonDeserializer(ContentType.JSON);
ODataJsonDeserializer deserializer = new ODataJsonDeserializer(ContentType.JSON, this.metadata);
EntityCollection set = deserializer.entityCollection(new FileInputStream(new File(
"src/test/resources/" + entitySetName.toLowerCase() + ".json")), type).getEntityCollection();

View File

@ -121,6 +121,19 @@ public class ODataImpl extends OData {
throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
}
}
@Override
public ODataDeserializer createDeserializer(final ContentType contentType,
ServiceMetadata metadata) throws DeserializerException {
if (contentType.isCompatible(ContentType.JSON)) {
return new ODataJsonDeserializer(contentType, metadata);
} else if (contentType.isCompatible(ContentType.APPLICATION_XML)
|| contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
return new ODataXmlDeserializer(metadata);
} else {
throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
}
}
@Override

View File

@ -48,10 +48,13 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.commons.api.edm.EdmStructuredType;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.EdmTypeDefinition;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.constants.EdmTypeKind;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.deserializer.DeserializerException.MessageKeys;
import org.apache.olingo.server.api.deserializer.DeserializerResult;
@ -76,10 +79,20 @@ public class ODataJsonDeserializer implements ODataDeserializer {
private static final String ODATA_CONTROL_INFORMATION_PREFIX = "@odata.";
private final boolean isIEEE754Compatible;
private ServiceMetadata serviceMetadata;
public ODataJsonDeserializer(final ContentType contentType) {
isIEEE754Compatible = isODataIEEE754Compatible(contentType);
}
public ODataJsonDeserializer(final ContentType contentType, final ServiceMetadata serviceMetadata) {
isIEEE754Compatible = isODataIEEE754Compatible(contentType);
this.serviceMetadata = serviceMetadata;
}
public void setMetadata(ServiceMetadata metadata) {
this.serviceMetadata = metadata;
}
@Override
public DeserializerResult entityCollection(final InputStream stream, final EdmEntityType edmEntityType)
@ -124,7 +137,8 @@ public class ODataJsonDeserializer implements ODataDeserializer {
throw new DeserializerException("Nested Arrays and primitive values are not allowed for an entity value.",
DeserializerException.MessageKeys.INVALID_ENTITY);
}
entities.add(consumeEntityNode(edmEntityType, (ObjectNode) arrayElement, expandBuilder));
EdmEntityType derivedEdmEntityType = (EdmEntityType)getDerivedType(edmEntityType, arrayElement);
entities.add(consumeEntityNode(derivedEdmEntityType, (ObjectNode) arrayElement, expandBuilder));
}
return entities;
} else {
@ -140,7 +154,9 @@ public class ODataJsonDeserializer implements ODataDeserializer {
final ObjectNode tree = parseJsonTree(stream);
final ExpandTreeBuilderImpl expandBuilder = new ExpandTreeBuilderImpl();
return DeserializerResultImpl.with().entity(consumeEntityNode(edmEntityType, tree, expandBuilder))
EdmEntityType derivedEdmEntityType = (EdmEntityType)getDerivedType(edmEntityType, tree);
return DeserializerResultImpl.with().entity(consumeEntityNode(derivedEdmEntityType, tree, expandBuilder))
.expandOption(expandBuilder.build())
.build();
} catch (final IOException e) {
@ -257,6 +273,7 @@ public class ODataJsonDeserializer implements ODataDeserializer {
edmParameter.isNullable(), edmParameter.getMaxLength(),
edmParameter.getPrecision(), edmParameter.getScale(), true, edmParameter.getMapping(), node);
parameter.setValue(property.getValueType(), property.getValue());
parameter.setType(property.getType());
}
return parameter;
}
@ -472,7 +489,12 @@ public class ODataJsonDeserializer implements ODataDeserializer {
value);
break;
case COMPLEX:
value = readComplexNode(name, type, isNullable, jsonNode);
EdmType derivedType = getDerivedType((EdmComplexType) type,
jsonNode);
property.setType(derivedType.getFullQualifiedName()
.getFullQualifiedNameAsString());
value = readComplexNode(name, derivedType, isNullable, jsonNode);
property.setValue(ValueType.COMPLEX, value);
break;
default:
@ -808,4 +830,53 @@ public class ODataJsonDeserializer implements ODataDeserializer {
&& Boolean.TRUE.toString().equalsIgnoreCase(
contentType.getParameter(ContentType.PARAMETER_IEEE754_COMPATIBLE));
}
private EdmType getDerivedType(final EdmStructuredType edmType, final JsonNode jsonNode)
throws DeserializerException {
JsonNode odataTypeNode = jsonNode.get(Constants.JSON_TYPE);
if (odataTypeNode != null) {
String odataType = odataTypeNode.asText();
if (!odataType.isEmpty()) {
odataType = odataType.substring(1);
if (odataType.equalsIgnoreCase(edmType.getFullQualifiedName().getFullQualifiedNameAsString())) {
return edmType;
} else if (this.serviceMetadata == null) {
throw new DeserializerException(
"Failed to resolve Odata type " + odataType + " due to metadata is not available",
DeserializerException.MessageKeys.UNKNOWN_CONTENT);
}
EdmStructuredType currentEdmType = null;
if(edmType instanceof EdmEntityType) {
currentEdmType = serviceMetadata.getEdm()
.getEntityType(new FullQualifiedName(odataType));
} else {
currentEdmType = serviceMetadata.getEdm()
.getComplexType(new FullQualifiedName(odataType));
}
if (!isAssignable(edmType, currentEdmType)) {
throw new DeserializerException(
"Odata type " + odataType + " not allowed here",
DeserializerException.MessageKeys.UNKNOWN_CONTENT);
}
return currentEdmType;
}
}
return edmType;
}
private boolean isAssignable(final EdmStructuredType edmStructuredType,
final EdmStructuredType edmStructuredTypeToAssign) {
if (edmStructuredTypeToAssign == null) {
return false;
} else if (edmStructuredType.getFullQualifiedName()
.equals(edmStructuredTypeToAssign.getFullQualifiedName())) {
return true;
} else {
return isAssignable(edmStructuredType,
edmStructuredTypeToAssign.getBaseType());
}
}
}

View File

@ -52,10 +52,13 @@ import org.apache.olingo.commons.api.edm.EdmParameter;
import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.commons.api.edm.EdmStructuredType;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
import org.apache.olingo.commons.core.edm.primitivetype.AbstractGeospatialType;
import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.deserializer.DeserializerException.MessageKeys;
import org.apache.olingo.server.api.deserializer.DeserializerResult;
@ -75,7 +78,21 @@ public class ODataXmlDeserializer implements ODataDeserializer {
private static final QName etagQName = new QName(Constants.NS_METADATA, Constants.ATOM_ATTR_ETAG);
private static final QName countQName = new QName(Constants.NS_METADATA, Constants.ATOM_ELEM_COUNT);
private static final QName parametersQName = new QName(Constants.NS_METADATA, "parameters");
private static final QName typeQName = new QName(Constants.NS_METADATA, Constants.ATTR_TYPE);
private ServiceMetadata serviceMetadata;
public ODataXmlDeserializer() {
}
public ODataXmlDeserializer(final ServiceMetadata serviceMetadata) {
this.serviceMetadata = serviceMetadata;
}
public void setMetadata(ServiceMetadata metadata) {
this.serviceMetadata = metadata;
}
protected XMLEventReader getReader(final InputStream input) throws XMLStreamException {
return FACTORY.createXMLEventReader(input);
}
@ -178,7 +195,17 @@ public class ODataXmlDeserializer implements ODataDeserializer {
} else {
property.setName(start.getName().getLocalPart());
}
valuable(property, reader, start, edmType, isNullable, maxLength, precision, scale, isUnicode, isCollection);
EdmType resolvedType = edmType;
final Attribute attrType = start.getAttributeByName(typeQName);
if (attrType != null && (edmType instanceof EdmComplexType)) {
String type = new EdmTypeInfo.Builder().setTypeExpression(attrType.getValue()).build().internal();
if (type.startsWith("Collection(") && type.endsWith(")")) {
type = type.substring(11, type.length()-1);
}
resolvedType = getDerivedType((EdmComplexType)edmType, type);
}
valuable(property, reader, start, resolvedType, isNullable, maxLength, precision, scale, isUnicode, isCollection);
return property;
}
@ -431,6 +458,7 @@ public class ODataXmlDeserializer implements ODataDeserializer {
private Entity entity(final XMLEventReader reader, final StartElement start, final EdmEntityType edmEntityType)
throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException {
Entity entity = null;
EdmEntityType resolvedType = edmEntityType;
if (entryRefQName.equals(start.getName())) {
entity = entityRef(start);
} else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
@ -455,7 +483,9 @@ public class ODataXmlDeserializer implements ODataDeserializer {
} else if (Constants.QNAME_ATOM_ELEM_CATEGORY.equals(event.asStartElement().getName())) {
final Attribute term = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_TERM));
if (term != null) {
entity.setType(new EdmTypeInfo.Builder().setTypeExpression(term.getValue()).build().internal());
String type = new EdmTypeInfo.Builder().setTypeExpression(term.getValue()).build().internal();
entity.setType(type);
resolvedType = (EdmEntityType)getDerivedType(edmEntityType, type);
}
} else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
final Link link = new Link();
@ -485,7 +515,7 @@ public class ODataXmlDeserializer implements ODataDeserializer {
entity.setMediaETag(mediaETag.getValue());
}
} else if (link.getRel().startsWith(Constants.NS_NAVIGATION_LINK_REL)) {
inline(reader, event.asStartElement(), link, edmEntityType);
inline(reader, event.asStartElement(), link, resolvedType);
if (link.getInlineEntity() == null && link.getInlineEntitySet() == null) {
entity.getNavigationBindings().add(link);
} else {
@ -525,7 +555,7 @@ public class ODataXmlDeserializer implements ODataDeserializer {
.getAttributeByName(QName.valueOf(Constants.ATTR_TYPE));
if (contenttype == null || ContentType.APPLICATION_XML.toContentTypeString()
.equals(contenttype.getValue())) {
properties(reader, skipBeforeFirstStartElement(reader), entity, edmEntityType);
properties(reader, skipBeforeFirstStartElement(reader), entity, resolvedType);
} else {
entity.setMediaContentType(contenttype.getValue());
final Attribute src = event.asStartElement().getAttributeByName(QName.valueOf(Constants.ATOM_ATTR_SRC));
@ -534,7 +564,7 @@ public class ODataXmlDeserializer implements ODataDeserializer {
}
}
} else if (propertiesQName.equals(event.asStartElement().getName())) {
properties(reader, event.asStartElement(), entity, edmEntityType);
properties(reader, event.asStartElement(), entity, resolvedType);
}
}
@ -799,4 +829,48 @@ public class ODataXmlDeserializer implements ODataDeserializer {
}
return parameter;
}
private EdmType getDerivedType(final EdmStructuredType edmType, String odataType)
throws DeserializerException {
if (odataType != null && !odataType.isEmpty()) {
if (odataType.equalsIgnoreCase(edmType.getFullQualifiedName().getFullQualifiedNameAsString())) {
return edmType;
} else if (this.serviceMetadata == null) {
throw new DeserializerException(
"Failed to resolve Odata type " + odataType + " due to metadata is not available",
DeserializerException.MessageKeys.UNKNOWN_CONTENT);
}
EdmStructuredType currentEdmType = null;
if(edmType instanceof EdmEntityType) {
currentEdmType = serviceMetadata.getEdm()
.getEntityType(new FullQualifiedName(odataType));
} else {
currentEdmType = serviceMetadata.getEdm()
.getComplexType(new FullQualifiedName(odataType));
}
if (!isAssignable(edmType, currentEdmType)) {
throw new DeserializerException(
"Odata type " + odataType + " not allowed here",
DeserializerException.MessageKeys.UNKNOWN_CONTENT);
}
return currentEdmType;
}
return edmType;
}
private boolean isAssignable(final EdmStructuredType edmStructuredType,
final EdmStructuredType edmStructuredTypeToAssign) {
if (edmStructuredTypeToAssign == null) {
return false;
} else if (edmStructuredType.getFullQualifiedName()
.equals(edmStructuredTypeToAssign.getFullQualifiedName())) {
return true;
} else {
return isAssignable(edmStructuredType,
edmStructuredTypeToAssign.getBaseType());
}
}
}

View File

@ -24,6 +24,7 @@ import java.util.Collections;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.edmx.EdmxReference;
import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
@ -33,7 +34,9 @@ public class AbstractODataDeserializerTest {
protected static final Edm edm = OData.newInstance()
.createServiceMetadata(new EdmTechProvider(), Collections.<EdmxReference> emptyList())
.getEdm();
protected static final ServiceMetadata metadata = OData.newInstance()
.createServiceMetadata(new EdmTechProvider(), Collections.<EdmxReference> emptyList());
protected InputStream getFileAsStream(final String filename) throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
if (in == null) {

View File

@ -165,14 +165,14 @@ public class ODataDeserializerEntityCollectionTest extends AbstractODataDeserial
private EntityCollection deserialize(final InputStream stream, final String entityTypeName)
throws DeserializerException {
return OData.newInstance().createDeserializer(ContentType.JSON)
return OData.newInstance().createDeserializer(ContentType.JSON, metadata)
.entityCollection(stream, edm.getEntityType(new FullQualifiedName(NAMESPACE, entityTypeName)))
.getEntityCollection();
}
private EntityCollection deserialize(final String input, final String entityTypeName)
throws DeserializerException {
return OData.newInstance().createDeserializer(ContentType.JSON)
return OData.newInstance().createDeserializer(ContentType.JSON, metadata)
.entityCollection(new ByteArrayInputStream(input.getBytes()),
edm.getEntityType(new FullQualifiedName(NAMESPACE, entityTypeName)))
.getEntityCollection();

View File

@ -254,7 +254,7 @@ public class ODataJsonDeserializerActionParametersTest extends AbstractODataDese
private Map<String, Parameter> deserialize(final String input, final String actionName, final String bindingTypeName)
throws DeserializerException {
return OData.newInstance().createDeserializer(ContentType.JSON)
return OData.newInstance().createDeserializer(ContentType.JSON, metadata)
.actionParameters(new ByteArrayInputStream(input.getBytes()),
bindingTypeName == null ?
edm.getUnboundAction(new FullQualifiedName(NAMESPACE, actionName)) :

View File

@ -46,10 +46,12 @@ import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.CsdlMapping;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.core.edm.primitivetype.EdmDate;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.deserializer.ODataDeserializer;
import org.apache.olingo.server.core.deserializer.AbstractODataDeserializerTest;
import org.junit.Assert;
import org.junit.Test;
public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTest {
@ -217,6 +219,46 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
assertEquals(16, complexProperties.size());
}
@Test
public void extendedComplexProperty() throws Exception {
final String payload = "{"
+ "\"@odata.context\":\"$metadata#ESCompComp/$entity\","
+ "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
+ "\"@odata.etag\":\"W/\\\"32767\\\"\","
+ "\"PropertyInt16\":32767,"
+ "\"PropertyComp\":{"
+ "\"@odata.type\":\"#olingo.odata.test1.CTCompCompExtended\","
+ "\"PropertyComp\":{"
+ "\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+ "\"PropertyInt16\":32767,"
+ "\"PropertyString\":\"First Resource - first\""
+ "},"
+ "\"PropertyDate\":\"2012-10-03\""
+ "}}";
final Entity result = deserialize(payload, "ETCompComp");
Assert.assertNotNull(result);
Property property = result.getProperty("PropertyComp");
Assert.assertEquals("PropertyComp", property.getName());
Assert.assertTrue(property.isComplex());
final ComplexValue cv = property.asComplex();
Assert.assertEquals("olingo.odata.test1.CTCompCompExtended", property.getType());
Assert.assertEquals(
"2012-10-03",
EdmDate.getInstance().valueToString(getCVProperty(cv, "PropertyDate").asPrimitive(), false, 10, 3, 0,
false));
}
private Property getCVProperty(ComplexValue cv, String name) {
for (Property p : cv.getValue()) {
if (p.getName().equals(name)) {
return p;
}
}
return null;
}
@Test
public void simpleEntityETCollAllPrim() throws Exception {
final String entityString = "{"
@ -606,7 +648,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
"{\"PropertyDate\":\"2012-12-03\","
+ "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ContentType.JSON);
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ContentType.JSON, metadata);
Entity entity = deserializer.entity(stream, entityType).getEntity();
assertNotNull(entity);
List<Property> properties = entity.getProperties();
@ -1272,7 +1314,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
protected static Entity deserialize(final InputStream stream, final String entityTypeName,
final ContentType contentType) throws DeserializerException {
return OData.newInstance().createDeserializer(contentType)
return OData.newInstance().createDeserializer(contentType, metadata)
.entity(stream, edm.getEntityType(new FullQualifiedName(NAMESPACE, entityTypeName)))
.getEntity();
}

View File

@ -234,7 +234,7 @@ public class ODataXMLDeserializerActionParametersTest extends AbstractODataDeser
private Map<String, Parameter> deserialize(final String input, final String actionName, final String bindingTypeName)
throws DeserializerException {
return OData.newInstance().createDeserializer(ContentType.APPLICATION_XML)
return OData.newInstance().createDeserializer(ContentType.APPLICATION_XML, metadata)
.actionParameters(new ByteArrayInputStream(input.getBytes()),
bindingTypeName == null ?
edm.getUnboundAction(new FullQualifiedName(NAMESPACE, actionName)) :

View File

@ -36,6 +36,7 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmProperty;
import org.apache.olingo.commons.core.edm.primitivetype.EdmDate;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.deserializer.ODataDeserializer;
import org.apache.olingo.server.core.deserializer.AbstractODataDeserializerTest;
@ -47,7 +48,7 @@ import org.junit.Test;
public class ODataXmlDeserializerTest extends AbstractODataDeserializerTest {
private static final EdmEntityContainer entityContainer = edm.getEntityContainer();
private final ODataDeserializer deserializer = new ODataXmlDeserializer();
private final ODataDeserializer deserializer = new ODataXmlDeserializer(metadata);
@BeforeClass
public static void setup() {
@ -534,6 +535,46 @@ public class ODataXmlDeserializerTest extends AbstractODataDeserializerTest {
Assert.assertTrue(getCVProperty(cv, "PropertyString").isNull());
}
@Test
public void extendedComplexProperty() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompComp");
String payload = "<?xml version='1.0' encoding='UTF-8'?>"
+ "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" "
+ "xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\" "
+ "xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" "
+ "metadata:etag=\"W/&quot;32767&quot;\">"
+ "<atom:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\" "
+ "term=\"#olingo.odata.test1.ETCompComp\"/>"
+ "<atom:content type=\"application/xml\">"
+ "<metadata:properties>"
+ "<data:PropertyInt16>32767</data:PropertyInt16>"
+ "<data:PropertyComp metadata:type=\"#olingo.odata.test1.CTCompCompExtended\">"
+ "<data:PropertyComp metadata:type=\"#olingo.odata.test1.CTTwoPrim\">"
+ "<data:PropertyInt16>32767</data:PropertyInt16>"
+ "<data:PropertyString>First Resource - first</data:PropertyString>"
+ "</data:PropertyComp>"
+ "<data:PropertyDate>2012-10-03</data:PropertyDate>"
+ "</data:PropertyComp>"
+ "</metadata:properties>"
+ "</atom:content>"
+ "</atom:entry>";
Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()),
edmEntitySet.getEntityType()).getEntity();
Assert.assertNotNull(result);
Property property = result.getProperty("PropertyComp");
Assert.assertEquals("PropertyComp", property.getName());
Assert.assertTrue(property.isComplex());
final ComplexValue cv = property.asComplex();
Assert.assertEquals("olingo.odata.test1.CTCompCompExtended", property.getType());
Assert.assertEquals(
"2012-10-03",
EdmDate.getInstance().valueToString(getCVProperty(cv, "PropertyDate").asPrimitive(), false, 10, 3, 0,
false));
}
@Test
public void complexCollectionProperty() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");