Example #1
0
        private static OkapiInfo Cleanup(SwaggerModel obj)
        {
            OkapiInfo result = new OkapiInfo();

            result.ApiVersion = obj.ApiVersion;

            // Set up alternative version numbers: This one does not permit dashes
            result.ApiVersionPeriodsOnly = result.ApiVersion.Replace("-", ".");

            // Set up alternative version numbers: This one permits only three segments
            var sb         = new StringBuilder();
            int numPeriods = 0;

            foreach (char c in obj.ApiVersion)
            {
                if (c == '.')
                {
                    numPeriods++;
                }
                if (numPeriods > 3 || c == '-')
                {
                    break;
                }
                sb.Append(c);
            }
            result.ApiVersionThreeSegmentsOnly = sb.ToString();

            // 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
                    var api = new OkapiMethodInfo();
                    api.URI         = path.Key;
                    api.HttpVerb    = verb.Key;
                    api.Summary     = verb.Value.summary;
                    api.Description = verb.Value.description;
                    api.Params      = new List <OkapiParameterInfo>();
                    api.Category    = verb.Value.tags.FirstOrDefault();
                    api.Name        = verb.Value.operationId;

                    // Now figure out all the URL parameters
                    foreach (var parameter in verb.Value.parameters)
                    {
                        // Construct parameter
                        var pi = ResolveType(parameter);

                        // Query String Parameters
                        if (parameter.paramIn == "query")
                        {
                            pi.ParameterLocation = ParameterLocationType.QueryString;

                            // URL Path parameters
                        }
                        else if (parameter.paramIn == "path")
                        {
                            pi.ParameterLocation = ParameterLocationType.UriPath;

                            // Body parameters
                        }
                        else if (parameter.paramIn == "body")
                        {
                            pi.ParamName         = "model";
                            pi.ParameterLocation = ParameterLocationType.RequestBody;
                            api.BodyParam        = pi;
                        }
                        else if (parameter.paramIn == "header")
                        {
                            pi.ParameterLocation = ParameterLocationType.Header;
                        }
                        else if (parameter.paramIn == "formData")
                        {
                            pi.ParameterLocation = ParameterLocationType.FormData;
                        }
                        else
                        {
                            throw new Exception("Unrecognized parameter location: " + parameter.paramIn);
                        }
                        api.Params.Add(pi);

                        // 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.ResponseType     = ok.schema == null ? null : ok.schema.type;
                        api.ResponseTypeName = ResolveTypeName(ok.schema);
                    }
                    else if (verb.Value.responses.TryGetValue("201", out ok))
                    {
                        api.ResponseType     = ok.schema == null ? null : ok.schema.type;
                        api.ResponseTypeName = ResolveTypeName(ok.schema);
                    }

                    // Ensure that body parameters are always last for consistency
                    if (api.BodyParam != null)
                    {
                        api.Params.Remove(api.BodyParam);
                        api.Params.Add(api.BodyParam);
                    }

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

            // Loop through all the schemas
            foreach (var def in obj.definitions)
            {
                var m = new OkapiModelInfo()
                {
                    SchemaName  = def.Key,
                    Comment     = def.Value.description,
                    Example     = def.Value.example,
                    Description = def.Value.description,
                    Required    = def.Value.required,
                    Type        = def.Value.type,
                    Properties  = new List <OkapiParameterInfo>()
                };
                foreach (var prop in def.Value.properties)
                {
                    if (!prop.Value.required && def.Value.required != null)
                    {
                        prop.Value.required = def.Value.required.Contains(prop.Key);
                    }

                    // Construct property
                    var pi = ResolveType(prop.Value);
                    pi.ParamName = prop.Key;
                    m.Properties.Add(pi);

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

                result.Models.Add(m);
            }

            // Here's your processed API
            return(result);
        }
Example #2
0
        private static SwaggerInfo Cleanup(SwaggerModel obj)
        {
            SwaggerInfo result = new SwaggerInfo();

            result.ApiVersion = obj.ApiVersion;

            // Set up alternative version numbers: This one does not permit dashes
            result.ApiVersionPeriodsOnly = result.ApiVersion.Replace("-", ".");

            // Set up alternative version numbers: This one permits only three segments
            var sb         = new StringBuilder();
            int numPeriods = 0;

            foreach (char c in obj.ApiVersion)
            {
                if (c == '.')
                {
                    numPeriods++;
                }
                if (numPeriods > 3 || c == '-')
                {
                    break;
                }
                sb.Append(c);
            }
            result.ApiVersionThreeSegmentsOnly = sb.ToString();

            // 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
                    MethodInfo api = new MethodInfo();
                    api.URI         = path.Key;
                    api.HttpVerb    = verb.Key;
                    api.Summary     = verb.Value.summary;
                    api.Description = verb.Value.description;
                    api.Params      = new List <ParameterInfo>();
                    api.Category    = verb.Value.tags.FirstOrDefault();
                    api.Name        = verb.Value.operationId;

                    // Now figure out all the URL parameters
                    foreach (var parameter in verb.Value.parameters)
                    {
                        // Construct parameter
                        var pi = ResolveType(parameter);

                        // Query String Parameters
                        if (parameter.paramIn == "query")
                        {
                            pi.ParameterLocation = ParameterLocationType.QueryString;

                            // URL Path parameters
                        }
                        else if (parameter.paramIn == "path")
                        {
                            pi.ParameterLocation = ParameterLocationType.UriPath;

                            // Body parameters
                        }
                        else if (parameter.paramIn == "body")
                        {
                            pi.ParamName         = "model";
                            pi.ParameterLocation = ParameterLocationType.RequestBody;
                            api.BodyParam        = pi;
                        }
                        else if (parameter.paramIn == "header")
                        {
                            pi.ParameterLocation = ParameterLocationType.Header;
                        }
                        else if (parameter.paramIn == "formData")
                        {
                            pi.ParameterLocation = ParameterLocationType.FormData;
                        }
                        else
                        {
                            throw new Exception("Unrecognized parameter location: " + parameter.paramIn);
                        }
                        api.Params.Add(pi);

                        // 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.ResponseType     = ok.schema == null ? null : ok.schema.type;
                        api.ResponseTypeName = ResolveTypeName(ok.schema);
                    }
                    else if (verb.Value.responses.TryGetValue("201", out ok))
                    {
                        api.ResponseType     = ok.schema == null ? null : ok.schema.type;
                        api.ResponseTypeName = ResolveTypeName(ok.schema);
                    }

                    // Ensure that body parameters are always last for consistency
                    if (api.BodyParam != null)
                    {
                        api.Params.Remove(api.BodyParam);
                        api.Params.Add(api.BodyParam);
                    }

                    // 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,
                    Example     = def.Value.example,
                    Description = def.Value.description,
                    Required    = def.Value.required,
                    Type        = def.Value.type,
                    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);
                    }

                    // Construct property
                    var pi = ResolveType(prop.Value);
                    pi.ParamName = prop.Key;
                    m.Properties.Add(pi);

                    // 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 = (from e in result.Enums where e.EnumDataType == "TransactionAddressType" select e).FirstOrDefault();
            //if (tat == null) {
            //    tat = new EnumInfo()
            //    {
            //        EnumDataType = "TransactionAddressType",
            //        Items = new List<EnumItem>()
            //    };
            //    result.Enums.Add(tat);
            //}
            //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");

            // Here's your processed API
            return(result);
        }
        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);
        }