private ControllerMethod BuildControllerMethod(string url, Operation method, RAML.Parser.Model.EndPoint resource, ControllerObject parent, IDictionary <string, Parameter> parentUriParameters, string modelsNamespace) { var relativeUri = UrlGeneratorHelper.GetRelativeUri(url, parent.PrefixUri); var parentUrl = UrlGeneratorHelper.GetParentUri(url, resource.Path); var operationWithSecurity = resource.Operations.FirstOrDefault(m => m.Method == method.Method && m.Security != null && m.Security.Any()); var securedBy = operationWithSecurity?.Security?.Where(s => s != null) .Select(s => s.Name).ToArray(); return(new ControllerMethod { ModelsNamespace = modelsNamespace, Name = NetNamingMapper.GetMethodName(method.Method ?? "Get" + resource.Path), Parameter = GetParameter(GeneratorServiceHelper.GetKeyForResource(method, resource), method, resource, url), UriParameters = uriParametersGenerator.GetUriParameters(resource, url, parentUriParameters), ResponseStatusCode = method.Responses != null && method.Responses.Count() == 1 ? method.Responses.First().StatusCode : null, ReturnType = GetReturnType(method, resource, url), Comment = GetComment(resource, method, url), Url = relativeUri, Verb = NetNamingMapper.Capitalize(method.Method), Parent = null, UseSecurity = resource.Operations.Any(m => m.Method == method.Method && m.Security != null && m.Security.Any()), SecuredBy = securedBy, SecurityParameters = GetSecurityParameters(method) }); }
private void AddProperty(RAML.Parser.Model.EndPoint resource, Operation method, string key, Response response, ICollection <Property> properties, string fullUrl) { var mimeType = GeneratorServiceHelper.GetMimeType(response); if (mimeType == null) { return; } var type = responseTypesService.GetResponseType(method, resource, mimeType, key, response.StatusCode, fullUrl); if (string.IsNullOrWhiteSpace(type)) { return; } var name = NetNamingMapper.GetPropertyName(CollectionTypeHelper.GetBaseType(type)); if (properties.Any(p => p.Name == name)) { name = name + response.StatusCode; } var property = new Property { Name = name, Description = response.Description + " " + mimeType.Schema.Description, Example = ObjectParser.MapExample(mimeType.Schema), Type = type, StatusCode = GetStatusCode(response), JSONSchema = mimeType.Schema as SchemaShape == null ? null : ((SchemaShape)mimeType.Schema).Raw.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\n", "").Replace("\"", "\\\"") }; properties.Add(property); }
private string HandleMultipleSchemaType(IEnumerable <Response> responses, RAML.Parser.Model.EndPoint resource, Operation method, string fullUrl) { var properties = GetProperties(responses, resource, method, fullUrl); if (properties.Count == 0) { return("string"); } if (properties.Count == 1) { return(properties.First().Type); } // Build a new response object containing all types var key = GeneratorServiceHelper.GetKeyForResource(method, resource); var name = NetNamingMapper.GetObjectName("Multiple" + key); var apiObject = new ApiObject { Name = name, Description = "Multiple Response Types " + string.Join(", ", properties.Select(p => p.Name)), Properties = properties, IsMultiple = true }; schemaResponseObjects.Add(new KeyValuePair <string, ApiObject>(name, apiObject)); return(name); }
private static string GetObjectNameForParameter(RAML.Parser.Model.EndPoint resource) { var relativeUri = resource.Path.Replace("{mediaTypeExtension}", string.Empty); var objectNameForParameter = relativeUri.Substring(1).Replace("{", string.Empty).Replace("}", string.Empty); objectNameForParameter = NetNamingMapper.GetObjectName(objectNameForParameter); return(objectNameForParameter); }
private List <Property> GetProperties(IEnumerable <Response> responses, RAML.Parser.Model.EndPoint resource, Operation method, string fullUrl) { var properties = new List <Property>(); foreach (var response in responses) { var key = GeneratorServiceHelper.GetKeyForResource(method, resource, response); AddProperty(resource, method, key, response, properties, fullUrl); } return(properties); }
protected string GetReturnType(Operation method, RAML.Parser.Model.EndPoint resource, string fullUrl) { if (method.Responses.All(r => !r.Payloads.Any(p => p.Schema != null))) { return("string"); } var responses = method.Responses .Where(r => r.Payloads.Any(b => b.Schema != null)) .ToArray(); var returnType = HandleMultipleSchemaType(responses, resource, method, fullUrl); if (!string.IsNullOrWhiteSpace(returnType)) { return(returnType); } return("string"); }
public IEnumerable <ControllerMethod> GetMethods(RAML.Parser.Model.EndPoint endpoint, string url, ControllerObject parent, string objectName, IDictionary <string, Parameter> parentUriParameters, string modelsNamespace) { var methodsNames = new List <string>(); if (parent != null && parent.Methods != null) { methodsNames = parent.Methods.Select(m => m.Name).ToList(); } var generatorMethods = new Collection <ControllerMethod>(); if (endpoint.Operations == null) { return(generatorMethods); } foreach (var method in endpoint.Operations) { var generatedMethod = BuildControllerMethod(url, method, endpoint, parent, parentUriParameters, modelsNamespace); if (IsVerbForMethod(method)) { if (methodsNames.Contains(generatedMethod.Name)) { generatedMethod.Name = GetUniqueName(methodsNames, generatedMethod.Name, GetRelativePath(endpoint.Path, parent.PrefixUri)); } if (method.Request != null && method.Request.QueryParameters != null && method.Request.QueryParameters.Any()) { var queryParameters = queryParametersParser.ParseParameters(method); generatedMethod.QueryParameters = queryParameters; } generatorMethods.Add(generatedMethod); methodsNames.Add(generatedMethod.Name); } } return(generatorMethods); }
private void AddGeneratedMethod(RAML.Parser.Model.EndPoint resource, string url, string objectName, Operation method, ICollection <string> methodsNames, ICollection <ClientGeneratorMethod> generatorMethods, IDictionary <string, Parameter> parentUriParameters, string modelsNamespace) { var generatedMethod = BuildClassMethod(url, method, resource, modelsNamespace); if (generatedMethod.ReturnType != "string") { var returnType = CollectionTypeHelper.GetBaseType(generatedMethod.ReturnType); var returnTypeObject = schemaObjects.Values.Any(o => o.Name == returnType) ? schemaObjects.Values.First(o => o.Name == returnType) : schemaResponseObjects.Values.FirstOrDefault(o => o.Name == returnType); if (returnTypeObject != null) { generatedMethod.ReturnTypeObject = returnTypeObject; generatedMethod.OkReturnType = GetOkReturnType(generatedMethod); } } uriParametersGenerator.Generate(resource, url, generatedMethod, uriParameterObjects, parentUriParameters); if (!IsVerbForMethod(method)) { return; } if (methodsNames.Contains(generatedMethod.Name)) { generatedMethod.Name = GetUniqueName(methodsNames, generatedMethod.Name, resource.Path); } GetQueryParameters(objectName, method, generatedMethod); GetHeaders(objectName, method, generatedMethod); GetResponseHeaders(objectName, generatedMethod, method); generatorMethods.Add(generatedMethod); methodsNames.Add(generatedMethod.Name); }
protected string GetComment(RAML.Parser.Model.EndPoint resource, Operation method, string url) { var description = resource.Description; if (!string.IsNullOrWhiteSpace(method.Description)) { description += string.IsNullOrWhiteSpace(description) ? method.Description : ". " + method.Description; } if (description != null) { description = new SchemaParameterParser(new EnglishPluralizationService()).Parse(description, resource, method, url); } description = ParserHelpers.RemoveNewLines(description); if (!string.IsNullOrWhiteSpace(resource.Path)) { description += string.IsNullOrWhiteSpace(description) ? resource.Path : " - " + resource.Path; } return(description); }
public ICollection <ClientGeneratorMethod> GetMethods(RAML.Parser.Model.EndPoint resource, string url, ClassObject parent, string objectName, IDictionary <string, Parameter> parentUriParameters, string modelsNamespace) { var methodsNames = new List <string>(); if (parent != null) { methodsNames = parent.Methods.Select(m => m.Name).ToList(); } var generatorMethods = new Collection <ClientGeneratorMethod>(); if (resource.Operations == null) { return(generatorMethods); } foreach (var method in resource.Operations) { AddGeneratedMethod(resource, url, objectName, method, methodsNames, generatorMethods, parentUriParameters, modelsNamespace); } return(generatorMethods); }
protected string GetUniqueObjectName(RAML.Parser.Model.EndPoint resource, RAML.Parser.Model.EndPoint parent) { string objectName; if (resource.Path.StartsWith("/{") && resource.Path.EndsWith("}")) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(resource)); } else { if (resource.Path == "/") { objectName = "RootUrl"; } else { objectName = NetNamingMapper.GetObjectName(resource.Path); } if (classesNames.Contains(objectName)) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(resource)); } } if (string.IsNullOrWhiteSpace(objectName)) { throw new InvalidOperationException("object name is null for " + resource.Path); } if (!classesNames.Contains(objectName)) { return(objectName); } if (parent == null || string.IsNullOrWhiteSpace(parent.Path)) { return(GetUniqueObjectName(objectName)); } if (resource.Path.StartsWith("/{") && parent.Path.EndsWith("}")) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(parent)) + objectName; } else { objectName = NetNamingMapper.GetObjectName(parent.Path) + objectName; if (classesNames.Contains(objectName)) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(parent)); } } if (string.IsNullOrWhiteSpace(objectName)) { throw new InvalidOperationException("object name is null for " + resource.Path); } if (!classesNames.Contains(objectName)) { return(objectName); } return(GetUniqueObjectName(objectName)); }
protected static void GetInheritedUriParams(IDictionary <string, Parameter> parentUriParameters, RAML.Parser.Model.EndPoint resource) { foreach (var uriParam in resource.Parameters.Where(p => p.Binding == "URL")) //TODO: check { if (!parentUriParameters.ContainsKey(uriParam.Name)) { parentUriParameters.Add(uriParam.Name, uriParam); } } }
private ClientGeneratorMethod BuildClassMethod(string url, Operation operation, RAML.Parser.Model.EndPoint resource, string modelsNamespace) { var parentUrl = UrlGeneratorHelper.GetParentUri(url, resource.Path); //TODO: check var responseContentTypes = operation.Responses != null? operation.Responses.Where(r => r.Payloads != null).SelectMany(r => r.Payloads).Select(p => p.MediaType).ToArray() : new string[0]; var generatedMethod = new ClientGeneratorMethod { ModelsNamespace = modelsNamespace, Name = NetNamingMapper.GetMethodName(operation.Method ?? "Get" + resource.Path), ReturnType = GetReturnType(operation, resource, url), Parameter = GetParameter(GeneratorServiceHelper.GetKeyForResource(operation, resource), operation, resource, url), Comment = GetComment(resource, operation, url), Url = url, Verb = NetNamingMapper.Capitalize(operation.Method), Parent = null, UseSecurity = resource.Operations.Any(m => m.Method == operation.Method && m.Security != null && m.Security.Any()), RequestContentTypes = operation.ContentType, ResponseContentTypes = responseContentTypes }; // look in traits // look in resource types return(generatedMethod); }
protected GeneratorParameter GetParameter(string key, Operation method, RAML.Parser.Model.EndPoint resource, string fullUrl) { return(requestTypesService.GetRequestParameter(key, method, resource, fullUrl, raml.WebApi.Accepts)); }