Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ODataValidationSettingsConvention"/> class.
        /// </summary>
        /// <param name="validationSettings">The <see cref="ODataValidationSettings">validation settings</see> the convention is based on.</param>
        /// <param name="settings">The <see cref="ODataQueryOptionSettings">settings</see> used by the convention.</param>
        public ODataValidationSettingsConvention(ODataValidationSettings validationSettings, ODataQueryOptionSettings settings)
        {
            Arg.NotNull(validationSettings, nameof(validationSettings));

            ValidationSettings = validationSettings;
            Settings           = settings;
        }
Exemplo n.º 2
0
        public void apply_should_apply_configured_conventions()
        {
            // arrange
            var controller  = new HttpControllerDescriptor(new HttpConfiguration(), "Stub", typeof(StubController));
            var action      = new ReflectedHttpActionDescriptor(controller, typeof(StubController).GetMethod(nameof(StubController.Get)));
            var description = new VersionedApiDescription()
            {
                ActionDescriptor    = action,
                HttpMethod          = HttpMethod.Get,
                ResponseDescription = new ResponseDescription()
                {
                    ResponseType = typeof(object),
                },
                Properties =
                {
                    [typeof(IEdmModel)] = new EdmModel(),
                }
            };
            var builder  = new ODataQueryOptionsConventionBuilder();
            var settings = new ODataQueryOptionSettings()
            {
                DescriptionProvider = builder.DescriptionProvider
            };
            var convention = new Mock <IODataQueryOptionsConvention>();

            convention.Setup(c => c.ApplyTo(It.IsAny <ApiDescription>()));
            builder.Add(convention.Object);

            // act
            builder.ApplyTo(new[] { description }, settings);

            // assert
            convention.Verify(c => c.ApplyTo(description), Once());
        }
Exemplo n.º 3
0
        public void apply_should_apply_configured_conventions()
        {
            // arrange
            var description = new ApiDescription()
            {
                ActionDescriptor = new ControllerActionDescriptor()
                {
                    ControllerTypeInfo = typeof(StubController).GetTypeInfo(),
                    MethodInfo         = typeof(StubController).GetTypeInfo().GetRuntimeMethod(nameof(StubController.Get), Type.EmptyTypes),
                },
                HttpMethod = "GET",
            };
            var builder  = new ODataQueryOptionsConventionBuilder();
            var settings = new ODataQueryOptionSettings()
            {
                DescriptionProvider   = builder.DescriptionProvider,
                DefaultQuerySettings  = new DefaultQuerySettings(),
                ModelMetadataProvider = Mock.Of <IModelMetadataProvider>(),
            };
            var convention = new Mock <IODataQueryOptionsConvention>();

            convention.Setup(c => c.ApplyTo(It.IsAny <ApiDescription>()));
            builder.Add(convention.Object);

            // act
            builder.ApplyTo(new[] { description }, settings);

            // assert
            convention.Verify(c => c.ApplyTo(description), Once());
        }
Exemplo n.º 4
0
 internal ODataControllerQueryOptionConvention(
     ODataActionQueryOptionConventionLookup lookup,
     ODataQueryOptionSettings settings)
 {
     this.lookup   = lookup;
     this.settings = settings;
 }
Exemplo n.º 5
0
        internal ODataControllerQueryOptionConvention(
            ODataActionQueryOptionConventionLookup lookup,
            ODataQueryOptionSettings settings)
        {
            Contract.Requires(lookup != null);
            Contract.Requires(lookup != null);
            Contract.Requires(settings != null);

            this.lookup   = lookup;
            this.settings = settings;
        }
Exemplo n.º 6
0
        bool Lookup(MethodInfo action, ODataQueryOptionSettings settings, out IODataQueryOptionsConvention?convention)
        {
            if (ActionBuilders.TryGetValue(action, out var builder))
            {
                convention = builder !.Build(settings);
                return(true);
            }

            convention = default;
            return(false);
        }
Exemplo n.º 7
0
        static IODataQueryOptionsConvention ImplicitActionConvention(ODataQueryOptionSettings settings)
        {
            var validationSettings = new ODataValidationSettings()
            {
                AllowedArithmeticOperators = AllowedArithmeticOperators.None,
                AllowedFunctions           = AllowedFunctions.None,
                AllowedLogicalOperators    = AllowedLogicalOperators.None,
                AllowedQueryOptions        = AllowedQueryOptions.None,
            };

            return(new ODataValidationSettingsConvention(validationSettings, settings));
        }
Exemplo n.º 8
0
        static IODataQueryOptionsConvention ImplicitActionConvention(ODataQueryOptionSettings settings)
        {
            Contract.Requires(settings != null);
            Contract.Ensures(Contract.Result <IODataQueryOptionsConvention>() != null);

            var validationSettings = new ODataValidationSettings()
            {
                AllowedArithmeticOperators = AllowedArithmeticOperators.None,
                AllowedFunctions           = AllowedFunctions.None,
                AllowedLogicalOperators    = AllowedLogicalOperators.None,
                AllowedQueryOptions        = AllowedQueryOptions.None,
            };

            return(new ODataValidationSettingsConvention(validationSettings, settings));
        }
        /// <summary>
        /// Applies the defined OData query option conventions to the specified API description.
        /// </summary>
        /// <param name="apiDescriptions">The <see cref="IEnumerable{T}">sequence</see> of <see cref="ApiDescription">API descriptions</see>
        /// to apply configured conventions to.</param>
        /// <param name="queryOptionSettings">The <see cref="ODataQueryOptionSettings">settings</see> used to apply OData query option conventions.</param>
        public virtual void ApplyTo(IEnumerable <ApiDescription> apiDescriptions, ODataQueryOptionSettings queryOptionSettings)
        {
            if (apiDescriptions == null)
            {
                throw new ArgumentNullException(nameof(apiDescriptions));
            }

            var conventions = new Dictionary <TypeInfo, IODataQueryOptionsConvention>();

            foreach (var description in apiDescriptions)
            {
                var controller = GetController(description);

                if (!conventions.TryGetValue(controller, out var convention))
                {
                    if (!controller.IsODataController() && !IsODataLike(description))
                    {
                        continue;
                    }

                    if (!ConventionBuilders.TryGetValue(controller, out var builder))
                    {
                        builder = new ODataControllerQueryOptionsConventionBuilder(controller);
                    }

                    convention = builder.Build(queryOptionSettings);
                    conventions.Add(controller, convention);
                }

                convention.ApplyTo(description);

                for (var i = 0; i < Conventions.Count; i++)
                {
                    Conventions[i].ApplyTo(description);
                }
            }
        }
Exemplo n.º 10
0
 /// <inheritdoc />
 public virtual IODataQueryOptionsConvention Build(ODataQueryOptionSettings settings) =>
 new ODataControllerQueryOptionConvention(Lookup, settings);
Exemplo n.º 11
0
 /// <inheritdoc />
 public virtual IODataQueryOptionsConvention Build(ODataQueryOptionSettings settings) =>
 new ODataValidationSettingsConvention(ValidationSettings, settings);
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ODataValidationSettingsConvention"/> class.
 /// </summary>
 /// <param name="validationSettings">The <see cref="ODataValidationSettings">validation settings</see> the convention is based on.</param>
 /// <param name="settings">The <see cref="ODataQueryOptionSettings">settings</see> used by the convention.</param>
 public ODataValidationSettingsConvention(ODataValidationSettings validationSettings, ODataQueryOptionSettings settings)
 {
     ValidationSettings = validationSettings;
     Settings           = settings;
 }