public static ApiDescription Create(
            MethodInfo methodInfo,
            string groupName,
            string httpMethod,
            string relativePath,
            IEnumerable <ApiParameterDescription> parameterDescriptions = null,
            IEnumerable <ApiRequestFormat> supportedRequestFormats      = null,
            IEnumerable <ApiResponseType> supportedResponseTypes        = null)
        {
            var actionDescriptor = CreateActionDescriptor(methodInfo);

            var apiDescription = new ApiDescription
            {
                ActionDescriptor = actionDescriptor,
                GroupName        = groupName,
                HttpMethod       = httpMethod,
                RelativePath     = relativePath,
            };

            if (parameterDescriptions != null)
            {
                foreach (var parameter in parameterDescriptions)
                {
                    // If the provided action has a matching parameter - use it to assign ParameterDescriptor & ModelMetadata
                    var controllerParameterDescriptor = actionDescriptor.Parameters
                                                        .OfType <ControllerParameterDescriptor>()
                                                        .FirstOrDefault(parameterDescriptor => parameterDescriptor.Name == parameter.Name);

                    if (controllerParameterDescriptor != null)
                    {
                        parameter.ParameterDescriptor = controllerParameterDescriptor;
                        parameter.ModelMetadata       = ModelMetadataFactory.CreateForParameter(controllerParameterDescriptor.ParameterInfo);
                    }

                    apiDescription.ParameterDescriptions.Add(parameter);
                }
            }

            if (supportedRequestFormats != null)
            {
                foreach (var requestFormat in supportedRequestFormats)
                {
                    apiDescription.SupportedRequestFormats.Add(requestFormat);
                }
            }

            if (supportedResponseTypes != null)
            {
                foreach (var responseType in supportedResponseTypes)
                {
                    // If the provided action has a return value AND the response status is 2XX - use it to assign ModelMetadata
                    if (methodInfo.ReturnType != null && responseType.StatusCode / 100 == 2)
                    {
                        responseType.ModelMetadata = ModelMetadataFactory.CreateForType(methodInfo.ReturnType);
                    }

                    apiDescription.SupportedResponseTypes.Add(responseType);
                }
            }

            return(apiDescription);
        }
Esempio n. 2
0
        public static ApiDescription Create(
            ActionDescriptor actionDescriptor,
            MethodInfo methodInfo,
            string groupName    = "v1",
            string httpMethod   = "POST",
            string relativePath = "resoure",
            IEnumerable <ApiParameterDescription> parameterDescriptions = null,
            IEnumerable <ApiRequestFormat> supportedRequestFormats      = null,
            IEnumerable <ApiResponseType> supportedResponseTypes        = null)
        {
            var apiDescription = new ApiDescription
            {
                ActionDescriptor = actionDescriptor,
                GroupName        = groupName,
                HttpMethod       = httpMethod,
                RelativePath     = relativePath,
            };

            if (parameterDescriptions != null)
            {
                foreach (var parameter in parameterDescriptions)
                {
                    // If the provided action has a matching parameter - use it to assign ParameterDescriptor & ModelMetadata
                    parameter.ParameterDescriptor = actionDescriptor.Parameters
                                                    .OfType <ParameterDescriptor>()
                                                    .Where(parameterDescriptor => parameterDescriptor.Name == parameter.Name)
                                                    .FirstOrDefault();

                    var parameterDescriptorWithParameterInfo = parameter.ParameterDescriptor as
#if NETCOREAPP2_2_OR_GREATER
                                                               Microsoft.AspNetCore.Mvc.Infrastructure.IParameterInfoParameterDescriptor;
#else
                                                               ControllerParameterDescriptor;
#endif

                    if (parameterDescriptorWithParameterInfo != null)
                    {
                        parameter.ModelMetadata = ModelMetadataFactory.CreateForParameter(parameterDescriptorWithParameterInfo.ParameterInfo);
                    }

                    apiDescription.ParameterDescriptions.Add(parameter);
                }
            }

            if (supportedRequestFormats != null)
            {
                foreach (var requestFormat in supportedRequestFormats)
                {
                    apiDescription.SupportedRequestFormats.Add(requestFormat);
                }
            }

            if (supportedResponseTypes != null)
            {
                foreach (var responseType in supportedResponseTypes)
                {
                    // If the provided action has a return value AND the response status is 2XX - use it to assign ModelMetadata
                    if (methodInfo.ReturnType != null && responseType.StatusCode / 100 == 2)
                    {
                        responseType.ModelMetadata = ModelMetadataFactory.CreateForType(methodInfo.ReturnType);
                    }

                    apiDescription.SupportedResponseTypes.Add(responseType);
                }
            }

            return(apiDescription);
        }