/// <summary>
        /// Applies a controller convention given the specified builder and model.
        /// </summary>
        /// <param name="controller">The <see cref="IControllerConventionBuilder">builder</see> used to apply conventions.</param>
        /// <param name="controllerModel">The <see cref="ControllerModel">model</see> to build conventions from.</param>
        /// <returns>True if any conventions were applied to the <paramref name="controllerModel">controller model</paramref>;
        /// otherwise, false.</returns>
        public virtual bool Apply(IControllerConventionBuilder controller, ControllerModel controllerModel)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (controllerModel == null)
            {
                throw new ArgumentNullException(nameof(controllerModel));
            }

            if (GetApiVersion(controllerModel.ControllerType.Namespace !) is not ApiVersion apiVersion)
            {
                return(false);
            }

            var deprecated = controllerModel.Attributes.OfType <ObsoleteAttribute>().Any();

            if (deprecated)
            {
                controller.HasDeprecatedApiVersion(apiVersion);
            }
            else
            {
                controller.HasApiVersion(apiVersion);
            }

            return(true);
        }
        /// <summary>
        /// Applies a controller convention given the specified builder and model.
        /// </summary>
        /// <param name="controller">The <see cref="IControllerConventionBuilder">builder</see> used to apply conventions.</param>
        /// <param name="controllerDescriptor">The <see cref="HttpControllerDescriptor">descriptor</see> to build conventions from.</param>
        /// <returns>True if any conventions were applied to the <paramref name="controllerDescriptor">descriptor</paramref>;
        /// otherwise, false.</returns>
        public virtual bool Apply(IControllerConventionBuilder controller, HttpControllerDescriptor controllerDescriptor)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (controllerDescriptor == null)
            {
                throw new ArgumentNullException(nameof(controllerDescriptor));
            }

            if (GetApiVersion(controllerDescriptor.ControllerType.Namespace) is not ApiVersion apiVersion)
            {
                return(false);
            }

            var deprecated = controllerDescriptor.GetCustomAttributes <ObsoleteAttribute>().Any();

            if (deprecated)
            {
                controller.HasDeprecatedApiVersion(apiVersion);
            }
            else
            {
                controller.HasApiVersion(apiVersion);
            }

            return(true);
        }
        /// <summary>
        /// Applies a controller convention given the specified builder and model.
        /// </summary>
        /// <param name="controller">The <see cref="IControllerConventionBuilder">builder</see> used to apply conventions.</param>
        /// <param name="controllerDescriptor">The <see cref="HttpControllerDescriptor">descriptor</see> to build conventions from.</param>
        /// <returns>True if any conventions were applied to the <paramref name="controllerDescriptor">descriptor</paramref>;
        /// otherwise, false.</returns>
        public virtual bool Apply(IControllerConventionBuilder controller, HttpControllerDescriptor controllerDescriptor)
        {
            Arg.NotNull(controller, nameof(controller));
            Arg.NotNull(controllerDescriptor, nameof(controller));

            var text = GetRawApiVersion(controllerDescriptor.ControllerType.Namespace);

            if (!ApiVersion.TryParse(text, out var apiVersion))
            {
                return(false);
            }

            var deprecated = controllerDescriptor.GetCustomAttributes <ObsoleteAttribute>().Any();

            if (deprecated)
            {
                controller.HasDeprecatedApiVersion(apiVersion);
            }
            else
            {
                controller.HasApiVersion(apiVersion);
            }

            return(true);
        }
        public bool Apply(IControllerConventionBuilder controller, ControllerModel controllerModel)
        {
            if (controller.ControllerType.BaseType == typeof(BaseODataController))
            {
                controller.HasApiVersion(1, 0);
                return(true);
            }

            return(false);
        }
        private void SetActionApiVersions(ActionModel actionModel, IControllerConventionBuilder controller, ApiVersion introducedVersion, ApiVersion removedVersion)
        {
            var actionSupportedVersions  = _allVersions.GetSupportedVersions(introducedVersion, removedVersion);
            var actionDeprecatedVersions = _allVersions.GetDeprecatedVersions(introducedVersion, removedVersion);

            var action = controller.Action(actionModel.ActionMethod);

            action.HasApiVersions(actionSupportedVersions);
            action.HasDeprecatedApiVersions(actionDeprecatedVersions);
        }
示例#6
0
        /// <summary>
        /// Gets or creates the convention builder for the specified controller action method.
        /// </summary>
        /// <param name="builder">The extended <see cref="IActionConventionBuilder"/>.</param>
        /// <param name="methodName">The name of the action method.</param>
        /// <param name="argumentTypes">The optional array of action method argument types.</param>
        /// <returns>A new or existing <see cref="ActionApiVersionConventionBuilder"/>.</returns>
        /// <remarks>The specified <paramref name="methodName">method name</paramref> must refer to a public, non-static action method.
        /// If there is only one corresponding match found, then the <paramref name="argumentTypes">argument types</paramref> are ignored;
        /// otherwise, the <paramref name="argumentTypes">argument types</paramref> are used for method overload resolution. Action
        /// methods that have the <see cref="NonActionAttribute"/> applied will also be ignored.</remarks>
        public static IActionConventionBuilder Action(this IControllerConventionBuilder builder, string methodName, params Type[] argumentTypes)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var method = ActionMethodResolver.Resolve(builder.ControllerType, methodName, argumentTypes);

            return(builder.Action(method));
        }
        private void SetControllerApiVersions(IControllerConventionBuilder controller,
                                              ApiVersion introducedVersion, ApiVersion removedVersion)
        {
            var supportedVersions =
                _allVersions.GetSupportedVersions(introducedVersion, removedVersion);

            var deprecatedVersions =
                _allVersions.GetDeprecatedVersions(introducedVersion, removedVersion);

            controller.HasApiVersions(supportedVersions);
            controller.HasDeprecatedApiVersions(deprecatedVersions);
        }
示例#8
0
        public virtual bool Apply(IControllerConventionBuilder controller, ControllerModel controllerModel)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (controllerModel == null)
            {
                throw new ArgumentNullException(nameof(controllerModel));
            }

            var match = Regex.Match(controllerModel.ControllerType.Namespace !, @$ "(?:(?:^{prefix}|\.{prefix})\.([A-Za-z]+)\.)?[vV](\d+)[\.$]?", RegexOptions.Singleline);
        private void SetActionApiVersions(ControllerModel controllerModel, ApiVersion controllerIntroducedInVersion,
                                          ApiVersion controllerRemovedAsOfVersion, IControllerConventionBuilder controller)
        {
            foreach (var actionModel in controllerModel.Actions)
            {
                var actionModelIntroduced = actionModel.GetIntroducedVersion();
                ValidateActionModel(controllerModel, controllerIntroducedInVersion, actionModelIntroduced, actionModel);

                var actionIntroducedVersion = actionModelIntroduced ?? controllerIntroducedInVersion;
                var actionRemovedVersion    = actionModel.GetRemovedVersion() ?? controllerRemovedAsOfVersion;

                SetActionApiVersions(actionModel, controller, actionIntroducedVersion, actionRemovedVersion);
            }
        }
            public StateHolder(IControllerConventionBuilder controller, HttpControllerDescriptor descriptor)
            {
                Controller           = controller;
                ControllerDescriptor = descriptor;

                // apply lazy loading to properties so that they are loaded and cached when needed
                // instead of right a way.
                _controllerIntroduced = new Lazy <IntroducedInApiVersionAttribute>(
                    () => ControllerDescriptor.GetCustomAttributes <IntroducedInApiVersionAttribute>().SingleOrDefault());
                _controllerRemoved = new Lazy <RemovedInApiVersionAttribute>(
                    () => ControllerDescriptor.GetCustomAttributes <RemovedInApiVersionAttribute>().SingleOrDefault());
                _actionDescriptors = new Lazy <HttpActionDescriptor[]>(
                    () => new ApiControllerActionSelector().GetActionMapping(descriptor)?.SelectMany(x => x).ToArray() ?? Array.Empty <HttpActionDescriptor>());
            }
        public bool Apply(IControllerConventionBuilder controller, HttpControllerDescriptor controllerDescriptor)
        {
            var state = new StateHolder(controller, controllerDescriptor);

            if (!state.IsValid)
            {
                return(false);
            }

            ApplyControllerVersions(state);
            ApplyControllerActionVersions(state);

            return(true);
        }
示例#12
0
        /// <summary>
        /// Gets or creates the convention builder for the specified controller action method.
        /// </summary>
        /// <typeparam name="TController">The type of controller.</typeparam>
        /// <param name="builder">The extended <see cref="IControllerConventionBuilder{T}"/>.</param>
        /// <param name="actionExpression">The <see cref="Expression{TDelegate}">expression</see> representing the controller action method.</param>
        /// <returns>A new or existing <see cref="ActionApiVersionConventionBuilder{T}"/>.</returns>
        public static IActionConventionBuilder <TController> Action <TController>(this IControllerConventionBuilder <TController> builder, Expression <Action <TController> > actionExpression)
            where TController : notnull
#if WEBAPI
#pragma warning disable SA1001 // Commas should be spaced correctly
        , IHttpController
#pragma warning restore SA1001 // Commas should be spaced correctly
#endif
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (actionExpression == null)
            {
                throw new ArgumentNullException(nameof(actionExpression));
            }

            return(builder.Action(actionExpression.ExtractMethod()));
        }