Esempio n. 1
0
        public void ResolveProperty_ThrowsArugmentNull()
        {
            // Arrange & Act & Assert
            IEdmStructuredType structuredType = null;

            ExceptionAssert.ThrowsArgumentNull(() => structuredType.ResolveProperty(null), "structuredType");
        }
Esempio n. 2
0
        private static DynamicPathSegment CreateDynamicSegment(ODataPathSegment previous, ODataTemplateTranslateContext context)
        {
            if (previous == null)
            {
                return(null);
            }

            IEdmStructuredType previewEdmType = previous.EdmType as IEdmStructuredType;

            if (previewEdmType == null || !previewEdmType.IsOpen)
            {
                return(null);
            }

            if (!context.RouteValues.TryGetValue("dynamicproperty", out object value))
            {
                return(null);
            }

            string       propertyName = value as string;
            IEdmProperty edmProperty  = previewEdmType.ResolveProperty(propertyName);

            if (edmProperty != null)
            {
                return(null);
            }

            return(new DynamicPathSegment(propertyName));
        }
Esempio n. 3
0
        /// <summary>
        /// Try to bind the idenfier as property segment,
        /// Append it into path.
        /// </summary>
        private static bool TryBindPropertySegment(string identifier, string parenthesisExpressions, IEdmModel model,
                                                   IList <PathSegment> path,
                                                   PathParserSettings settings)
        {
            PathSegment preSegment = path.LastOrDefault();

            if (preSegment == null || !preSegment.IsSingle)
            {
                return(false);
            }

            IEdmStructuredType structuredType = preSegment.EdmType as IEdmStructuredType;

            if (structuredType == null)
            {
                return(false);
            }

            IEdmProperty property = structuredType.ResolveProperty(identifier, settings.EnableCaseInsensitive);

            if (property == null)
            {
                return(false);
            }

            PathSegment segment;

            if (property.PropertyKind == EdmPropertyKind.Navigation)
            {
                var navigationProperty = (IEdmNavigationProperty)property;

                IEdmNavigationSource navigationSource = null;
                if (preSegment.NavigationSource != null)
                {
                    IEdmPathExpression bindingPath;
                    navigationSource = preSegment.NavigationSource.FindNavigationTarget(navigationProperty, path, out bindingPath);
                }

                // Relationship between TargetMultiplicity and navigation property:
                //  1) EdmMultiplicity.Many <=> collection navigation property
                //  2) EdmMultiplicity.ZeroOrOne <=> nullable singleton navigation property
                //  3) EdmMultiplicity.One <=> non-nullable singleton navigation property
                segment = new NavigationSegment(navigationProperty, navigationSource, identifier);
            }
            else
            {
                segment = new PropertySegment((IEdmStructuralProperty)property, preSegment.NavigationSource, identifier);
            }

            path.Add(segment);

            if (parenthesisExpressions != null && !property.Type.IsCollection() && !property.Type.AsCollection().ElementType().IsEntity())
            {
                throw new Exception($"Invalid '{parenthesisExpressions}' after property '{identifier}'.");
            }

            TryBindKeySegment(parenthesisExpressions, path);
            return(true);
        }
Esempio n. 4
0
        private static void VerifyResolvedProperty(IEdmStructuredType structuredType, string propertyName, string expectedName, string expectedTypeName)
        {
            IEdmProperty property = structuredType.ResolveProperty(propertyName);

            Assert.NotNull(property);

            Assert.Equal(expectedName, property.Name);
            Assert.Equal(expectedTypeName, property.Type.FullName());
        }
Esempio n. 5
0
        /// <summary>
        /// Find the property using the given <see cref="IEdmPathExpression"/> starting from the given <see cref="IEdmStructuredType"/>.
        /// </summary>
        /// <param name="model">The Edm model.</param>
        /// <param name="structuredType">The structured type.</param>
        /// <param name="path">The property path.</param>
        /// <returns>Null or the found edm property.</returns>
        public static IEdmProperty FindProperty(this IEdmModel model, IEdmStructuredType structuredType, IEdmPathExpression path)
        {
            if (model == null)
            {
                throw Error.ArgumentNull(nameof(model));
            }

            if (structuredType == null)
            {
                throw Error.ArgumentNull(nameof(structuredType));
            }

            if (path == null)
            {
                throw Error.ArgumentNull(nameof(path));
            }

            IEdmProperty       property     = null;
            IEdmStructuredType startingType = structuredType;

            foreach (var segment in path.PathSegments)
            {
                if (string.IsNullOrEmpty(segment))
                {
                    // Let's simply ignore the empty segment
                    continue;
                }

                // So far, we only support "property and type cast in the path"
                if (segment.Contains('.', StringComparison.Ordinal))
                {
                    startingType = model.ResolveType(segment) as IEdmStructuredType;
                    if (startingType == null)
                    {
                        throw new ODataException(Error.Format(SRResources.ResourceTypeNotInModel, segment));
                    }
                }
                else
                {
                    if (startingType == null)
                    {
                        return(null);
                    }

                    property = startingType.ResolveProperty(segment);
                    if (property == null)
                    {
                        throw new ODataException(Error.Format(SRResources.PropertyNotFoundOnPathExpression, path.Path, structuredType.FullTypeName()));
                    }

                    startingType = property.Type.GetElementTypeOrSelf().Definition as IEdmStructuredType;
                }
            }

            return(property);
        }
Esempio n. 6
0
        /// <summary>Helper method to resolve a property.</summary>
        /// <param name="model">The model.</param>
        /// <param name="typeAndPropertyName">The name of the type and property to find, like 'Type.Property'.</param>
        /// <returns>The property found or null otherwise.</returns>
        public static IEdmProperty ResolveProperty(this IEdmModel model, string typeAndPropertyName)
        {
            int                lastDot      = typeAndPropertyName.LastIndexOf('.');
            string             propertyName = typeAndPropertyName.Substring(lastDot + 1);
            string             typeName     = typeAndPropertyName.Substring(0, lastDot);
            IEdmStructuredType type         = model.ResolveType(typeName) as IEdmStructuredType;

            ExceptionUtilities.Assert(type != null, "Could not resolve name '" + typeName + "' to a structured type.");
            return(type.ResolveProperty(propertyName));
        }
Esempio n. 7
0
        internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource,
                                           IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmStructuredType structuredType = resourceType.StructuredDefinition();
            IEdmProperty       edmProperty    = structuredType == null ? null : structuredType.ResolveProperty(property.Name);

            bool   isDynamicProperty = false;
            string propertyName      = property.Name;

            if (edmProperty != null)
            {
                propertyName = readContext.Model.GetClrPropertyName(edmProperty);
            }
            else
            {
                isDynamicProperty = structuredType != null && structuredType.IsOpen;
            }

            if (!isDynamicProperty && edmProperty == null)
            {
                throw new ODataException(
                          Error.Format(SRResources.CannotDeserializeUnknownProperty, property.Name, resourceType.Definition));
            }

            // dynamic properties have null values
            IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null;

            EdmTypeKind propertyKind;
            object      value = ConvertValue(property.Value, ref propertyType, deserializerProvider, readContext,
                                             out propertyKind);

            if (isDynamicProperty)
            {
                SetDynamicProperty(resource, resourceType, propertyKind, propertyName, value, propertyType,
                                   readContext.Model);
            }
            else
            {
                SetDeclaredProperty(resource, propertyKind, propertyName, value, edmProperty, readContext);
            }
        }
Esempio n. 8
0
        private static ODataPathSegment CreatePropertySegment(ODataPathSegment previous, ODataTemplateTranslateContext context)
        {
            if (previous == null)
            {
                return(null);
            }

            IEdmStructuredType previewEdmType = previous.EdmType as IEdmStructuredType;

            if (previewEdmType == null)
            {
                return(null);
            }

            if (!context.RouteValues.TryGetValue("property", out object value))
            {
                return(null);
            }

            string                 propertyName       = value as string;
            IEdmProperty           edmProperty        = previewEdmType.ResolveProperty(propertyName);
            IEdmStructuralProperty structuralProperty = edmProperty as IEdmStructuralProperty;

            if (structuralProperty != null)
            {
                return(new PropertySegment(structuralProperty));
            }

            IEdmNavigationProperty navProperty = edmProperty as IEdmNavigationProperty;

            if (navProperty != null)
            {
                // TODO: shall we calculate the navigation source for navigation segment?
                return(new NavigationPropertySegment(navProperty, null));
            }

            return(null);
        }