public void add_parameter_should_make_parameters_optional_after_first_parameter()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            }.Object;
            var description = new ApiDescription()
            {
                ActionDescriptor = action
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            action.Configuration = configuration;

            // act
            context.AddParameter("api-version", Query);
            context.AddParameter("api-version", Header);

            // assert
            description.ParameterDescriptions[0].ParameterDescriptor.IsOptional.Should().BeFalse();
            description.ParameterDescriptions[1].ParameterDescriptor.IsOptional.Should().BeTrue();
        }
        public void add_parameter_should_add_descriptor_for_media_type_parameter()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var json          = new JsonMediaTypeFormatter();

            configuration.Formatters.Clear();
            configuration.Formatters.Add(json);

            var description = new ApiDescription()
            {
                SupportedResponseFormatters = { json }
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            // act
            context.AddParameter("v", MediaTypeParameter);

            // assert
            var formatter = description.SupportedResponseFormatters.Single();

            foreach (var mediaType in formatter.SupportedMediaTypes)
            {
                mediaType.Parameters.Single().Should().Be(new NameValueHeaderValue("v", "1.0"));
            }

            formatter.Should().NotBeSameAs(json);
        }
Esempio n. 3
0
        public void add_parameter_should_not_add_query_parameter_after_path_parameter_has_been_added()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = NewActionDescriptor();
            var route         = new HttpRoute()
            {
                Constraints = { ["api-version"] = new ApiVersionRouteConstraint() }
            };
            var description = new ApiDescription()
            {
                ActionDescriptor = action,
                Route            = route,
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            action.Configuration = configuration;
            description.ParameterDescriptions.Add(new ApiParameterDescription()
            {
                Name = "api-version", Source = FromUri
            });

            // act
            context.AddParameter("api-version", Path);
            context.AddParameter("api-version", Query);

            // assert
            description.ParameterDescriptions.Should().HaveCount(1);
        }
        public void add_parameter_should_not_add_query_parameter_after_path_parameter_has_been_added()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            }.Object;
            var description = new ApiDescription()
            {
                ActionDescriptor = action
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            action.Configuration = configuration;
            description.ParameterDescriptions.Add(new ApiParameterDescription()
            {
                Name = "api-version", Source = FromUri
            });

            // act
            context.AddParameter("api-version", Path);
            context.AddParameter("api-version", Query);

            // assert
            description.ParameterDescriptions.Should().HaveCount(1);
        }
        public void add_parameter_should_remove_other_descriptors_after_path_parameter_is_added()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = NewActionDescriptor();
            var description   = new ApiDescription()
            {
                ActionDescriptor = action
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            action.Configuration = configuration;
            description.ParameterDescriptions.Add(new ApiParameterDescription()
            {
                Name = "api-version", Source = FromUri
            });

            // act
            context.AddParameter("api-version", Query);
            context.AddParameter("api-version", Path);

            // assert
            description.ParameterDescriptions.Should().HaveCount(1);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VersionedApiExplorer"/> class.
        /// </summary>
        /// <param name="configuration">The current <see cref="HttpConfiguration">HTTP configuration</see>.</param>
        /// <param name="options">The associated <see cref="ApiExplorerOptions">API explorer options</see>.</param>
        public VersionedApiExplorer(HttpConfiguration configuration, ApiExplorerOptions options)
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNull(options, nameof(options));

            Configuration   = configuration;
            Options         = options;
            apiDescriptions = new Lazy <ApiDescriptionGroupCollection>(InitializeApiDescriptions);
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ApiVersionParameterDescriptionContext"/> class.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription"/> to provide API version parameter descriptions for.</param>
        /// <param name="apiVersion">The current API version.</param>
        /// <param name="options">The configured <see cref="ApiExplorerOptions">API explorer options</see>.</param>
        public ApiVersionParameterDescriptionContext(
            ApiDescription apiDescription,
            ApiVersion apiVersion,
            ApiExplorerOptions options)
        {
            Arg.NotNull(apiDescription, nameof(apiDescription));
            Arg.NotNull(apiVersion, nameof(apiVersion));
            Arg.NotNull(options, nameof(options));

            ApiDescription = apiDescription;
            ApiVersion     = apiVersion;
            Options        = options;
            optional       = options.AssumeDefaultVersionWhenUnspecified && apiVersion == options.DefaultApiVersion;
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ApiVersionParameterDescriptionContext"/> class.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription"/> to provide API version parameter descriptions for.</param>
        /// <param name="apiVersion">The current API version.</param>
        /// <param name="options">The configured <see cref="ApiExplorerOptions">API explorer options</see>.</param>
        public ApiVersionParameterDescriptionContext(
            ApiDescription apiDescription,
            ApiVersion apiVersion,
            ApiExplorerOptions options)
        {
            Arg.NotNull(apiDescription, nameof(apiDescription));
            Arg.NotNull(apiVersion, nameof(apiVersion));
            Arg.NotNull(options, nameof(options));

            ApiDescription = apiDescription;
            ApiVersion     = apiVersion;
            Options        = options;
            optional       = options.AssumeDefaultVersionWhenUnspecified && apiVersion == options.DefaultApiVersion;
            versionNeutral = new Lazy <bool>(() => ApiDescription.ActionDescriptor.GetApiVersionModel().IsApiVersionNeutral);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ApiVersionParameterDescriptionContext"/> class.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription"/> to provide API version parameter descriptions for.</param>
        /// <param name="apiVersion">The current API version.</param>
        /// <param name="options">The configured <see cref="ApiExplorerOptions">API explorer options</see>.</param>
        public ApiVersionParameterDescriptionContext(
            ApiDescription apiDescription,
            ApiVersion apiVersion,
            ApiExplorerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ApiDescription = apiDescription;
            ApiVersion     = apiVersion;
            Options        = options;
            optional       = options.AssumeDefaultVersionWhenUnspecified && apiVersion == options.DefaultApiVersion;
            versionNeutral = new Lazy <bool>(() => ApiDescription.ActionDescriptor.GetApiVersionModel().IsApiVersionNeutral);
        }
Esempio n. 10
0
        public void add_parameter_should_add_descriptor_for_media_type_parameter()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            }.Object;
            var json           = new JsonMediaTypeFormatter();
            var formUrlEncoded = new FormUrlEncodedMediaTypeFormatter();

            configuration.Formatters.Clear();
            configuration.Formatters.Add(json);
            configuration.Formatters.Add(formUrlEncoded);
            action.Configuration = configuration;

            var description = new ApiDescription()
            {
                ActionDescriptor = action,
                SupportedRequestBodyFormatters = { json, formUrlEncoded }
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            // act
            context.AddParameter("v", MediaTypeParameter);

            // assert
            var formatter = description.SupportedRequestBodyFormatters[0];

            foreach (var mediaType in formatter.SupportedMediaTypes)
            {
                mediaType.Parameters.Single().Should().Be(new NameValueHeaderValue("v", "1.0"));
            }

            formatter.Should().NotBeSameAs(json);
            formatter = description.SupportedRequestBodyFormatters[1];

            foreach (var mediaType in formatter.SupportedMediaTypes)
            {
                mediaType.Parameters.Single().Should().Be(new NameValueHeaderValue("v", "1.0"));
            }

            formatter.Should().NotBeSameAs(formUrlEncoded);
        }
Esempio n. 11
0
        public void add_parameter_should_add_descriptor_for_path()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = NewActionDescriptor();
            var route         = new HttpRoute()
            {
                Constraints = { ["api-version"] = new ApiVersionRouteConstraint() }
            };
            var description = new ApiDescription()
            {
                ActionDescriptor = action,
                Route            = route,
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            action.Configuration = configuration;
            description.ParameterDescriptions.Add(new ApiParameterDescription()
            {
                Name = "api-version", Source = FromUri
            });

            // act
            context.AddParameter("api-version", Path);

            // assert
            description.ParameterDescriptions.Single().Should().BeEquivalentTo(
                new
            {
                Name                = "api-version",
                Documentation       = options.DefaultApiVersionParameterDescription,
                Source              = FromUri,
                ParameterDescriptor = new
                {
                    ParameterName    = "api-version",
                    DefaultValue     = "1.0",
                    IsOptional       = false,
                    Configuration    = configuration,
                    ActionDescriptor = action
                }
            },
                o => o.ExcludingMissingMembers());
        }
        public void add_parameter_should_add_optional_parameter_when_allowed()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            }.Object;
            var description = new ApiDescription()
            {
                ActionDescriptor = action
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);

            action.Configuration = configuration;
            configuration.AddApiVersioning(o => o.AssumeDefaultVersionWhenUnspecified = true);

            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            // act
            context.AddParameter("api-version", Query);

            // assert
            description.ParameterDescriptions.Single().ShouldBeEquivalentTo(
                new
            {
                Name                = "api-version",
                Documentation       = options.DefaultApiVersionParameterDescription,
                Source              = FromUri,
                ParameterDescriptor = new
                {
                    ParameterName    = "api-version",
                    DefaultValue     = "1.0",
                    IsOptional       = true,
                    Configuration    = configuration,
                    ActionDescriptor = action
                }
            },
                o => o.ExcludingMissingMembers());
        }
        public void add_parameter_should_add_descriptor_for_header()
        {
            // arrange
            var configuration = new HttpConfiguration();
            var action        = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            }.Object;
            var description = new ApiDescription()
            {
                ActionDescriptor = action
            };
            var version = new ApiVersion(1, 0);
            var options = new ApiExplorerOptions(configuration);
            var context = new ApiVersionParameterDescriptionContext(description, version, options);

            action.Configuration = configuration;

            // act
            context.AddParameter("api-version", Header);

            // assert
            description.ParameterDescriptions.Single().ShouldBeEquivalentTo(
                new
            {
                Name                = "api-version",
                Documentation       = options.DefaultApiVersionParameterDescription,
                Source              = Unknown,
                ParameterDescriptor = new
                {
                    ParameterName    = "api-version",
                    DefaultValue     = "1.0",
                    IsOptional       = false,
                    Configuration    = configuration,
                    ActionDescriptor = action
                }
            },
                o => o.ExcludingMissingMembers());
        }
Esempio n. 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VersionedApiExplorer"/> class.
 /// </summary>
 /// <param name="configuration">The current <see cref="HttpConfiguration">HTTP configuration</see>.</param>
 /// <param name="options">The associated <see cref="ApiExplorerOptions">API explorer options</see>.</param>
 public VersionedApiExplorer(HttpConfiguration configuration, ApiExplorerOptions options)
 {
     Configuration   = configuration;
     Options         = options;
     apiDescriptions = new Lazy <ApiDescriptionGroupCollection>(InitializeApiDescriptions);
 }