/// <summary> /// Execute this template against the specified model /// </summary> /// <param name="model"></param> /// <param name="m"></param> /// <param name="e"></param> /// <returns></returns> public virtual string ExecuteTemplate(OkapiInfo api, OkapiMethodInfo method, OkapiModelInfo model, OkapiEnumInfo enumDataType) { Buffer.Clear(); SwaggerModel = api; MethodModel = method; ClassModel = model; EnumModel = enumDataType; Execute(); return(Buffer.ToString()); }
public string PhpComment(OkapiMethodInfo method, int indent) { List <string> lines = new List <string>(); lines.Add(method.Summary); lines.Add(""); // Break apart description into multiple lines var descriptionLines = SplitLines(method.Description); if (descriptionLines.Count > 0) { lines.AddRange(descriptionLines); lines.Add(""); } // Add one line for each parameter foreach (var pc in method.Params) { if (pc.CleanParamName != "X-Avalara-Client") { lines.Add("@param " + PhpTypeName(pc.TypeName) + " " + pc.CleanParamName + " " + PhpTypeComment(SwaggerModel, pc)); } } // Add the "return" value lines.Add("@return " + PhpTypeName(method.ResponseTypeName)); // Construct an indent StringBuilder sb = new StringBuilder(); for (int i = 0; i < indent; i++) { sb.Append(' '); } var indentString = sb.ToString(); // Now turn these lines into a JavaDoc indentString = indentString + " "; sb.AppendLine("/**"); foreach (var l in lines) { sb.Append(indentString); sb.Append("* "); sb.AppendLine(l); } sb.Append(indentString); sb.AppendLine("*/"); return(sb.ToString()); }
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); }