예제 #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_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]
            });
        }
예제 #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],
            });
        }
예제 #4
0
        /// <summary>
        /// Applies the builder conventions to the specified controller action.
        /// </summary>
        /// <param name="actionModel">The <see cref="ActionModel">action model</see> to apply the conventions to.</param>
        public void ApplyTo(ActionModel actionModel)
        {
            Arg.NotNull(actionModel, nameof(actionModel));

            mappedVersions.UnionWith(from provider in actionModel.Attributes.OfType <IApiVersionProvider>()
                                     where !provider.AdvertiseOnly && !provider.Deprecated
                                     from version in provider.Versions
                                     select version);

            var controllerControllerVersionInfo = actionModel.GetProperty <ControllerVersionInfo>();
            var versionModel = new ApiVersionModel(
                declaredVersions: mappedVersions,
                supportedVersions: controllerControllerVersionInfo.Item1,
                deprecatedVersions: controllerControllerVersionInfo.Item2,
                advertisedVersions: controllerControllerVersionInfo.Item3,
                deprecatedAdvertisedVersions: controllerControllerVersionInfo.Item4);

            actionModel.SetProperty(versionModel);
        }