/// <summary>
        /// Defines a filter for DefaultResponseType that the controller action should have an attribute of type <typeparamref name="TAttribute"/> in order to be added to the api description.
        /// </summary>
        /// <typeparam name="TAttribute">Type of the attribute, with a constraint on type Attribute</typeparam>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <returns>The original default response type with an added filter.</returns>
        public static DefaultResponseType WhenHasAttribute <TAttribute>(this DefaultResponseType defaultResponseType)
            where TAttribute : Attribute
        {
            defaultResponseType.When(apiDesc => apiDesc.ActionDescriptor.HasCustomControllerActionAttribute <TAttribute>());

            return(defaultResponseType);
        }
        /// <summary>
        /// Specifies that the default response type produces a specific mediatype and optional outputFormatter.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <param name="mediaType">The media type the api description produces for this specific response type.</param>
        /// <param name="outputFormatter">The output formatter to be used for this specific response type.</param>
        /// <returns>The original default response type with an added mediatype and outputformatter combination.</returns>
        public static DefaultResponseType Produces(this DefaultResponseType defaultResponseType, string mediaType, IOutputFormatter outputFormatter = null)
        {
            defaultResponseType.ResponseType.ApiResponseFormats.Add(new ApiResponseFormat()
            {
                MediaType = mediaType,
                Formatter = outputFormatter
            });

            return(defaultResponseType);
        }
Esempio n. 3
0
        public DefaultResponseType AddDefaultResponseType(ApiResponseType responseType)
        {
            var defaultResponseType = new DefaultResponseType()
            {
                ResponseType = responseType
            };

            DefaultResponseTypes.Add(defaultResponseType);

            return(defaultResponseType);
        }
        /// <summary>
        /// Defines a filter for DefaultResponseType that a route parameter has to be present in order to be added to the api description.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <param name="paramNameEquals">The name of the route parameter should equal the provided value. Comparison is case insensitive. Cannot be used in combination with <paramref name="paramNameContains"/>.</param>
        /// <param name="paramNameContains">The name of the route parameter should contain the provided value. Comparison is case insensitive. Cannot be used in combination with <paramref name="paramNameEquals"/>.</param>
        /// <returns>The original default response type with an added filter.</returns>
        /// <exception cref="ArgumentException">Thrown when both the <paramref name="paramNameEquals"/> and <paramref name="paramNameContains"/> are provided.</exception>
        public static DefaultResponseType WhenHasRouteParameter(this DefaultResponseType defaultResponseType, string paramNameEquals = null, string paramNameContains = null)
        {
            if (paramNameContains != null && paramNameEquals != null)
            {
                throw new ArgumentException($"Parameters {nameof(paramNameContains)} and {nameof(paramNameEquals)} cannot be specified both.");
            }

            if (paramNameEquals != null)
            {
                defaultResponseType.When(apiDesc => apiDesc.ParameterDescriptions.Any(x => x.Source.Id == "Path" && x.Name.Equals(paramNameEquals, StringComparison.OrdinalIgnoreCase)));
            }
            else if (paramNameContains != null)
            {
                defaultResponseType.When(apiDesc => apiDesc.ParameterDescriptions.Any(x => x.Source.Id == "Path" && x.Name.Contains(paramNameEquals, StringComparison.OrdinalIgnoreCase)));
            }
            else
            {
                defaultResponseType.When(apiDesc => apiDesc.ParameterDescriptions.Any(x => x.Source.Id == "Path"));
            }

            return(defaultResponseType);
        }
        /// <summary>
        /// Defines a filter for DefaultResponseType that the api method should equal the provided value in order to be added to the api description.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type</param>
        /// <param name="httpMethod">The HTTP method should equal the provided value.</param>
        /// <returns>The original default response type with an added filter.</returns>
        public static DefaultResponseType ToMethod(this DefaultResponseType defaultResponseType, string httpMethod)
        {
            defaultResponseType.When(apiDesc => apiDesc.HttpMethod.Equals(httpMethod, StringComparison.OrdinalIgnoreCase));

            return(defaultResponseType);
        }
        /// <summary>
        /// Defines a filter for DefaultResponseType that the api method should equal DELETE in order to be added to the api description.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <returns>The original default response type with an added filter.</returns>
        public static DefaultResponseType ToDelete(this DefaultResponseType defaultResponseType)
        {
            defaultResponseType.ToMethod("delete");

            return(defaultResponseType);
        }
        /// <summary>
        /// Defines a filter for DefaultResponseType that the api method should equal PUT in order to be added to the api description.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <returns>The original default response type with an added filter.</returns>
        public static DefaultResponseType ToPut(this DefaultResponseType defaultResponseType)
        {
            defaultResponseType.ToMethod("put");

            return(defaultResponseType);
        }
        /// <summary>
        /// Specifies that the default response type should be added to the api description, even if that description does not have the ProducesDefaultResponseTypes attribute.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <returns>The original default response type with the property AddToDescriptionsWithoutDefaultAttribute set to true.</returns>
        public static DefaultResponseType EvenWithoutDefaultAttribute(this DefaultResponseType defaultResponseType)
        {
            defaultResponseType.AddToDescriptionsWithoutDefaultAttribute = true;

            return(defaultResponseType);
        }
        /// <summary>
        /// Defines a filter with a specific delegate function. If the <paramref name="filter"/> function returns true, the response type will be added to the api description.
        /// </summary>
        /// <param name="defaultResponseType">The original default response type.</param>
        /// <param name="filter">The specific delegate function that has to return true in order to be added to the api description.</param>
        /// <returns>The original default response type with an added filter.</returns>
        public static DefaultResponseType When(this DefaultResponseType defaultResponseType, Func <ApiDescription, bool> filter)
        {
            defaultResponseType.Filters.Add(filter);

            return(defaultResponseType);
        }