コード例 #1
0
        internal Type NewStructuredType(IEdmStructuredType structuredType, Type clrType, ApiVersion apiVersion)
        {
            Contract.Requires(structuredType != null);
            Contract.Requires(clrType != null);
            Contract.Requires(apiVersion != null);
            Contract.Ensures(Contract.Result <Type>() != null);

            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

            var properties           = new List <ClassProperty>();
            var structuralProperties = new HashSet <string>(structuredType.StructuralProperties().Select(p => p.Name), StringComparer.OrdinalIgnoreCase);

            foreach (var property in clrType.GetProperties(bindingFlags))
            {
                if (structuralProperties.Contains(property.Name))
                {
                    properties.Add(new ClassProperty(property));
                }
            }

            var name      = clrType.FullName;
            var signature = new ClassSignature(name, properties, apiVersion);

            return(generatedTypes.GetOrAdd(signature, CreateFromSignature));
        }
コード例 #2
0
        /// <summary>
        /// Create a map of string/<see cref="OpenApiSchema"/> map for a <see cref="IEdmStructuredType"/>'s all properties.
        /// </summary>
        /// <param name="context">The OData context.</param>
        /// <param name="structuredType">The Edm structured type.</param>
        /// <returns>The created map of <see cref="OpenApiSchema"/>.</returns>
        public static IDictionary <string, OpenApiSchema> CreateStructuredTypeCombinedPropertiesSchema(this ODataContext context, IEdmStructuredType structuredType)
        {
            Utils.CheckArgumentNull(context, nameof(context));
            Utils.CheckArgumentNull(structuredType, nameof(structuredType));

            // The name is the property name, the value is a Schema Object describing the allowed values of the property.
            IDictionary <string, OpenApiSchema> properties = new Dictionary <string, OpenApiSchema>();

            // structure properties
            foreach (var property in structuredType.StructuralProperties())
            {
                // OpenApiSchema propertySchema = property.Type.CreateSchema();
                // propertySchema.Default = property.DefaultValueString != null ? new OpenApiString(property.DefaultValueString) : null;
                if (property.Type.FullName() != structuredType.FullTypeName() &&
                    property.Type.FullName() != $"Collection({structuredType.FullTypeName()})")
                {
                    properties.Add(property.Name, context.CreatePropertySchema(property));
                }
            }

            // navigation properties
            foreach (var property in structuredType.NavigationProperties())
            {
                if (property.Type.FullName() != structuredType.FullTypeName() &&
                    property.Type.FullName() != $"Collection({structuredType.FullTypeName()})")
                {
                    OpenApiSchema propertySchema = context.CreateEdmTypeSchema(property.Type);
                    properties.Add(property.Name, propertySchema);
                }
            }

            return(properties);
        }
コード例 #3
0
        static JObject CreateSwaggerDefinitionForStructureType(IEdmStructuredType edmType)
        {
            JObject swaggerProperties = new JObject();

            foreach (var property in edmType.StructuralProperties())
            {
                JObject swaggerProperty = new JObject().Description(property.Name);
                SetSwaggerType(swaggerProperty, property.Type.Definition);
                swaggerProperties.Add(property.Name, swaggerProperty);
            }

            //Add support for base type
            //Root base type to be ignored: microsoft.graph.entity

            string baseType = "";

            if (edmType.BaseType() != null)
            {
                baseType = edmType.BaseType().FullTypeName();
            }
            JObject _EntityDefintiion = null;

            if (baseType != "microsoft.graph.entity")
            {
                if (baseType == "microsoft.graph.directoryObject")
                {
                    swaggerProperties.Remove("id");

                    _EntityDefintiion = new JObject()
                    {
                        { "allOf", new JArray(new JObject()
                            {
                                { "$ref", "#/definitions/microsoft.graph.directoryObject" }
                            }, new JObject()
                            {
                                { "properties", swaggerProperties }
                            }) }
                    };

                    //
                }
                else
                {
                    _EntityDefintiion = new JObject()
                    {
                        { "properties", swaggerProperties }
                    };
                }
            }
            else
            {
                _EntityDefintiion = new JObject()
                {
                    { "properties", swaggerProperties }
                };
            }


            return(_EntityDefintiion);
        }
コード例 #4
0
        private IEdmStructuralProperty GetStructuralProperty(IEdmStructuredType edmType, string propertyName)
        {
            var property = edmType.StructuralProperties().BestMatch(x => x.Name, propertyName, NameMatchResolver);

            if (property == null)
            {
                throw new UnresolvableObjectException(propertyName, $"Structural property [{propertyName}] not found");
            }

            return(property);
        }
コード例 #5
0
        static bool IsEquivalentTo(this IEdmStructuredType structuredType, Type type)
        {
            Contract.Requires(structuredType != null);
            Contract.Requires(type != null);

            const BindingFlags bindingFlags = Public | Instance;

            var comparer             = StringComparer.OrdinalIgnoreCase;
            var clrProperties        = new HashSet <string>(type.GetProperties(bindingFlags).Select(p => p.Name), comparer);
            var structuralProperties = new HashSet <string>(structuredType.StructuralProperties().Select(p => p.Name), comparer);

            return(structuralProperties.IsSupersetOf(clrProperties));
        }
コード例 #6
0
        // Validate all structured properties of a type, recursing through nested complex typed properties
        private void ValidateProperties(IEdmType edmType, ODataUrlValidationContext context)
        {
            IEdmStructuredType structuredType = edmType as IEdmStructuredType;

            if (structuredType != null)
            {
                foreach (IEdmProperty property in structuredType.StructuralProperties())
                {
                    ValidateItem(property, context, /* impliedProperty */ true);
                    ValidateItem(property.Type.Definition.AsElementType(), context, /* impliedProperty */ true);
                    ValidateProperties(property.Type.Definition.AsElementType(), context);
                }
            }
        }
コード例 #7
0
        public static OpenApiParameter CreateSelect(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmStructuredType structuredType)
        {
            Utils.CheckArgumentNull(context, nameof(context));
            Utils.CheckArgumentNull(target, nameof(target));
            Utils.CheckArgumentNull(structuredType, nameof(structuredType));

            NavigationRestrictionsType navigation = context.Model.GetRecord <NavigationRestrictionsType>(target, CapabilitiesConstants.NavigationRestrictions);

            if (navigation != null && !navigation.IsNavigable)
            {
                return(null);
            }

            IList <IOpenApiAny> selectItems = new List <IOpenApiAny>();

            foreach (var property in structuredType.StructuralProperties())
            {
                selectItems.Add(new OpenApiString(property.Name));
            }

            foreach (var property in structuredType.NavigationProperties())
            {
                if (navigation != null && navigation.IsRestrictedProperty(property.Name))
                {
                    continue;
                }

                selectItems.Add(new OpenApiString(property.Name));
            }

            return(new OpenApiParameter
            {
                Name = "$select",
                In = ParameterLocation.Query,
                Description = "Select properties to be returned",
                Schema = new OpenApiSchema
                {
                    Type = "array",
                    UniqueItems = true,
                    Items = new OpenApiSchema
                    {
                        Type = "string",
                        Enum = selectItems
                    }
                },
                Style = ParameterStyle.Form,
                Explode = false
            });
        }
コード例 #8
0
        private void ValidateSelectItem(SelectItem selectItem, IEdmProperty pathProperty, IEdmStructuredType pathStructuredType,
                                        IEdmModel edmModel)
        {
            PathSelectItem pathSelectItem = selectItem as PathSelectItem;

            if (pathSelectItem != null)
            {
                ODataPathSegment          segment = pathSelectItem.SelectedPath.LastSegment;
                NavigationPropertySegment navigationPropertySegment = segment as NavigationPropertySegment;
                if (navigationPropertySegment != null)
                {
                    IEdmNavigationProperty property = navigationPropertySegment.NavigationProperty;
                    if (EdmHelpers.IsNotNavigable(property, edmModel))
                    {
                        throw new ODataException(Error.Format(SRResources.NotNavigablePropertyUsedInNavigation,
                                                              property.Name));
                    }
                }
                else
                {
                    PropertySegment propertySegment = segment as PropertySegment;
                    if (propertySegment != null)
                    {
                        if (EdmHelpers.IsNotSelectable(propertySegment.Property, pathProperty, pathStructuredType, edmModel,
                                                       _defaultQuerySettings.EnableSelect))
                        {
                            throw new ODataException(Error.Format(SRResources.NotSelectablePropertyUsedInSelect,
                                                                  propertySegment.Property.Name));
                        }
                    }
                }
            }
            else
            {
                WildcardSelectItem wildCardSelectItem = selectItem as WildcardSelectItem;
                if (wildCardSelectItem != null)
                {
                    foreach (var property in pathStructuredType.StructuralProperties())
                    {
                        if (EdmHelpers.IsNotSelectable(property, pathProperty, pathStructuredType, edmModel,
                                                       _defaultQuerySettings.EnableSelect))
                        {
                            throw new ODataException(Error.Format(SRResources.NotSelectablePropertyUsedInSelect,
                                                                  property.Name));
                        }
                    }
                }
            }
        }
コード例 #9
0
        static JObject CreateSwaggerDefinitionForStructureType(IEdmStructuredType edmType)
        {
            JObject swaggerProperties = new JObject();

            foreach (var property in edmType.StructuralProperties())
            {
                JObject swaggerProperty = new JObject().Description(property.Name);
                SetSwaggerType(swaggerProperty, property.Type.Definition);
                swaggerProperties.Add(property.Name, swaggerProperty);
            }
            return(new JObject()
            {
                { "properties", swaggerProperties }
            });
        }
コード例 #10
0
        public static IEnumerable <IEdmStructuralProperty> GetAutoSelectProperties(
            IEdmProperty pathProperty,
            IEdmStructuredType pathStructuredType,
            IEdmModel edmModel,
            ModelBoundQuerySettings querySettings = null)
        {
            List <IEdmStructuralProperty> autoSelectProperties = new List <IEdmStructuralProperty>();
            IEdmEntityType baseEntityType = pathStructuredType as IEdmEntityType;

            if (baseEntityType != null)
            {
                List <IEdmEntityType> entityTypes = new List <IEdmEntityType>();
                entityTypes.Add(baseEntityType);
                entityTypes.AddRange(GetAllDerivedEntityTypes(baseEntityType, edmModel));
                foreach (var entityType in entityTypes)
                {
                    IEnumerable <IEdmStructuralProperty> properties = entityType == baseEntityType
                        ? entityType.StructuralProperties()
                        : entityType.DeclaredStructuralProperties();

                    if (properties != null)
                    {
                        autoSelectProperties.AddRange(
                            properties.Where(
                                property =>
                                IsAutoSelect(property, pathProperty, entityType, edmModel,
                                             querySettings)));
                    }
                }
            }
            else if (pathStructuredType != null)
            {
                IEnumerable <IEdmStructuralProperty> properties = pathStructuredType.StructuralProperties();
                if (properties != null)
                {
                    autoSelectProperties.AddRange(
                        properties.Where(
                            property =>
                            IsAutoSelect(property, pathProperty, pathStructuredType, edmModel,
                                         querySettings)));
                }
            }

            return(autoSelectProperties);
        }
コード例 #11
0
        public static void GetStructuralProperties(IEdmStructuredType structuredType, HashSet <IEdmStructuralProperty> structuralProperties,
                                                   HashSet <IEdmStructuralProperty> nestedStructuralProperties)
        {
            // Be noted: this method is not used anymore. Keeping it unremoved is only for non-breaking changes.
            // If any new requirement for such method, it's better to move to "EdmLibHelpers" class.

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

            if (structuralProperties == null)
            {
                throw Error.ArgumentNull("structuralProperties");
            }

            if (nestedStructuralProperties == null)
            {
                throw Error.ArgumentNull("nestedStructuralProperties");
            }

            foreach (var edmStructuralProperty in structuredType.StructuralProperties())
            {
                if (edmStructuralProperty.Type.IsComplex())
                {
                    nestedStructuralProperties.Add(edmStructuralProperty);
                }
                else if (edmStructuralProperty.Type.IsCollection())
                {
                    if (edmStructuralProperty.Type.AsCollection().ElementType().IsComplex())
                    {
                        nestedStructuralProperties.Add(edmStructuralProperty);
                    }
                    else
                    {
                        structuralProperties.Add(edmStructuralProperty);
                    }
                }
                else
                {
                    structuralProperties.Add(edmStructuralProperty);
                }
            }
        }
コード例 #12
0
        public IDictionary <string, object> ToDictionary(Func <IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
        {
            if (mapperProvider == null)
            {
                throw Error.ArgumentNull("mapperProvider");
            }

            Dictionary <string, object> dictionary = new Dictionary <string, object>();
            IEdmStructuredType          type       = GetEdmType().AsStructured().StructuredDefinition();

            IPropertyMapper mapper = mapperProvider(GetModel(), type);

            if (mapper == null)
            {
                throw Error.InvalidOperation(SRResources.InvalidPropertyMapper, typeof(IPropertyMapper).FullName,
                                             type.FullTypeName());
            }

            if (Container != null)
            {
                dictionary = Container.ToDictionary(mapper, includeAutoSelected: false);
            }

            // The user asked for all the structural properties on this instance.
            if (UseInstanceForProperties && UntypedInstance != null)
            {
                foreach (IEdmStructuralProperty property in type.StructuralProperties())
                {
                    object propertyValue;
                    if (TryGetPropertyValue(property.Name, out propertyValue))
                    {
                        string mappingName = mapper.MapProperty(property.Name);
                        if (String.IsNullOrWhiteSpace(mappingName))
                        {
                            throw Error.InvalidOperation(SRResources.InvalidPropertyMapping, property.Name);
                        }

                        dictionary[mappingName] = propertyValue;
                    }
                }
            }

            return(dictionary);
        }
コード例 #13
0
        // Validate all structured properties of a type, recursing through nested complex typed properties
        private void ValidateProperties(IEdmType edmType, ODataUrlValidationContext context)
        {
            // true if the element is added to the set; false if the element is already in the set.
            if (context.ValidatedTypes.Add(edmType))
            {
                IEdmStructuredType structuredType = edmType as IEdmStructuredType;
                if (structuredType != null)
                {
                    foreach (IEdmProperty property in structuredType.StructuralProperties())
                    {
                        IEdmType elementType = property.Type.Definition.AsElementType();

                        ValidateItem(property, context, /* impliedProperty */ true);
                        ValidateItem(elementType, context, /* impliedProperty */ true);
                        ValidateProperties(elementType, context);
                    }
                }
            }
        }
コード例 #14
0
        public static IEnumerable <object> GetPropertiesOrNestedResourceInfos(object instance, IEdmStructuredType structuredType)
        {
            var nonOpenProperties    = new List <object>();
            var structuralProperties = structuredType.StructuralProperties();

            foreach (var sp in structuralProperties)
            {
                nonOpenProperties.Add(ConvertToODataProperty(instance, sp.Name));
            }

            if (structuredType.IsOpen)
            {
                var openProperties = GetOpenPropertiesOrNestedResourceInfos(instance);
                return(MergeOpenAndNonOpenProperties(nonOpenProperties, openProperties));
            }
            else
            {
                return(nonOpenProperties);
            }
        }
コード例 #15
0
ファイル: SelectExpandNode.cs プロジェクト: matdavies/WebApi
        /// <summary>
        /// Separate the structural properties into two parts:
        /// 1. Complex and collection of complex are nested structural properties.
        /// 2. Others are non-nested structural properties.
        /// </summary>
        /// <param name="structuredType">The structural type of the resource.</param>
        /// <param name="structuralProperties">The non-nested structural properties of the structural type.</param>
        /// <param name="nestedStructuralProperties">The nested structural properties of the structural type.</param>
        public static void GetStructuralProperties(IEdmStructuredType structuredType, HashSet<IEdmStructuralProperty> structuralProperties,
            HashSet<IEdmStructuralProperty> nestedStructuralProperties)
        {
            if (structuredType == null)
            {
                throw Error.ArgumentNull("structuredType");
            }

            if (structuralProperties == null)
            {
                throw Error.ArgumentNull("structuralProperties");
            }

            if (nestedStructuralProperties == null)
            {
                throw Error.ArgumentNull("nestedStructuralProperties");
            }

            foreach (var edmStructuralProperty in structuredType.StructuralProperties())
            {
                if (edmStructuralProperty.Type.IsComplex())
                {
                    nestedStructuralProperties.Add(edmStructuralProperty);
                }
                else if (edmStructuralProperty.Type.IsCollection())
                {
                    if (edmStructuralProperty.Type.AsCollection().ElementType().IsComplex())
                    {
                        nestedStructuralProperties.Add(edmStructuralProperty);
                    }
                    else
                    {
                        structuralProperties.Add(edmStructuralProperty);
                    }
                }
                else
                {
                    structuralProperties.Add(edmStructuralProperty);
                }
            }
        }
コード例 #16
0
        /// <summary>
        ///     Create the Swagger definition for the structure Edm type.
        /// </summary>
        /// <param name="edmType">The structure Edm type.</param>
        /// <returns>
        ///     The <see cref="Schema" /> represents the related structure Edm type.
        /// </returns>
        public static Schema CreateSwaggerDefinitionForStructureType(IEdmStructuredType edmType)
        {
            if (edmType == null)
            {
                return(new Schema());
            }

            var swaggerProperties = new Dictionary <string, Schema>();

            foreach (var property in edmType.StructuralProperties())
            {
                var swaggerProperty = new Schema().Description(property.Name);
                SetSwaggerType(swaggerProperty, property.Type.Definition);
                swaggerProperties.Add(property.Name, swaggerProperty);
            }

            return(new Schema
            {
                properties = swaggerProperties
            });
        }
コード例 #17
0
        /// <summary>
        /// Compares two sets of structural properties (expected value in CSDL, actual as IEdmStructuredType).
        /// This can apply to both entity types and complex types.
        /// </summary>
        /// <param name="propertyElements">The CSDL element representing the properties.</param>
        /// <param name="modelType">The EDM model type to compare against.</param>
        private static void CompareStructuralProperties(IEnumerable <XElement> propertyElements, IEdmStructuredType modelType)
        {
            ExceptionUtilities.Assert(propertyElements.Count() == modelType.StructuralProperties().Count(), "Unexpected number of properties on type " + modelType.TestFullName());
            foreach (var propertyElement in propertyElements)
            {
                var propertyName    = propertyElement.GetAttributeValue("Name");
                var propertyOnModel = modelType.FindProperty(propertyName) as IEdmStructuralProperty;
                ExceptionUtilities.Assert(propertyOnModel != null, "Failed to find structural property " + propertyName + " on type " + modelType.TestFullName());
                CompareType(propertyElement, propertyOnModel.Type);
                // TODO: Enable this.
                // CompareTypeFacets(propertyElement, propertyOnModel.Type);

                string defaultValueString;
                if (propertyElement.TryGetAttributeValue("DefaultValue", out defaultValueString))
                {
                    ExceptionUtilities.Assert(string.Compare(defaultValueString, propertyOnModel.DefaultValueString) == 0, "Unexpected value for DefaultValue");
                }
                else
                {
                    ExceptionUtilities.Assert(string.IsNullOrEmpty(propertyOnModel.DefaultValueString), "Did not expect a value for DefaultValue property");
                }
            }
        }
コード例 #18
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;
            IEdmStructuralProperty edmProperty = previewEdmType.StructuralProperties().FirstOrDefault(p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));

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

            IEdmNavigationProperty navProperty = previewEdmType.NavigationProperties().FirstOrDefault(p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));

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

            return(null);
        }
コード例 #19
0
        /// <summary>
        /// Resolve the <see cref="IEdmProperty"/> using the property name. This method supports the property name case insensitive.
        /// However, ODL only support case-sensitive. Here's the logic:
        /// 1) If we match
        /// </summary>
        /// <param name="structuredType">The given structural type </param>
        /// <param name="propertyName">The given property name.</param>
        /// <returns>The resolved <see cref="IEdmProperty"/>.</returns>
        public static IEdmProperty ResolveProperty(this IEdmStructuredType structuredType, string propertyName)
        {
            if (structuredType == null)
            {
                throw Error.ArgumentNull(nameof(structuredType));
            }

            bool         ambiguous   = false;
            IEdmProperty edmProperty = null;

            foreach (var property in structuredType.StructuralProperties())
            {
                string name = property.Name;
                if (name.Equals(propertyName, StringComparison.OrdinalIgnoreCase))
                {
                    if (name.Equals(propertyName, StringComparison.Ordinal))
                    {
                        return(property);
                    }
                    else if (edmProperty != null)
                    {
                        ambiguous = true;
                    }
                    else
                    {
                        edmProperty = property;
                    }
                }
            }

            if (ambiguous)
            {
                throw new ODataException(Error.Format(SRResources.AmbiguousPropertyNameFound, propertyName));
            }

            return(edmProperty);
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: TomDu/lab
 static JObject CreateSwaggerDefinitionForStructureType(IEdmStructuredType edmType)
 {
     JObject swaggerProperties = new JObject();
     foreach (var property in edmType.StructuralProperties())
     {
         JObject swaggerProperty = new JObject().Description(property.Name);
         SetSwaggerType(swaggerProperty, property.Type.Definition);
         swaggerProperties.Add(property.Name, swaggerProperty);
     }
     return new JObject()
     {
         {"properties", swaggerProperties}
     };
 }
コード例 #21
0
        /// <summary>
        /// Determine if the
        /// </summary>
        /// <param name="responseValue">The response value.</param>
        /// <param name="singleResultCollection">The content as SingleResult.Queryable.</param>
        /// <param name="actionDescriptor">The action context, i.e. action and controller name.</param>
        /// <param name="request">The OData path.</param>
        /// <returns></returns>
        private static bool ContainsAutoSelectExpandProperty(
            object responseValue,
            IQueryable singleResultCollection,
            ControllerActionDescriptor actionDescriptor,
            HttpRequest request)
        {
            Type elementClrType = GetElementType(responseValue, singleResultCollection, actionDescriptor);

            IEdmModel model = GetModel(elementClrType, request, actionDescriptor);

            if (model == null)
            {
                throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
            }
            ODataPath          path           = request.ODataFeature().Path;
            IEdmType           edmType        = model.GetTypeMappingCache().GetEdmType(elementClrType, model)?.Definition;
            IEdmEntityType     baseEntityType = edmType as IEdmEntityType;
            IEdmStructuredType structuredType = edmType as IEdmStructuredType;
            IEdmProperty       property       = null;

            if (path != null)
            {
                string name;
                EdmHelpers.GetPropertyAndStructuredTypeFromPath(path, out property, out structuredType, out name);
            }

            if (baseEntityType != null)
            {
                List <IEdmEntityType> entityTypes = new List <IEdmEntityType>();
                entityTypes.Add(baseEntityType);
                entityTypes.AddRange(EdmHelpers.GetAllDerivedEntityTypes(baseEntityType, model));
                foreach (var entityType in entityTypes)
                {
                    IEnumerable <IEdmNavigationProperty> navigationProperties = entityType == baseEntityType
                        ? entityType.NavigationProperties()
                        : entityType.DeclaredNavigationProperties();

                    if (navigationProperties != null)
                    {
                        if (navigationProperties.Any(
                                navigationProperty =>
                                EdmHelpers.IsAutoExpand(navigationProperty, property, entityType, model)))
                        {
                            return(true);
                        }
                    }

                    IEnumerable <IEdmStructuralProperty> properties = entityType == baseEntityType
                        ? entityType.StructuralProperties()
                        : entityType.DeclaredStructuralProperties();

                    if (properties != null)
                    {
                        foreach (var edmProperty in properties)
                        {
                            if (EdmHelpers.IsAutoSelect(edmProperty, property, entityType, model))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (structuredType != null)
            {
                IEnumerable <IEdmStructuralProperty> properties = structuredType.StructuralProperties();
                if (properties != null)
                {
                    foreach (var edmProperty in properties)
                    {
                        if (EdmHelpers.IsAutoSelect(edmProperty, property, structuredType, model))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
コード例 #22
0
        public static OpenApiParameter CreateOrderBy(this ODataContext context, IEdmVocabularyAnnotatable target, IEdmStructuredType structuredType)
        {
            Utils.CheckArgumentNull(context, nameof(context));
            Utils.CheckArgumentNull(target, nameof(target));
            Utils.CheckArgumentNull(structuredType, nameof(structuredType));

            SortRestrictionsType sort = context.Model.GetRecord <SortRestrictionsType>(target, CapabilitiesConstants.SortRestrictions);

            if (sort != null && !sort.IsSortable)
            {
                return(null);
            }

            IList <IOpenApiAny> orderByItems = new List <IOpenApiAny>();

            foreach (var property in structuredType.StructuralProperties())
            {
                if (sort != null && sort.IsNonSortableProperty(property.Name))
                {
                    continue;
                }

                bool isAscOnly  = sort != null && sort.IsAscendingOnlyProperty(property.Name);
                bool isDescOnly = sort != null && sort.IsDescendingOnlyProperty(property.Name);
                if (isAscOnly || isDescOnly)
                {
                    if (isAscOnly)
                    {
                        orderByItems.Add(new OpenApiString(property.Name));
                    }
                    else
                    {
                        orderByItems.Add(new OpenApiString(property.Name + " desc"));
                    }
                }
                else
                {
                    orderByItems.Add(new OpenApiString(property.Name));
                    orderByItems.Add(new OpenApiString(property.Name + " desc"));
                }
            }

            return(new OpenApiParameter
            {
                Name = "$orderby",
                In = ParameterLocation.Query,
                Description = "Order items by property values",
                Schema = new OpenApiSchema
                {
                    Type = "array",
                    UniqueItems = true,
                    Items = new OpenApiSchema
                    {
                        Type = "string",
                        Enum = orderByItems
                    }
                },
                Style = ParameterStyle.Form,
                Explode = false
            });
        }
コード例 #23
0
        /// <summary>
        /// Determine if the
        /// </summary>
        /// <param name="responseValue">The response value.</param>
        /// <param name="singleResultCollection">The content as SingleResult.Queryable.</param>
        /// <param name="actionDescriptor">The action context, i.e. action and controller name.</param>
        /// <param name="modelFunction">A function to get the model.</param>
        /// <param name="path">The OData path.</param>
        /// <returns></returns>
        private static bool ContainsAutoSelectExpandProperty(
            object responseValue,
            IQueryable singleResultCollection,
            IWebApiActionDescriptor actionDescriptor,
            Func <Type, IEdmModel> modelFunction,
            ODataPath path)
        {
            Type elementClrType = GetElementType(responseValue, singleResultCollection, actionDescriptor);

            IEdmModel model = modelFunction(elementClrType);

            if (model == null)
            {
                throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
            }

            IEdmType type = null;
            IEdmModelClrTypeMappingHandler typeMappingHandler = model.GetAnnotationValue <IEdmModelClrTypeMappingHandler>(model);

            if (typeMappingHandler != null)
            {
                type = typeMappingHandler.MapClrInstanceToEdmType(model, responseValue);
                type = EdmLibHelpers.UnwrapCollectionType(type);
            }

            if (type == null)
            {
                type = model.GetEdmType(elementClrType);
            }

            IEdmEntityType     baseEntityType = type as IEdmEntityType;
            IEdmStructuredType structuredType = type as IEdmStructuredType;
            IEdmProperty       property       = null;

            if (path != null)
            {
                string name;
                EdmLibHelpers.GetPropertyAndStructuredTypeFromPath(path.Segments, out property,
                                                                   out structuredType,
                                                                   out name);
            }

            if (baseEntityType != null)
            {
                List <IEdmEntityType> entityTypes = new List <IEdmEntityType>();
                entityTypes.Add(baseEntityType);
                entityTypes.AddRange(EdmLibHelpers.GetAllDerivedEntityTypes(baseEntityType, model));
                foreach (var entityType in entityTypes)
                {
                    IEnumerable <IEdmNavigationProperty> navigationProperties = entityType == baseEntityType
                        ? entityType.NavigationProperties()
                        : entityType.DeclaredNavigationProperties();

                    if (navigationProperties != null)
                    {
                        if (navigationProperties.Any(
                                navigationProperty =>
                                EdmLibHelpers.IsAutoExpand(navigationProperty, property, entityType, model)))
                        {
                            return(true);
                        }
                    }

                    IEnumerable <IEdmStructuralProperty> properties = entityType == baseEntityType
                        ? entityType.StructuralProperties()
                        : entityType.DeclaredStructuralProperties();

                    if (properties != null)
                    {
                        foreach (var edmProperty in properties)
                        {
                            if (EdmLibHelpers.IsAutoSelect(edmProperty, property, entityType, model))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (structuredType != null)
            {
                IEnumerable <IEdmStructuralProperty> properties = structuredType.StructuralProperties();
                if (properties != null)
                {
                    foreach (var edmProperty in properties)
                    {
                        if (EdmLibHelpers.IsAutoSelect(edmProperty, property, structuredType, model))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
コード例 #24
0
        private bool ContainsAutoSelectExpandProperty(object response, HttpRequestMessage request,
                                                      HttpActionDescriptor actionDescriptor)
        {
            Type elementClrType = GetElementType(response, actionDescriptor);

            IEdmModel model = GetModel(elementClrType, request, actionDescriptor);

            if (model == null)
            {
                throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
            }

            IEdmEntityType     baseEntityType = model.GetEdmType(elementClrType) as IEdmEntityType;
            IEdmStructuredType structuredType = model.GetEdmType(elementClrType) as IEdmStructuredType;
            IEdmProperty       property       = null;

            if (request.ODataProperties().Path != null)
            {
                string name;
                EdmLibHelpers.GetPropertyAndStructuredTypeFromPath(request.ODataProperties().Path.Segments, out property,
                                                                   out structuredType,
                                                                   out name);
            }

            if (baseEntityType != null)
            {
                List <IEdmEntityType> entityTypes = new List <IEdmEntityType>();
                entityTypes.Add(baseEntityType);
                entityTypes.AddRange(EdmLibHelpers.GetAllDerivedEntityTypes(baseEntityType, model));
                foreach (var entityType in entityTypes)
                {
                    IEnumerable <IEdmNavigationProperty> navigationProperties = entityType == baseEntityType
                        ? entityType.NavigationProperties()
                        : entityType.DeclaredNavigationProperties();

                    if (navigationProperties != null)
                    {
                        if (navigationProperties.Any(
                                navigationProperty =>
                                EdmLibHelpers.IsAutoExpand(navigationProperty, property, entityType, model)))
                        {
                            return(true);
                        }
                    }

                    IEnumerable <IEdmStructuralProperty> properties = entityType == baseEntityType
                        ? entityType.StructuralProperties()
                        : entityType.DeclaredStructuralProperties();

                    if (properties != null)
                    {
                        foreach (var edmProperty in properties)
                        {
                            if (EdmLibHelpers.IsAutoSelect(edmProperty, property, entityType, model))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (structuredType != null)
            {
                IEnumerable <IEdmStructuralProperty> properties = structuredType.StructuralProperties();
                if (properties != null)
                {
                    foreach (var edmProperty in properties)
                    {
                        if (EdmLibHelpers.IsAutoSelect(edmProperty, property, structuredType, model))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
コード例 #25
0
        public static IEnumerable<ODataProperty> GetProperties(object instance, IEdmStructuredType structuredType)
        {
            var nonOpenProperties = new List<ODataProperty>();
            var structuralProperties = structuredType.StructuralProperties();
            foreach (var sp in structuralProperties)
            {
                nonOpenProperties.Add(ConvertToODataProperty(instance, sp.Name));
            }

            if (structuredType.IsOpen)
            {
                var openProperties = GetOpenProperties(instance);
                return MergeOpenAndNonOpenProperties(nonOpenProperties, openProperties);
            }
            else
            {
                return nonOpenProperties;
            }
        }