示例#1
0
        private static ApiModel ParseSwagger(SwaggerModel obj)
        {
            ApiModel result = new ApiModel();

            // Loop through all paths and spit them out to the console
            foreach (var path in (from p in obj.paths orderby p.Key select p))
            {
                foreach (var verb in path.Value)
                {
                    // Set up our API
                    ApiInfo api = new ApiInfo();
                    api.URI         = path.Key;
                    api.HttpVerb    = verb.Key;
                    api.Comment     = verb.Value.summary;
                    api.Params      = new List <ParameterInfo>();
                    api.QueryParams = new List <ParameterInfo>();
                    api.Category    = verb.Value.tags.FirstOrDefault();
                    api.OperationId = verb.Value.operationId.Replace("ApiV2", "");

                    // Now figure out all the URL parameters
                    foreach (var parameter in verb.Value.parameters)
                    {
                        // Query String Parameters
                        if (parameter.paramIn == "query")
                        {
                            api.QueryParams.Add(new ParameterInfo()
                            {
                                Comment   = parameter.description ?? "",
                                ParamName = parameter.name,
                                TypeName  = ResolveType(parameter, parameter.schema)
                            });

                            // URL Path parameters
                        }
                        else if (parameter.paramIn == "path")
                        {
                            api.Params.Add(new ParameterInfo()
                            {
                                Comment   = parameter.description ?? "",
                                ParamName = parameter.name,
                                TypeName  = ResolveType(parameter, parameter.schema)
                            });

                            // Body parameters
                        }
                        else if (parameter.paramIn == "body")
                        {
                            api.BodyParam = new ParameterInfo()
                            {
                                Comment   = parameter.description ?? "",
                                ParamName = "model",
                                TypeName  = ResolveType(parameter, parameter.schema)
                            };
                        }

                        // Is this property an enum?
                        if (parameter.EnumDataType != null)
                        {
                            ExtractEnum(result.Enums, parameter);
                        }
                    }

                    // Now figure out the response type
                    SwaggerResult ok = null;
                    if (verb.Value.responses.TryGetValue("200", out ok))
                    {
                        api.TypeName = ResolveType(null, ok.schema);
                    }
                    else if (verb.Value.responses.TryGetValue("201", out ok))
                    {
                        api.TypeName = ResolveType(null, ok.schema);
                    }

                    // Done with this API
                    result.Methods.Add(api);
                }
            }

            // Loop through all the schemas
            foreach (var def in obj.definitions)
            {
                var m = new ModelInfo()
                {
                    SchemaName = def.Key,
                    Comment    = def.Value.description,
                    Properties = new List <ParameterInfo>()
                };
                foreach (var prop in def.Value.properties)
                {
                    if (!prop.Value.required && def.Value.required != null)
                    {
                        prop.Value.required = def.Value.required.Contains(prop.Key);
                    }
                    m.Properties.Add(new ParameterInfo()
                    {
                        Comment   = prop.Value.description,
                        ParamName = prop.Key,
                        TypeName  = ResolveType(prop.Value, null)
                    });

                    // Is this property an enum?
                    if (prop.Value.EnumDataType != null)
                    {
                        ExtractEnum(result.Enums, prop.Value);
                    }
                }

                result.Models.Add(m);
            }

            // Now add the enums we know we need.
            // Because of the complex way this Dictionary<> is rendered in Swagger, it's hard to pick up the correct values.
            var tat = new EnumInfo()
            {
                EnumDataType = "TransactionAddressType",
                Items        = new List <EnumItem>()
            };

            tat.AddItem("ShipFrom", "This is the location from which the product was shipped");
            tat.AddItem("ShipTo", "This is the location to which the product was shipped");
            tat.AddItem("PointOfOrderAcceptance", "Location where the order was accepted; typically the call center, business office where purchase orders are accepted, server locations where orders are processed and accepted");
            tat.AddItem("PointOfOrderOrigin", "Location from which the order was placed; typically the customer's home or business location");
            tat.AddItem("SingleLocation", "Only used if all addresses for this transaction were identical; e.g. if this was a point-of-sale physical transaction");
            result.Enums.Add(tat);

            // Here's your processed API
            return(result);
        }