Ejemplo n.º 1
0
        public void GetSwagger_SetsParameterRequired_ForNonControllerActionDescriptor_IfApiParameterDescriptionForBodyIsRequired(bool isRequired)
        {
            void Execute(object obj)
            {
            }

            Action <object> action = Execute;

            var actionDescriptor = new ActionDescriptor
            {
                RouteValues = new Dictionary <string, string>
                {
                    ["controller"] = "Foo",
                }
            };

            var parameter = new ApiParameterDescription
            {
                Name          = "obj",
                Source        = BindingSource.Body,
                IsRequired    = isRequired,
                Type          = typeof(object),
                ModelMetadata = ModelMetadataFactory.CreateForParameter(action.Method.GetParameters()[0])
            };

            var subject = Subject(
                apiDescriptions: new[]
            {
                ApiDescriptionFactory.Create(actionDescriptor, action.Method, groupName: "v1", httpMethod: "POST", relativePath: "resource", parameterDescriptions: new[] { parameter }),
            }
                );

            var document = subject.GetSwagger("v1");

            Assert.Equal(isRequired, document.Paths["/resource"].Operations[OperationType.Post].RequestBody.Required);
        }
Ejemplo n.º 2
0
        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);
        }