/// <summary> /// Adds a path to the source and target tree which is obtained by looking at the EntityPropertyMappingAttribute in the <paramref name="epmInfo"/> /// </summary> /// <param name="epmInfo">EnitityPropertyMappingInfo holding the source path</param> internal void Add(EntityPropertyMappingInfo epmInfo) { DebugUtils.CheckNoExternalCallers(); List <EpmSourcePathSegment> pathToCurrentSegment = new List <EpmSourcePathSegment>(); EpmSourcePathSegment currentSourceSegment = this.Root; EpmSourcePathSegment foundSourceSegment = null; IEdmType currentType = epmInfo.ActualPropertyType; Debug.Assert(!string.IsNullOrEmpty(epmInfo.Attribute.SourcePath), "Invalid source path"); string[] propertyPath = epmInfo.Attribute.SourcePath.Split('/'); int propertyPathLength = propertyPath.Length; Debug.Assert(propertyPathLength > 0, "Must have been validated during EntityPropertyMappingAttribute construction"); for (int sourcePropertyIndex = 0; sourcePropertyIndex < propertyPathLength; sourcePropertyIndex++) { string propertyName = propertyPath[sourcePropertyIndex]; if (propertyName.Length == 0) { throw new ODataException(ODataErrorStrings.EpmSourceTree_InvalidSourcePath(epmInfo.DefiningType.ODataFullName(), epmInfo.Attribute.SourcePath)); } IEdmTypeReference nextPropertyTypeReference = GetPropertyType(currentType, propertyName); IEdmType nextPropertyType = nextPropertyTypeReference == null ? null : nextPropertyTypeReference.Definition; // If we don't find a property type this is an open property; check whether this is the last segment in the path // since otherwise this would not be an open primitive property and only open primitive properties are allowed. if (nextPropertyType == null && sourcePropertyIndex < propertyPathLength - 1) { throw new ODataException(ODataErrorStrings.EpmSourceTree_OpenComplexPropertyCannotBeMapped(propertyName, currentType.ODataFullName())); } currentType = nextPropertyType; foundSourceSegment = currentSourceSegment.SubProperties.SingleOrDefault(e => e.PropertyName == propertyName); if (foundSourceSegment != null) { currentSourceSegment = foundSourceSegment; } else { EpmSourcePathSegment newSourceSegment = new EpmSourcePathSegment(propertyName); currentSourceSegment.SubProperties.Add(newSourceSegment); currentSourceSegment = newSourceSegment; } pathToCurrentSegment.Add(currentSourceSegment); } // The last segment is the one being mapped from by the user specified attribute. // It must be a primitive type - we don't allow mappings of anything else than primitive properties directly. // Note that we can only verify this for non-open properties, for open properties we must assume it's a primitive type // and we will make this check later during serialization again when we actually have the value of the property. if (currentType != null) { if (!currentType.IsODataPrimitiveTypeKind()) { throw new ODataException(ODataErrorStrings.EpmSourceTree_EndsWithNonPrimitiveType(currentSourceSegment.PropertyName)); } } Debug.Assert(foundSourceSegment == null || foundSourceSegment.EpmInfo != null, "Can't have a leaf node in the tree without EpmInfo."); // Two EpmAttributes with same PropertyName in the same type, this could be a result of inheritance if (foundSourceSegment != null) { Debug.Assert(object.ReferenceEquals(foundSourceSegment, currentSourceSegment), "currentSourceSegment variable should have been updated already to foundSourceSegment"); // Check for duplicates on the same entity type Debug.Assert(foundSourceSegment.SubProperties.Count == 0, "If non-leaf, it means we allowed complex type to be a leaf node"); if (foundSourceSegment.EpmInfo.DefiningTypesAreEqual(epmInfo)) { throw new ODataException(ODataErrorStrings.EpmSourceTree_DuplicateEpmAttributesWithSameSourceName(epmInfo.DefiningType.ODataFullName(), epmInfo.Attribute.SourcePath)); } // In case of inheritance, we need to remove the node from target tree which was mapped to base type property this.epmTargetTree.Remove(foundSourceSegment.EpmInfo); } epmInfo.SetPropertyValuePath(pathToCurrentSegment.ToArray()); currentSourceSegment.EpmInfo = epmInfo; this.epmTargetTree.Add(epmInfo); }
/// <summary> /// Reads a property value starting with the specified index to the property value path. /// </summary> /// <param name="epmInfo">The EPM info which describes the mapping for which to read the property value.</param> /// <param name="cachedProperties">The enumeration of properties to search for the first property in the property value path.</param> /// <param name="sourceSegmentIndex">The index in the property value path to start with.</param> /// <param name="structuredTypeReference">The type of the entry or complex value the <paramref name="cachedProperties"/> enumeration belongs to.</param> /// <param name="epmValueCache">The EPM value cache to use.</param> /// <returns>The value of the property (may be null), or null if the property itself was not found due to one of its parent properties being null.</returns> private object ReadPropertyValue( EntityPropertyMappingInfo epmInfo, IEnumerable <ODataProperty> cachedProperties, int sourceSegmentIndex, IEdmStructuredTypeReference structuredTypeReference, EpmValueCache epmValueCache) { Debug.Assert(epmInfo != null, "epmInfo != null"); Debug.Assert(epmInfo.PropertyValuePath != null, "The PropertyValuePath should have been initialized by now."); Debug.Assert(epmInfo.PropertyValuePath.Length > sourceSegmentIndex, "The PropertyValuePath must be at least as long as the source segment index."); Debug.Assert(structuredTypeReference != null, "structuredTypeReference != null"); Debug.Assert(epmValueCache != null, "epmValueCache != null"); EpmSourcePathSegment sourceSegment = epmInfo.PropertyValuePath[sourceSegmentIndex]; string propertyName = sourceSegment.PropertyName; bool lastSegment = epmInfo.PropertyValuePath.Length == sourceSegmentIndex + 1; IEdmStructuredType structuredType = structuredTypeReference.StructuredDefinition(); IEdmProperty edmProperty = WriterValidationUtils.ValidatePropertyDefined(propertyName, structuredType, this.atomOutputContext.MessageWriterSettings.UndeclaredPropertyBehaviorKinds); IEdmTypeReference propertyTypeReference = null; if (edmProperty != null) { propertyTypeReference = edmProperty.Type; // If this is the last part of the path, then it has to be a primitive or atomic collection type otherwise should be a complex type if (lastSegment) { if (!propertyTypeReference.IsODataPrimitiveTypeKind() && !propertyTypeReference.IsNonEntityCollectionType()) { throw new ODataException(ODataErrorStrings.EpmSourceTree_EndsWithNonPrimitiveType(propertyName)); } } else { if (propertyTypeReference.TypeKind() != EdmTypeKind.Complex) { throw new ODataException(ODataErrorStrings.EpmSourceTree_TraversalOfNonComplexType(propertyName)); } } } else { Debug.Assert( structuredType.IsOpen, "Only open types can have undeclared properties, otherwise we should have failed in the ValidatePropertyDefined method."); } ODataProperty property = cachedProperties == null ? null : cachedProperties.FirstOrDefault(p => p.Name == propertyName); if (property == null) { throw new ODataException(ODataErrorStrings.EpmSourceTree_MissingPropertyOnInstance(propertyName, structuredTypeReference.ODataFullName())); } object propertyValue = property.Value; ODataComplexValue propertyComplexValue = propertyValue as ODataComplexValue; if (lastSegment) { if (propertyValue == null) { WriterValidationUtils.ValidateNullPropertyValue(propertyTypeReference, propertyName, this.WriterBehavior, this.atomOutputContext.Model); } else { // If this property is the last one it has to be either a primitive or collection if (propertyComplexValue != null) { throw new ODataException(ODataErrorStrings.EpmSourceTree_EndsWithNonPrimitiveType(propertyName)); } else { ODataCollectionValue propertyCollectionValue = propertyValue as ODataCollectionValue; if (propertyCollectionValue != null) { // Validate the type name for the collection TypeNameOracle.ResolveAndValidateTypeNameForValue( this.atomOutputContext.Model, propertyTypeReference, propertyCollectionValue, /*isOpenProperty*/ edmProperty == null); } else { if (propertyValue is ODataStreamReferenceValue) { // Stream properties should not come here, if it were an ODataEntry property it would have been // filtered in ReadEntryPropertyValue() by "epmValueCache.EntryProperties" call. throw new ODataException(ODataErrorStrings.ODataWriter_StreamPropertiesMustBePropertiesOfODataEntry(propertyName)); } else if (propertyValue is ISpatial) { throw new ODataException(ODataErrorStrings.EpmSourceTree_OpenPropertySpatialTypeCannotBeMapped(propertyName, epmInfo.DefiningType.FullName())); } else if (propertyTypeReference != null) { ValidationUtils.ValidateIsExpectedPrimitiveType(propertyValue, propertyTypeReference); } } } } return(propertyValue); } // Otherwise it's in the middle and thus it must be a complex value if (propertyComplexValue == null) { if (propertyValue != null) { // It's not a complex value - fail. throw new ODataException(ODataErrorStrings.EpmSourceTree_TraversalOfNonComplexType(propertyName)); } else { // The value of the property is null, which can be a null complex value // Note that we must not attempt to resolve the type as if the type name was null here, because // 1) We don't need the type for anything anyway (the value is null, this is the end) // 2) If the property is open, trying to resolve a null type name would throw // but we don't have a null type name, we have a null entire value. return(null); } } IEdmComplexTypeReference complexValueType = TypeNameOracle.ResolveAndValidateTypeNameForValue( this.atomOutputContext.Model, edmProperty == null ? null : edmProperty.Type, propertyComplexValue, /*isOpenProperty*/ edmProperty == null).AsComplexOrNull(); return(this.ReadComplexPropertyValue( epmInfo, propertyComplexValue, epmValueCache, sourceSegmentIndex + 1, complexValueType)); }