Пример #1
0
        private void LoadDefaultReturnType(SwaggerOperation operation, MethodInfo method, string xmlDescription, SwaggerGenerator swaggerGenerator)
        {
            var returnType = method.ReturnType;

            if (returnType == typeof(Task))
            {
                returnType = typeof(void);
            }
            else if (returnType.Name == "Task`1")
            {
                returnType = returnType.GenericTypeArguments[0];
            }

            if (IsVoidResponse(returnType))
            {
                operation.Responses[GetVoidResponseStatusCode()] = new SwaggerResponse
                {
                    Description = xmlDescription ?? string.Empty
                };
            }
            else
            {
                var typeDescription = JsonObjectTypeDescription.FromType(returnType,
                                                                         method.ReturnParameter?.GetCustomAttributes(), Settings.DefaultEnumHandling);
                operation.Responses["200"] = new SwaggerResponse
                {
                    Description   = xmlDescription ?? string.Empty,
                    IsNullableRaw = typeDescription.IsNullable,
                    Schema        = swaggerGenerator.GenerateAndAppendSchemaFromType(returnType, typeDescription.IsNullable, null)
                };
            }
        }
Пример #2
0
        private void LoadReturnType(SwaggerOperation operation, MethodInfo method, SwaggerGenerator swaggerGenerator)
        {
            var xmlDescription = method.ReturnParameter.GetXmlDocumentation();

            if (xmlDescription == string.Empty)
            {
                xmlDescription = null;
            }

            var responseTypeAttributes = method.GetCustomAttributes()
                                         .Where(a => a.GetType().Name == "ResponseTypeAttribute")
                                         .ToList();

            var producesResponseTypeAttributes = method.GetCustomAttributes()
                                                 .Where(a => a.GetType().Name == "ProducesResponseTypeAttribute")
                                                 .ToList();

            if (responseTypeAttributes.Any() || producesResponseTypeAttributes.Any())
            {
                foreach (var responseTypeAttribute in responseTypeAttributes)
                {
                    dynamic dynResultTypeAttribute = responseTypeAttribute;
                    var     returnType             = dynResultTypeAttribute.ResponseType;

                    var httpStatusCode = IsVoidResponse(returnType) ? GetVoidResponseStatusCode() : "200";
                    if (responseTypeAttribute.GetType().GetRuntimeProperty("HttpStatusCode") != null)
                    {
                        httpStatusCode = dynResultTypeAttribute.HttpStatusCode.ToString();
                    }

                    var description = xmlDescription;
                    if (responseTypeAttribute.GetType().GetRuntimeProperty("Description") != null)
                    {
                        if (!string.IsNullOrEmpty(dynResultTypeAttribute.Description))
                        {
                            description = dynResultTypeAttribute.Description;
                        }
                    }

                    var typeDescription = JsonObjectTypeDescription.FromType(returnType, method.ReturnParameter?.GetCustomAttributes(), Settings.DefaultEnumHandling);
                    var response        = new SwaggerResponse
                    {
                        Description = description ?? string.Empty
                    };

                    if (IsVoidResponse(returnType) == false)
                    {
                        response.IsNullableRaw = typeDescription.IsNullable;
                        response.Schema        = swaggerGenerator.GenerateAndAppendSchemaFromType(returnType, typeDescription.IsNullable, null);
                    }

                    operation.Responses[httpStatusCode] = response;
                }

                foreach (dynamic producesResponseTypeAttribute in producesResponseTypeAttributes)
                {
                    var returnType      = producesResponseTypeAttribute.Type;
                    var typeDescription = JsonObjectTypeDescription.FromType(returnType, method.ReturnParameter?.GetCustomAttributes(), Settings.DefaultEnumHandling);

                    var httpStatusCode = producesResponseTypeAttribute.StatusCode.ToString(CultureInfo.InvariantCulture);
                    var response       = new SwaggerResponse
                    {
                        Description = xmlDescription ?? string.Empty
                    };

                    if (IsVoidResponse(returnType) == false)
                    {
                        response.IsNullableRaw = typeDescription.IsNullable;
                        response.Schema        = swaggerGenerator.GenerateAndAppendSchemaFromType(returnType, typeDescription.IsNullable, null);
                    }

                    operation.Responses[httpStatusCode] = response;
                }
            }
            else
            {
                LoadDefaultReturnType(operation, method, xmlDescription, swaggerGenerator);
            }
        }