From d4c057b80c5a185401b7233665f69206bea789b6 Mon Sep 17 00:00:00 2001 From: ramya vasanth Date: Thu, 19 Mar 2020 10:02:52 +0530 Subject: [PATCH] [OLINGO-1062]Fallback to fetch full qualified name of Record when the vocab file is not loaded --- .../commons/api/edm/annotation/EdmRecord.java | 7 ++++++ .../core/edm/annotation/EdmRecordImpl.java | 6 +++++ .../json/MetadataDocumentJsonSerializer.java | 23 ++++++++++++++++--- .../xml/MetadataDocumentXmlSerializer.java | 15 +++++++++--- .../MetadataDocumentJsonSerializerTest.java | 13 +++++++++-- 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/annotation/EdmRecord.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/annotation/EdmRecord.java index 7abd08a63..37847d2e3 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/annotation/EdmRecord.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/annotation/EdmRecord.java @@ -22,6 +22,7 @@ import java.util.List; import org.apache.olingo.commons.api.edm.EdmAnnotatable; import org.apache.olingo.commons.api.edm.EdmStructuredType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; /** * The edm:Record expression enables a new entity type or complex type instance to be constructed. @@ -40,4 +41,10 @@ public interface EdmRecord extends EdmDynamicExpression, EdmAnnotatable { * @return Entity type or complex type */ EdmStructuredType getType(); + + /** + * + * @return Fully Qualified Name of the Record + */ + FullQualifiedName getTypeFQN(); } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/annotation/EdmRecordImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/annotation/EdmRecordImpl.java index b8eb9f91a..568f130d2 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/annotation/EdmRecordImpl.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/annotation/EdmRecordImpl.java @@ -25,6 +25,7 @@ import java.util.List; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.EdmException; import org.apache.olingo.commons.api.edm.EdmStructuredType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.annotation.EdmPropertyValue; import org.apache.olingo.commons.api.edm.annotation.EdmRecord; import org.apache.olingo.commons.api.edm.provider.annotation.CsdlPropertyValue; @@ -74,4 +75,9 @@ public class EdmRecordImpl extends AbstractEdmAnnotatableDynamicExpression imple public EdmExpressionType getExpressionType() { return EdmExpressionType.Record; } + + @Override + public FullQualifiedName getTypeFQN() { + return record.getType() != null ? new FullQualifiedName(record.getType()) : null; + } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializer.java index 7ca36d982..f3565403a 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializer.java @@ -36,6 +36,7 @@ import org.apache.olingo.commons.api.edm.EdmEntityContainer; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.EdmEnumType; +import org.apache.olingo.commons.api.edm.EdmException; import org.apache.olingo.commons.api.edm.EdmFunction; import org.apache.olingo.commons.api.edm.EdmFunctionImport; import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; @@ -130,6 +131,8 @@ public class MetadataDocumentJsonSerializer { private static final String ANNOTATION = DOLLAR + "Annotations"; private static final String ANNOTATION_PATH = DOLLAR + "Path"; private static final String NAME = DOLLAR + "Name"; + private static final String ON_DELETE = "OnDelete"; + private static final String ON_DELETE_PROPERTY = "Action"; public MetadataDocumentJsonSerializer(final ServiceMetadata serviceMetadata) throws SerializerException { if (serviceMetadata == null || serviceMetadata.getEdm() == null) { @@ -663,6 +666,13 @@ public class MetadataDocumentJsonSerializer { json.writeEndObject(); } } + + if (navigationProperty.getOnDelete() != null) { + json.writeObjectFieldStart(ON_DELETE); + json.writeStringField(ON_DELETE_PROPERTY, navigationProperty.getOnDelete().getAction()); + appendAnnotations(json, navigationProperty.getOnDelete(), null); + json.writeEndObject(); + } appendAnnotations(json, navigationProperty, null); @@ -993,9 +1003,16 @@ public class MetadataDocumentJsonSerializer { case Record: EdmRecord asRecord = dynExp.asRecord(); json.writeStartObject(); - EdmStructuredType type = asRecord.getType(); - if (type != null) { - json.writeStringField(TYPE, getAliasedFullQualifiedName(type)); + try { + EdmStructuredType structuredType = asRecord.getType(); + if (structuredType != null) { + json.writeStringField(TYPE, getAliasedFullQualifiedName(structuredType)); + } + } catch (EdmException e) { + FullQualifiedName type = asRecord.getTypeFQN(); + if (type != null) { + json.writeStringField(TYPE, getAliasedFullQualifiedName(type)); + } } for (EdmPropertyValue propValue : asRecord.getPropertyValues()) { appendExpression(json, propValue.getValue(), propValue.getProperty()); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java index 626ca7e22..71af4add5 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializer.java @@ -37,6 +37,7 @@ import org.apache.olingo.commons.api.edm.EdmEntityContainer; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.EdmEnumType; +import org.apache.olingo.commons.api.edm.EdmException; import org.apache.olingo.commons.api.edm.EdmFunction; import org.apache.olingo.commons.api.edm.EdmFunctionImport; import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; @@ -455,10 +456,18 @@ public class MetadataDocumentXmlSerializer { break; case Record: EdmRecord asRecord = dynExp.asRecord(); - EdmStructuredType type = asRecord.getType(); - if (type != null) { - writer.writeAttribute(XML_TYPE, getAliasedFullQualifiedName(type, false)); + try { + EdmStructuredType structuredType = asRecord.getType(); + if (structuredType != null) { + writer.writeAttribute(XML_TYPE, getAliasedFullQualifiedName(structuredType, false)); + } + } catch (EdmException e) { + FullQualifiedName type = asRecord.getTypeFQN(); + if (type != null) { + writer.writeAttribute(XML_TYPE, getAliasedFullQualifiedName(type, false)); + } } + for (EdmPropertyValue propValue : asRecord.getPropertyValues()) { writer.writeStartElement(XML_PROPERTY_VALUE); writer.writeAttribute(XML_PROPERTY, propValue.getProperty()); diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializerTest.java index 2b9125867..38b7844f4 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/MetadataDocumentJsonSerializerTest.java @@ -64,6 +64,8 @@ import org.apache.olingo.commons.api.edm.provider.CsdlFunction; import org.apache.olingo.commons.api.edm.provider.CsdlFunctionImport; import org.apache.olingo.commons.api.edm.provider.CsdlNavigationProperty; import org.apache.olingo.commons.api.edm.provider.CsdlNavigationPropertyBinding; +import org.apache.olingo.commons.api.edm.provider.CsdlOnDelete; +import org.apache.olingo.commons.api.edm.provider.CsdlOnDeleteAction; import org.apache.olingo.commons.api.edm.provider.CsdlParameter; import org.apache.olingo.commons.api.edm.provider.CsdlProperty; import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef; @@ -379,7 +381,8 @@ public class MetadataDocumentJsonSerializerTest { + "},\"Info\":" + "{\"$Type\":\"Alias.CTEntityInfo\"}," + "\"NavPropertyETOne\":{\"$Kind\":\"NavigationProperty\"," - + "\"$Type\":\"Alias.ETOne\"}}")); + + "\"$Type\":\"Alias.ETOne\"},\"NavProperty\":{\"$Kind\":\"NavigationProperty\"," + + "\"$Type\":\"Alias.ETAbstract\",\"$Nullable\":false,\"OnDelete\":{\"Action\":\"Cascade\"}}}")); assertTrue(metadata.contains("\"BAETTwoKeyNavRTETTwoKeyNavParam\":" + "[{\"$Kind\":\"Action\",\"$EntitySetPath\":\"BindingParam/NavPropertyETTwoKeyNavOne\"," + "\"$IsBound\":true,\"$Parameter\":[{\"$Name\":\"BindingParam\",\"$Type\":\"Alias.ETTwoKeyNav\"}," @@ -526,6 +529,12 @@ public class MetadataDocumentJsonSerializerTest { .setScale(2) .setDefaultValue("10-2-2017:20:30:40") .setMaxLength(30); + private final CsdlNavigationProperty navProperty = new CsdlNavigationProperty() + .setName("NavProperty") + .setType(nameETAbstract) + .setNullable(false) + .setOnDelete(new CsdlOnDelete().setAction(CsdlOnDeleteAction.Cascade) + .setAnnotations(Arrays.asList(new CsdlAnnotation().setTerm("core.Term")))); private final FullQualifiedName nameCTTwoPrim = new FullQualifiedName(nameSpace, "CTTwoPrim"); private final FullQualifiedName nameCTTwoPrimBase = new FullQualifiedName(nameSpace, "CTTwoPrimBase"); @@ -596,7 +605,7 @@ public class MetadataDocumentJsonSerializerTest { .setKey(Arrays.asList(new CsdlPropertyRef().setAlias("EntityInfoID").setName("Info/ID"), new CsdlPropertyRef().setName("name"))) .setNavigationProperties(Arrays.asList( - new CsdlNavigationProperty().setName("NavPropertyETOne").setType(nameETOne))) + new CsdlNavigationProperty().setName("NavPropertyETOne").setType(nameETOne), navProperty)) .setProperties(Arrays.asList(nameProperty, infoProperty)); } else if (entityTypeName.equals(nameETTwoKeyNav)) { return new CsdlEntityType()