Example #1
0
        public void apply_to_should_assign_model_with_declared_api_versions_from_mapped_convention_and_attributes()
        {
            // arrange
            var controllerBuilder     = new ControllerApiVersionConventionBuilder <DecoratedController>();
            var actionBuilder         = new ActionApiVersionConventionBuilder <DecoratedController>(controllerBuilder);
            var method                = typeof(DecoratedController).GetMethod(nameof(DecoratedController.Get));
            var attributes            = method.GetCustomAttributes().Cast <object>().ToArray();
            var actionModel           = new ActionModel(method, attributes);
            var empty                 = Enumerable.Empty <ApiVersion>();
            var controllerVersionInfo = Tuple.Create(empty, empty, empty, empty);

            actionModel.SetProperty(controllerVersionInfo);
            actionBuilder.MapToApiVersion(new ApiVersion(2, 0))
            .MapToApiVersion(new ApiVersion(3, 0));

            // act
            actionBuilder.ApplyTo(actionModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new[] { new ApiVersion(2, 0), new ApiVersion(3, 0) },
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0],
            });
        }
        public void apply_to_should_assign_empty_conventions_to_api_version_neutral_controller()
        {
            // arrange
            var controllerModel   = new ControllerModel(typeof(UndecoratedController).GetTypeInfo(), new object[0]);
            var controllerBuilder = new ControllerApiVersionConventionBuilder(typeof(UndecoratedController));

            controllerBuilder.HasDeprecatedApiVersion(0, 9)
            .HasApiVersion(2, 0)
            .AdvertisesApiVersion(3, 0)
            .AdvertisesDeprecatedApiVersion(3, 0, "Beta")
            .IsApiVersionNeutral();

            // act
            controllerBuilder.ApplyTo(controllerModel);

            // assert
            controllerModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = true,
                DeclaredApiVersions    = new ApiVersion[0],
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0]
            });
        }
Example #3
0
        public void apply_to_should_assign_empty_model_without_api_versions_from_mapped_convention()
        {
            // arrange
            var controllerBuilder     = new ControllerApiVersionConventionBuilder <UndecoratedController>();
            var actionBuilder         = new ActionApiVersionConventionBuilder <UndecoratedController>(controllerBuilder);
            var method                = typeof(UndecoratedController).GetMethod(nameof(UndecoratedController.Get));
            var actionModel           = new ActionModel(method, new object[0]);
            var empty                 = Enumerable.Empty <ApiVersion>();
            var controllerVersionInfo = Tuple.Create(empty, empty, empty, empty);

            actionModel.SetProperty(controllerVersionInfo);

            // act
            actionBuilder.ApplyTo(actionModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new ApiVersion[0],
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0],
            });
        }
Example #4
0
        public void apply_to_should_assign_model_to_controller_from_conventions_and_attributes()
        {
            // arrange
            var controllerType  = typeof(DecoratedController);
            var action          = controllerType.GetRuntimeMethod(nameof(DecoratedController.Get), Type.EmptyTypes);
            var attributes      = controllerType.GetTypeInfo().GetCustomAttributes().Cast <object>().ToArray();
            var actionModel     = new ActionModel(action, Array.Empty <object>());
            var controllerModel = new ControllerModel(controllerType.GetTypeInfo(), attributes)
            {
                Actions = { actionModel }
            };
            var controllerBuilder = new ControllerApiVersionConventionBuilder(controllerType);

            controllerBuilder.HasApiVersion(1, 0)
            .AdvertisesApiVersion(4, 0);

            // act
            controllerBuilder.ApplyTo(controllerModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = Array.Empty <ApiVersion>(),
                SupportedApiVersions   = new[] { new ApiVersion(1, 0), new ApiVersion(2, 0), new ApiVersion(3, 0), new ApiVersion(4, 0) },
                DeprecatedApiVersions  = new[] { new ApiVersion(0, 9), new ApiVersion(3, 0, "Beta") },
                ImplementedApiVersions = new[] { new ApiVersion(0, 9), new ApiVersion(1, 0), new ApiVersion(2, 0), new ApiVersion(3, 0), new ApiVersion(3, 0, "Beta"), new ApiVersion(4, 0) }
            });
        }
        public void apply_to_should_assign_model_with_declared_api_versions_from_mapped_convention()
        {
            // arrange
            var controllerBuilder     = new ControllerApiVersionConventionBuilder(typeof(UndecoratedController));
            var actionBuilder         = new ActionApiVersionConventionBuilder(controllerBuilder);
            var method                = typeof(UndecoratedController).GetMethod(nameof(UndecoratedController.Get));
            var attributes            = new object[] { new MapToApiVersionAttribute("2.0") };
            var actionModel           = new ActionModel(method, attributes);
            var empty                 = Enumerable.Empty <ApiVersion>();
            var controllerVersionInfo = new ControllerVersionInfo(empty, empty, empty, empty);

            actionModel.SetProperty(controllerVersionInfo);
            actionBuilder.MapToApiVersion(new ApiVersion(2, 0));

            // act
            actionBuilder.ApplyTo(actionModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().ShouldBeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new[] { new ApiVersion(2, 0) },
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0]
            });
        }
Example #6
0
        public void apply_to_should_assign_empty_conventions_to_api_version_neutral_controller()
        {
            // arrange
            var controllerType  = typeof(UndecoratedController);
            var action          = controllerType.GetRuntimeMethod(nameof(UndecoratedController.Get), Type.EmptyTypes);
            var attributes      = Array.Empty <object>();
            var actionModel     = new ActionModel(action, attributes);
            var controllerModel = new ControllerModel(controllerType.GetTypeInfo(), attributes)
            {
                Actions = { actionModel }
            };
            var controllerBuilder = new ControllerApiVersionConventionBuilder(controllerType);

            controllerBuilder.HasDeprecatedApiVersion(0, 9)
            .HasApiVersion(2, 0)
            .AdvertisesApiVersion(3, 0)
            .AdvertisesDeprecatedApiVersion(3, 0, "Beta")
            .IsApiVersionNeutral();

            // act
            controllerBuilder.ApplyTo(controllerModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = true,
                DeclaredApiVersions    = Array.Empty <ApiVersion>(),
                SupportedApiVersions   = Array.Empty <ApiVersion>(),
                DeprecatedApiVersions  = Array.Empty <ApiVersion>(),
                ImplementedApiVersions = Array.Empty <ApiVersion>()
            });
        }
        /// <summary>
        /// Gets or creates the convention builder for the specified controller.
        /// </summary>
        /// <typeparam name="TController">The <see cref="Type">type</see> of controller to build conventions for.</typeparam>
        /// <returns>A new or existing <see cref="ControllerApiVersionConventionBuilder{T}"/>.</returns>
        public virtual ControllerApiVersionConventionBuilder <TController> Controller <TController>()
        {
            Contract.Ensures(Contract.Result <ControllerApiVersionConventionBuilder <TController> >() != null);

            var key = typeof(TController).GetTypeInfo();

            if (!ControllerConventions.TryGetValue(key, out var convention))
            {
                var typedConvention = new ControllerApiVersionConventionBuilder <TController>();
                ControllerConventions[key] = typedConvention;
                return(typedConvention);
            }

            return((ControllerApiVersionConventionBuilder <TController>)convention);
        }
        /// <summary>
        /// Gets or creates the convention builder for the specified controller.
        /// </summary>
        /// <typeparam name="TController">The <see cref="Type">type</see> of controller to build conventions for.</typeparam>
        /// <returns>A new or existing <see cref="ControllerApiVersionConventionBuilder{T}"/>.</returns>
        public virtual ControllerApiVersionConventionBuilder <TController> Controller <TController>()
        {
            Contract.Ensures(Contract.Result <ControllerApiVersionConventionBuilder <TController> >() != null);

            var key = typeof(TController).GetTypeInfo();

            if (!ControllerConventions.TryGetValue(key, out var convention))
            {
                var typedConvention = new ControllerApiVersionConventionBuilder <TController>();
                ControllerConventions[key] = typedConvention;
                return(typedConvention);
            }

            if (convention is ControllerApiVersionConventionBuilder <TController> builder)
            {
                return(builder);
            }

            throw new InvalidOperationException(SR.ConventionStyleMismatch.FormatDefault(key.Name));
        }
Example #9
0
        void ApplyActionConventions(ControllerModel controller)
        {
            Contract.Requires(controller != null);

            if (VersionNeutral)
            {
                controller.SetProperty(ApiVersionModel.Neutral);

                for (var i = 0; i < controller.Actions.Count; i++)
                {
                    var action = controller.Actions[i];

                    action.SetProperty(controller);
                    action.SetProperty(ApiVersionModel.Neutral);
                }

                return;
            }

            controller.SetProperty(new ApiVersionModel(SupportedVersions, DeprecatedVersions, AdvertisedVersions, DeprecatedAdvertisedVersions));

            var anyController = new ControllerApiVersionConventionBuilder(typeof(ControllerBase));

            for (var i = 0; i < controller.Actions.Count; i++)
            {
                var action = controller.Actions[i];
                var key    = action.ActionMethod;

                if (!TryGetConvention(key, out var actionConvention))
                {
                    actionConvention = new ActionApiVersionConventionBuilder(anyController);
                }

                action.SetProperty(controller);
                actionConvention.ApplyTo(action);
            }

            ApplyInheritedActionConventions(controller.Actions);
        }
        public void apply_to_should_assign_model_to_controller_from_conventions_and_attributes()
        {
            // arrange
            var attributes        = typeof(DecoratedController).GetTypeInfo().GetCustomAttributes().Cast <object>().ToArray();
            var controllerModel   = new ControllerModel(typeof(DecoratedController).GetTypeInfo(), attributes);
            var controllerBuilder = new ControllerApiVersionConventionBuilder <DecoratedController>();

            controllerBuilder.HasApiVersion(1, 0)
            .AdvertisesApiVersion(4, 0);

            // act
            controllerBuilder.ApplyTo(controllerModel);

            // assert
            controllerModel.GetProperty <ApiVersionModel>().ShouldBeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new[] { new ApiVersion(0, 9), new ApiVersion(1, 0), new ApiVersion(2, 0) },
                SupportedApiVersions   = new[] { new ApiVersion(1, 0), new ApiVersion(2, 0), new ApiVersion(3, 0), new ApiVersion(4, 0) },
                DeprecatedApiVersions  = new[] { new ApiVersion(0, 9), new ApiVersion(3, 0, "Beta") },
                ImplementedApiVersions = new[] { new ApiVersion(0, 9), new ApiVersion(1, 0), new ApiVersion(2, 0), new ApiVersion(3, 0), new ApiVersion(3, 0, "Beta"), new ApiVersion(4, 0) }
            });
        }