Пример #1
0
        private void AddPath(SwaggerDocument swaggerDocument,
                             ReRoute reRoute, IReadOnlyDictionary <string, SwaggerDocument> documents)
        {
            var host = reRoute.DownstreamHostAndPorts.First().ToUrl();

            if (!documents.ContainsKey(host))
            {
                Log(new Exception($"不能找到 '{host}' 相对应的 Swagger 文档。"));
                return;
            }

            var document     = documents[host];
            var pathTemplate = reRoute.DownstreamPathTemplate;
            var httpMethods  = reRoute.UpstreamHttpMethod;

            var pathKey = document.Paths.Keys
                          .FirstOrDefault(x => string.Compare(x, pathTemplate, StringComparison.OrdinalIgnoreCase) == 0);

            if (string.IsNullOrWhiteSpace(pathKey))
            {
                Log(new Exception($"在 '{host}' 中不能找到 '{pathTemplate}' 相对应的值。"));
                return;
            }

            var pathItem = document.Paths[pathKey];

            SwaggerPathItem newPathItem = null;

            if (swaggerDocument.Paths.ContainsKey(reRoute.UpstreamPathTemplate))
            {
                newPathItem = swaggerDocument.Paths[reRoute.UpstreamPathTemplate];
            }
            else
            {
                newPathItem = new SwaggerPathItem()
                {
                    Summary     = pathItem.Summary,
                    Description = pathItem.Description,
                    Servers     = pathItem.Servers,
                    Parameters  = pathItem.Parameters,
                };

                swaggerDocument.Paths.Add(reRoute.UpstreamPathTemplate, newPathItem);
            }

            foreach (var httpMethod in httpMethods)
            {
                var operationMethod = ConvertToSwaggerOperationMethod(httpMethod);
                if (!pathItem.ContainsKey(operationMethod))
                {
                    Log(new Exception($"在 '{host}' 中不能找到 '({httpMethod}){pathKey}' 相对应的值。"));
                    continue;
                }

                newPathItem.Add(operationMethod, pathItem[operationMethod]);
            }
        }
Пример #2
0
        public override ObservableList <ApplicationAPIModel> ParseDocument(string FileName, bool avoidDuplicatesNodes = false)
        {
            string FinalFileName = "";
            Uri    url           = new Uri(FileName);

            ObservableList <ApplicationAPIModel> SwaggerModels = new ObservableList <ApplicationAPIModel>();
            string orignaljson = "";

            if (url.IsFile)
            {
                orignaljson = System.IO.File.ReadAllText(FileName);
            }
            {
                orignaljson = GeneralLib.HttpUtilities.Download(url);
            }
            try
            {
                JToken.Parse(orignaljson);
                FinalFileName = FileName;
            }
            catch
            {
                var          r            = new StringReader(orignaljson);
                var          deserializer = new Deserializer();
                var          yamlObject   = deserializer.Deserialize(r);
                StringWriter tw           = new StringWriter();
                var          serializer   = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(tw, yamlObject);
                orignaljson = tw.ToString();
                string tempfile = System.IO.Path.GetTempFileName();

                System.IO.File.WriteAllText(tempfile, orignaljson);
                FinalFileName = tempfile;
            }


            Swaggerdoc = SwaggerDocument.FromJsonAsync(orignaljson).Result;
            foreach (var paths in Swaggerdoc.Paths)
            {
                SwaggerPathItem SPi = paths.Value;
                foreach (KeyValuePair <SwaggerOperationMethod, SwaggerOperation> so in SPi.AsEnumerable())
                {
                    SwaggerOperation Operation = so.Value;


                    bool supportBody = true;
                    if (Operation.RequestBody == null && Operation.ActualConsumes.Count() == 0)
                    {
                        ApplicationAPIModel basicModal = GenerateBasicModel(Operation, so.Key, ref supportBody, paths.Key);
                        SwaggerModels.Add(basicModal);
                        GenerateResponse(Operation, basicModal);
                    }


                    else if (Operation.ActualConsumes.Count() == 0 && Operation.RequestBody.Content.Count() != 0)
                    {
                        foreach (var body in Operation.RequestBody.Content)
                        {
                            ApplicationAPIModel AAM = GenerateBasicModel(Operation, so.Key, ref supportBody, paths.Key);



                            if (supportBody)
                            {
                                switch (body.Key)
                                {
                                case "application/x-www-form-urlencoded":
                                    GenerateFormParameters(AAM, Operation);
                                    break;

                                case "multipart/form-data":
                                    GenerateFormParameters(AAM, Operation, true);
                                    break;

                                case "application/json":
                                    AAM.ContentType         = ApplicationAPIUtils.eContentType.JSon;
                                    AAM.ResponseContentType = ApplicationAPIUtils.eContentType.JSon;
                                    if (Operation.RequestBody != null)
                                    {
                                        AAM.AppModelParameters.Append(GenerateJsonBody(AAM, Operation.RequestBody.Content.ElementAt(0).Value.Schema));
                                    }
                                    if (Operation.ActualConsumes.Count() > 1)
                                    {
                                        AAM.Name += "-JSON"; AAM.Description = "Body Type is JSON";
                                    }

                                    break;

                                case "application/xml":
                                    AAM.ContentType         = ApplicationAPIUtils.eContentType.XML;
                                    AAM.ResponseContentType = ApplicationAPIUtils.eContentType.XML;
                                    if (Operation.RequestBody != null)
                                    {
                                        AAM.AppModelParameters.Append(GenerateXMLBody(AAM, Operation.RequestBody.Content.ElementAt(0).Value.Schema));
                                    }
                                    if (Operation.ActualConsumes.Count() > 1)
                                    {
                                        AAM.Name       += "-XML";
                                        AAM.Description = "Body Type is XML";
                                    }

                                    break;
                                }
                            }
                            GenerateResponse(Operation, AAM);
                            SwaggerModels.Add(AAM);
                        }
                    }

                    foreach (var body in    Operation.ActualConsumes)
                    {
                        ApplicationAPIModel AAM = GenerateBasicModel(Operation, so.Key, ref supportBody, paths.Key);



                        if (supportBody)
                        {
                            switch (body)
                            {
                            case "application/x-www-form-urlencoded":
                                GenerateFormParameters(AAM, Operation);
                                break;

                            case "multipart/form-data":
                                GenerateFormParameters(AAM, Operation, true);
                                break;

                            case "application/json":
                                AAM.ContentType         = ApplicationAPIUtils.eContentType.JSon;
                                AAM.ResponseContentType = ApplicationAPIUtils.eContentType.JSon;
                                if (Operation.RequestBody != null)
                                {
                                    AAM.AppModelParameters.Append(GenerateJsonBody(AAM, Operation.RequestBody.Content.ElementAt(0).Value.Schema));
                                }
                                if (Operation.ActualConsumes.Count() > 1)
                                {
                                    AAM.Name       += "-JSON";
                                    AAM.Description = "Body Type is JSON";
                                }

                                break;

                            case "application/xml":
                                AAM.ContentType         = ApplicationAPIUtils.eContentType.XML;
                                AAM.ResponseContentType = ApplicationAPIUtils.eContentType.XML;
                                if (Operation.RequestBody != null)
                                {
                                    AAM.AppModelParameters.Append(GenerateXMLBody(AAM, Operation.RequestBody.Content.ElementAt(0).Value.Schema));
                                }
                                if (Operation.ActualConsumes.Count() > 1)
                                {
                                    AAM.Name       += "-XML";
                                    AAM.Description = "Body Type is XML";
                                }
                                break;
                            }
                        }
                        GenerateResponse(Operation, AAM);

                        SwaggerModels.Add(AAM);
                    }
                }
            }
            return(SwaggerModels);
        }