Esempio n. 1
0
        static byte[] BuildSwaggerJson(SwaggerOptions options, IDictionary <string, object> environment, RegisteredHandlersInfo handlersInfo)
        {
            try
            {
                if (options.XmlDocumentPath != null && !File.Exists(options.XmlDocumentPath))
                {
                    return(Encoding.UTF8.GetBytes("Xml doesn't exists at " + options.XmlDocumentPath));
                }

                var xDocLookup = (options.XmlDocumentPath != null)
                    ? BuildXmlCommentStructure(options.XmlDocumentPath)
                    : null;

                var doc = new SwaggerDocument();
                doc.swagger  = "2.0";
                doc.info     = options.Info;
                doc.host     = (options.CustomHost != null) ? options.CustomHost(environment) : environment.AsRequestHeaders()["Host"][0];
                doc.basePath = options.ApiBasePath;
                doc.schemes  = new[] { environment.AsRequestScheme() };
                doc.paths    = new Dictionary <string, PathItem>();

                foreach (var item in handlersInfo.RegisteredHandlers)
                {
                    XmlCommentStructure xmlComment = null;
                    if (xDocLookup != null)
                    {
                        xmlComment = xDocLookup[Tuple.Create(item.Value.ClassName, item.Value.MethodName)].FirstOrDefault();
                    }

                    var parameters = item.Value.Parameters
                                     .Select(x =>
                    {
                        var parameterXmlComment = x.ParameterType.Name;
                        if (xmlComment != null)
                        {
                            xmlComment.Parameters.TryGetValue(x.Name, out parameterXmlComment);
                            parameterXmlComment = x.ParameterType.Name + " " + parameterXmlComment;
                        }

                        var defaultValue = x.DefaultValue;
                        if (defaultValue != null && x.ParameterType.IsEnum)
                        {
                            defaultValue = (options.IsEmitEnumAsString)
                                    ? defaultValue.ToString()
                                    : Convert.ChangeType(defaultValue, Enum.GetUnderlyingType(defaultValue.GetType()));
                        }

                        var items = (x.ParameterTypeIsArray)
                                ? new Items {
                            type = TypeToType(x.ParameterType.GetElementType())
                        }
                                : null;

                        object[] enums = null;
                        if (x.ParameterType.IsEnum || (x.ParameterType.IsArray && x.ParameterType.GetElementType().IsEnum))
                        {
                            var enumType = (x.ParameterType.IsEnum) ? x.ParameterType : x.ParameterType.GetElementType();

                            var enumValues = Enum.GetValues(enumType).Cast <object>()
                                             .Select(v =>
                            {
                                return((options.IsEmitEnumAsString)
                                            ? v.ToString()
                                            : Convert.ChangeType(v, Enum.GetUnderlyingType(enumType)));
                            })
                                             .ToArray();

                            if (x.ParameterType.IsArray)
                            {
                                items.@enum = enumValues;
                            }
                            else
                            {
                                enums = enumValues;
                            }
                        }

                        return(new Parameter
                        {
                            name = x.Name,
                            @in = item.Value.AcceptVerbs.HasFlag(AcceptVerbs.Get) ? "query" : "formData",
                            type = TypeToType(x.ParameterType),
                            description = parameterXmlComment,
                            required = !x.IsOptional,
                            @default = (x.IsOptional) ? defaultValue : null,
                            items = items,
                            @enum = enums,
                            collectionFormat = "multi"
                        });
                    })
                                     .ToArray();

                    var operation = new Operation
                    {
                        tags        = new[] { item.Value.ClassName },
                        summary     = (xmlComment != null) ? xmlComment.Summary : "",
                        description = (xmlComment != null) ? xmlComment.Remarks : "",
                        parameters  = parameters
                    };

                    doc.paths.Add(item.Key, CreatePathItem(item.Value.AcceptVerbs, operation));
                }

                using (var ms = new MemoryStream())
                {
                    var serializer = new DataContractJsonSerializer(typeof(SwaggerDocument), new DataContractJsonSerializerSettings
                    {
                        SerializeReadOnlyTypes    = true,
                        UseSimpleDictionaryFormat = true
                    });
                    serializer.WriteObject(ms, doc);
                    return(ms.ToArray());
                }
            }
            catch (Exception ex)
            {
                return(Encoding.UTF8.GetBytes(ex.ToString()));
            }
        }
Esempio n. 2
0
 public LightNodeSwaggerMiddleware(AppFunc next, SwaggerOptions options)
 {
     this.next    = next;
     this.options = options;
 }
 public LightNodeSwaggerMiddleware(RequestDelegate next, SwaggerOptions options)
 {
     this.next    = next;
     this.options = options;
 }