/// <summary>
        /// Attempts to update the relate path of the specified API description and remove the corresponding parameter according to the specified options.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to attempt to update.</param>
        /// <param name="options">The current <see cref="ApiExplorerOptions">API Explorer options</see>.</param>
        /// <returns>True if the <paramref name="apiDescription">API description</paramref> was updated; otherwise, false.</returns>
        public static bool TryUpdateRelativePathAndRemoveApiVersionParameter(this ApiDescription apiDescription, ApiExplorerOptions options)
        {
            Arg.NotNull(apiDescription, nameof(apiDescription));
            Arg.NotNull(options, nameof(options));

            if (!options.SubstituteApiVersionInUrl)
            {
                return(false);
            }

            var parameter = apiDescription.ParameterDescriptions.FirstOrDefault(pd => pd.Source == Path && pd.ModelMetadata?.DataTypeName == nameof(ApiVersion));

            if (parameter == null)
            {
                return(false);
            }

            var relativePath    = apiDescription.RelativePath;
            var token           = '{' + parameter.Name + '}';
            var value           = apiDescription.GetApiVersion().ToString(options.SubstitutionFormat, InvariantCulture);
            var newRelativePath = relativePath.Replace(token, value);

            if (relativePath == newRelativePath)
            {
                return(false);
            }

            apiDescription.RelativePath = newRelativePath;
            apiDescription.ParameterDescriptions.Remove(parameter);
            return(true);
        }
        /// <summary>
        /// Gets a value indicating whether the associated API description is deprecated.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to evaluate.</param>
        /// <returns><c>True</c> if the <see cref="ApiDescription">API description</see> is deprecated; otherwise, <c>false</c>.</returns>
        public static bool IsDeprecated(this ApiDescription apiDescription)
        {
            Arg.NotNull(apiDescription, nameof(apiDescription));

            var apiVersion = apiDescription.GetApiVersion();
            var model      = apiDescription.ActionDescriptor.GetApiVersionModel(Explicit | Implicit);

            return(model.DeprecatedApiVersions.Contains(apiVersion));
        }
        /// <summary>
        /// Gets a value indicating whether the associated API description is deprecated.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to evaluate.</param>
        /// <returns><c>True</c> if the <see cref="ApiDescription">API description</see> is deprecated; otherwise, <c>false</c>.</returns>
        public static bool IsDeprecated(this ApiDescription apiDescription)
        {
            if (apiDescription == null)
            {
                throw new ArgumentNullException(nameof(apiDescription));
            }

            var apiVersion = apiDescription.GetApiVersion();
            var model      = apiDescription.ActionDescriptor.GetApiVersionModel(Explicit | Implicit);

            return(model.DeprecatedApiVersions.Contains(apiVersion));
        }
예제 #4
0
        public void get_api_version_should_associated_value()
        {
            // arrange
            var version     = new ApiVersion(42, 0);
            var description = new ApiDescription();

            description.Properties[typeof(ApiVersion)] = version;

            // act
            var value = description.GetApiVersion();

            // assert
            value.Should().Be(version);
        }
        /// <summary>
        /// Attempts to update the relate path of the specified API description and remove the corresponding parameter according to the specified options.
        /// </summary>
        /// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to attempt to update.</param>
        /// <param name="options">The current <see cref="ApiExplorerOptions">API Explorer options</see>.</param>
        /// <returns>True if the <paramref name="apiDescription">API description</paramref> was updated; otherwise, false.</returns>
        public static bool TryUpdateRelativePathAndRemoveApiVersionParameter(this ApiDescription apiDescription, ApiExplorerOptions options)
        {
            if (apiDescription == null)
            {
                throw new ArgumentNullException(nameof(apiDescription));
            }

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

            if (!options.SubstituteApiVersionInUrl)
            {
                return(false);
            }

            var parameter = apiDescription.ParameterDescriptions.FirstOrDefault(pd => pd.Source == Path && pd.ModelMetadata?.DataTypeName == nameof(ApiVersion));

            if (parameter == null)
            {
                return(false);
            }

            var relativePath = apiDescription.RelativePath;
            var token        = '{' + parameter.Name + '}';
            var value        = apiDescription.GetApiVersion().ToString(options.SubstitutionFormat, InvariantCulture);

#if NETSTANDARD2_0
            var newRelativePath = relativePath.Replace(token, value);
#else
            var newRelativePath = relativePath.Replace(token, value, StringComparison.Ordinal);
#endif

            if (relativePath == newRelativePath)
            {
                return(false);
            }

            apiDescription.RelativePath = newRelativePath;
            apiDescription.ParameterDescriptions.Remove(parameter);
            return(true);
        }
        static void UpdateRelativePathAndRemoveApiVersionParameterIfNecessary(ApiDescription apiDescription, string apiVersionFormat)
        {
            Contract.Requires(apiDescription != null);

            var parameter = apiDescription.ParameterDescriptions.FirstOrDefault(pd => pd.Source == BindingSource.Path && pd.ModelMetadata?.DataTypeName == nameof(ApiVersion));

            if (parameter == null)
            {
                return;
            }

            var relativePath    = apiDescription.RelativePath;
            var token           = '{' + parameter.Name + '}';
            var value           = apiDescription.GetApiVersion().ToString(apiVersionFormat, InvariantCulture);
            var newRelativePath = relativePath.Replace(token, value);

            if (relativePath != newRelativePath)
            {
                apiDescription.RelativePath = newRelativePath;
                apiDescription.ParameterDescriptions.Remove(parameter);
            }
        }