Uniforming the type expression (de)serialization

This commit is contained in:
Francesco Chicchiriccò 2014-04-09 17:03:50 +02:00
parent 0612eab441
commit 9f96234e69
10 changed files with 77 additions and 61 deletions

View File

@ -50,7 +50,6 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.format.ODataPubFormat;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
@ -101,8 +100,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
ODataOperation boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetEmployeesCount");
assertNotNull(boundOp);
EdmFunction func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
EdmFunction func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(),
false, null);
assertNotNull(func);
@ -124,9 +122,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetProductDetails");
assertNotNull(boundOp);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false, null);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false, null);
assertNotNull(func);
final ODataPrimitiveValue count = getClient().getObjectFactory().newPrimitiveValueBuilder().buildInt32(1);
@ -153,9 +149,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetRelatedProduct");
assertNotNull(boundOp);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false, null);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false, null);
assertNotNull(func);
final ODataInvokeRequest<ODataEntity> getRelatedProductReq =
@ -165,7 +159,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
try {
final ODataEntity getRelatedProductRes = getRelatedProductReq.execute().getBody();
assertNotNull(getRelatedProductRes);
assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.Product",
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Product",
getRelatedProductRes.getTypeName().toString());
assertEquals(6, getRelatedProductRes.getProperty("ProductID").getPrimitiveValue().toCastValue(Integer.class), 0);
} catch (Exception e) {
@ -183,9 +177,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetDefaultPI");
assertNotNull(boundOp);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false, null);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false, null);
assertNotNull(func);
final ODataInvokeRequest<ODataEntity> getDefaultPIReq =
@ -193,7 +185,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
getDefaultPIReq.setFormat(format);
final ODataEntity getDefaultPIRes = getDefaultPIReq.execute().getBody();
assertNotNull(getDefaultPIRes);
assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.PaymentInstrument",
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.PaymentInstrument",
getDefaultPIRes.getTypeName().toString());
assertEquals(101901,
getDefaultPIRes.getProperty("PaymentInstrumentID").getPrimitiveValue().toCastValue(Integer.class), 0);
@ -202,9 +194,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetAccountInfo");
assertNotNull(boundOp);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false, null);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false, null);
assertNotNull(func);
final ODataInvokeRequest<ODataProperty> getAccountInfoReq =
@ -228,9 +218,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetActualAmount");
assertNotNull(boundOp);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false, null);
func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false, null);
assertNotNull(func);
final ODataPrimitiveValue bonusRate = getClient().getObjectFactory().newPrimitiveValueBuilder().buildDouble(1.1);
@ -270,9 +258,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
ODataOperation boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.IncreaseRevenue");
assertNotNull(boundOp);
EdmAction act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false);
EdmAction act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false);
assertNotNull(act);
final ODataPrimitiveValue increaseValue = getClient().getObjectFactory().newPrimitiveValueBuilder().
@ -296,9 +282,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.AddAccessRight");
assertNotNull(boundOp);
act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false);
act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false);
assertNotNull(act);
final ODataEnumValue accessRight = getClient().getObjectFactory().
@ -323,8 +307,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
assertNotNull(boundOp);
act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()),
edm.getEntityType(new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().
getFullQualifiedName()).getBaseType().getFullQualifiedName(), false);
edm.getEntityType(entity.getTypeName()).getBaseType().getFullQualifiedName(), false);
assertNotNull(act);
final ODataCollectionValue<org.apache.olingo.commons.api.domain.v4.ODataValue> addresses =
@ -361,9 +344,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.RefreshDefaultPI");
assertNotNull(boundOp);
act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()),
new EdmTypeInfo.Builder().setTypeExpression(entity.getTypeName().toString()).build().getFullQualifiedName(),
false);
act = edm.getBoundAction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(), false);
assertNotNull(act);
final ODataPrimitiveValue newDate = getClient().getObjectFactory().newPrimitiveValueBuilder().
@ -374,7 +355,7 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
getDefaultPIReq.setFormat(format);
final ODataEntity getDefaultPIRes = getDefaultPIReq.execute().getBody();
assertNotNull(getDefaultPIRes);
assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.PaymentInstrument",
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.PaymentInstrument",
getDefaultPIRes.getTypeName().toString());
assertEquals(101901,
getDefaultPIRes.getProperty("PaymentInstrumentID").getPrimitiveValue().toCastValue(Integer.class), 0);

View File

@ -67,7 +67,7 @@ public class EntityRetrieveTestITCase extends AbstractTestITCase {
final CommonODataEntity entity = res.getBody();
assertNotNull(entity);
assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.Customer", entity.getTypeName().toString());
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Customer", entity.getTypeName().toString());
assertEquals(getServiceRoot() + "/Customers(PersonID=1)", entity.getEditLink().toASCIIString());
assertEquals(3, entity.getNavigationLinks().size());

View File

@ -113,7 +113,7 @@ public class OperationImportInvokeTestITCase extends AbstractTestITCase {
person2Req.setFormat(format);
final ODataEntity person2 = person2Req.execute().getBody();
assertNotNull(person2);
assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.Customer", person2.getTypeName().toString());
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Customer", person2.getTypeName().toString());
assertEquals(1, person2.getProperty("PersonID").getPrimitiveValue().toCastValue(Integer.class), 0);
// GetPerson

View File

@ -53,7 +53,7 @@ public class EntityTest extends AbstractTest {
getClient().getDeserializer().toEntry(input, format).getObject());
assertNotNull(entity);
assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.Customer", entity.getTypeName().toString());
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Customer", entity.getTypeName().toString());
final ODataProperty birthday = entity.getProperty("Birthday");
assertTrue(birthday.hasPrimitiveValue());

View File

@ -36,7 +36,6 @@ import org.apache.olingo.commons.api.data.Value;
import org.apache.olingo.commons.api.domain.ODataOperation;
import org.apache.olingo.commons.api.domain.ODataPropertyType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.core.data.v3.XMLLinkCollectionImpl;
@ -247,7 +246,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
: new EdmTypeInfo.Builder().setTypeExpression(typeAttrValue).build();
if (typeInfo != null) {
property.setType(typeInfo.getTypeExpression());
property.setType(typeInfo.internal());
}
final ODataPropertyType propType = typeInfo == null
@ -452,7 +451,7 @@ public class AtomDeserializer extends AbstractAtomDealer {
} 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) {
entry.setType(term.getValue());
entry.setType(new EdmTypeInfo.Builder().setTypeExpression(term.getValue()).build().internal());
}
} else if (Constants.QNAME_ATOM_ELEM_LINK.equals(event.asStartElement().getName())) {
final LinkImpl link = new LinkImpl();

View File

@ -94,21 +94,10 @@ public class AtomSerializer extends AbstractAtomDealer {
}
if (StringUtils.isNotBlank(property.getType())) {
String type = property.getType();
if (version.compareTo(ODataServiceVersion.V40) >= 0) {
final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
if (typeInfo.isPrimitiveType()) {
if (typeInfo.isCollection()) {
type = "#Collection(" + typeInfo.getFullQualifiedName().getName() + ")";
} else {
type = typeInfo.getFullQualifiedName().getName();
}
} else {
type = "#" + property.getType();
}
}
final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build();
writer.writeAttribute(Constants.PREFIX_METADATA, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA),
Constants.ATTR_TYPE, type);
Constants.ATTR_TYPE,
new EdmTypeInfo.Builder().setTypeExpression(property.getType()).build().external(version));
}
if (property.getValue().isNull()) {
@ -223,7 +212,8 @@ public class AtomSerializer extends AbstractAtomDealer {
writer.writeStartElement(Constants.ATOM_ELEM_CATEGORY);
writer.writeAttribute(Constants.ATOM_ATTR_SCHEME, version.getNamespaceMap().get(ODataServiceVersion.NS_SCHEME));
writer.writeAttribute(Constants.ATOM_ATTR_TERM, entry.getType());
writer.writeAttribute(Constants.ATOM_ATTR_TERM,
new EdmTypeInfo.Builder().setTypeExpression(entry.getType()).build().external(version));
writer.writeEndElement();
if (entry instanceof AbstractODataObject) {

View File

@ -35,8 +35,8 @@ import org.apache.olingo.commons.api.data.Container;
import org.apache.olingo.commons.api.data.Link;
import org.apache.olingo.commons.api.domain.ODataLinkType;
import org.apache.olingo.commons.api.domain.ODataOperation;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
/**
* Reads JSON string into an entry.
@ -87,7 +87,7 @@ public class JSONEntryDeserializer extends AbstractJsonDeserializer<JSONEntryImp
}
if (tree.hasNonNull(jsonType)) {
entry.setType(tree.get(jsonType).textValue());
entry.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
tree.remove(jsonType);
}
@ -183,7 +183,9 @@ public class JSONEntryDeserializer extends AbstractJsonDeserializer<JSONEntryImp
} else {
final JSONPropertyImpl property = new JSONPropertyImpl();
property.setName(field.getKey());
property.setType(type);
property.setType(type == null
? null
: new EdmTypeInfo.Builder().setTypeExpression(type).build().internal());
type = null;
value(property, field.getValue(), parser.getCodec());

View File

@ -27,6 +27,7 @@ import org.apache.olingo.commons.api.data.Entry;
import org.apache.olingo.commons.api.data.Link;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
/**
* Writes out JSON string from an entry.
@ -40,7 +41,8 @@ public class JSONEntrySerializer extends AbstractJsonSerializer<JSONEntryImpl> {
jgen.writeStartObject();
if (StringUtils.isNotBlank(entry.getType())) {
jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE), entry.getType());
jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE),
new EdmTypeInfo.Builder().setTypeExpression(entry.getType()).build().external(version));
}
if (entry.getId() != null) {

View File

@ -27,6 +27,7 @@ import java.net.URI;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.Constants;
import org.apache.olingo.commons.api.data.Container;
import org.apache.olingo.commons.core.edm.EdmTypeInfo;
/**
* Parse JSON string into <tt>JSONPropertyImpl</tt>.
@ -58,14 +59,15 @@ public class JSONPropertyDeserializer extends AbstractJsonDeserializer<JSONPrope
tree.remove(Constants.JSON_CONTEXT);
} else if (tree.hasNonNull(Constants.JSON_METADATA)) {
contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue());
property.setType(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#"));
property.setType(new EdmTypeInfo.Builder().
setTypeExpression(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#")).build().internal());
tree.remove(Constants.JSON_METADATA);
} else {
contextURL = null;
}
if (tree.has(jsonType)) {
property.setType(tree.get(jsonType).asText());
property.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal());
}
if (tree.has(Constants.JSON_NULL) && tree.get(Constants.JSON_NULL).asBoolean()) {

View File

@ -26,6 +26,7 @@ import org.apache.olingo.commons.api.edm.EdmEnumType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveType;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -137,8 +138,47 @@ public class EdmTypeInfo {
}
}
public String getTypeExpression() {
return typeExpression;
public String internal() {
final StringBuilder deserialize = new StringBuilder();
if (isCollection()) {
deserialize.append("Collection(");
}
deserialize.append(getFullQualifiedName().toString());
if (isCollection()) {
deserialize.append(")");
}
return deserialize.toString();
}
public String external(final ODataServiceVersion version) {
final StringBuilder serialize = new StringBuilder();
if (isCollection()) {
if (version.compareTo(ODataServiceVersion.V40) >= 0) {
serialize.append('#');
}
serialize.append("Collection(");
}
if (isPrimitiveType() && version.compareTo(ODataServiceVersion.V40) >= 0) {
serialize.append(getFullQualifiedName().getName());
} else {
serialize.append(getFullQualifiedName().toString());
}
if (isCollection()) {
serialize.append(")");
}
if (version.compareTo(ODataServiceVersion.V40) >= 0 && !isPrimitiveType() && !isCollection()) {
serialize.insert(0, '#');
}
return serialize.toString();
}
public boolean isCollection() {