Beispiel #1
0
        public IActionResult Get()
        {
            string swaggerDocName            = "v1";
            PostmanRootCollection collection = postmanConverter.GetPostmanCollection(swaggerDocName, "localhost", "http://localhost:24724/");

            JsonSerializerSettings jsonSettings = new JsonSerializerSettings();

            jsonSettings.NullValueHandling = NullValueHandling.Ignore;
            jsonSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

            return(Json(collection, jsonSettings));
        }
Beispiel #2
0
        public async Task Invoke(HttpContext httpContext)
        {
            string documentName;

            if (!PathMatchesPostmanDoc(httpContext.Request, out documentName))
            {
                await _next(httpContext);

                return;
            }

            var basePath = string.IsNullOrEmpty(httpContext.Request.PathBase)
                ? null
                : httpContext.Request.PathBase.ToString();
            var            host           = httpContext.Request.Host != null ? httpContext.Request.Host.Value : "";
            JsonSerializer jsonSerializer = new JsonSerializer()
            {
                Formatting        = Formatting.Indented,
                NullValueHandling = NullValueHandling.Ignore
            };

            jsonSerializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
            var jsonBuilder = new StringBuilder();

            using (var writer = new StringWriter(jsonBuilder))
            {
                httpContext.Response.ContentType = "application/json";

                try
                {
                    PostmanRootCollection postmanCollection = _postmanConverter.GetPostmanCollection(documentName, host, basePath);

                    httpContext.Response.StatusCode = 200;
                    jsonSerializer.Serialize(writer, postmanCollection);
                    await httpContext.Response.WriteAsync(jsonBuilder.ToString(), new UTF8Encoding(false));
                }
                catch (UnknownSwaggerDocument)
                {
                    httpContext.Response.StatusCode = 404;
                    jsonSerializer.Serialize(writer, new ErrorResponse
                    {
                        Message = $"Unknown document '{documentName??""}'"
                    });
                    await httpContext.Response.WriteAsync(jsonBuilder.ToString(), new UTF8Encoding(false));
                }
            }
        }