protected GeneratorParameter GetParameter(string key, Method method, Resource resource, string fullUrl) { var schema = GetJsonSchemaOrDefault(method.Body); if (schema != null) { var generatorParameter = GetGeneratorParameterWhenNamed(method, resource, schema, fullUrl); if (generatorParameter != null) return generatorParameter; } if (resource.Type != null && resource.Type.Any() && raml.ResourceTypes.Any(rt => rt.ContainsKey(resource.GetResourceType()))) { var verb = GetResourceTypeVerb(method, resource); if (verb != null && verb.Body != null && !string.IsNullOrWhiteSpace(verb.Body.Schema)) { var generatorParameter = GetGeneratorParameterWhenNamed(method, resource, verb.Body.Schema, fullUrl); if (generatorParameter != null) return generatorParameter; } } if (RequestHasKey(key)) return GetGeneratorParameterByKey(key); var requestKey = key + GeneratorServiceBase.RequestContentSuffix; if (RequestHasKey(requestKey)) return GetGeneratorParameterByKey(requestKey); if (linkKeysWithObjectNames.ContainsKey(key)) { var linkedKey = linkKeysWithObjectNames[key]; if (RequestHasKey(linkedKey)) return GetGeneratorParameterByKey(linkedKey); } if (linkKeysWithObjectNames.ContainsKey(requestKey)) { var linkedKey = linkKeysWithObjectNames[requestKey]; if (RequestHasKey(linkedKey)) return GetGeneratorParameterByKey(linkedKey); } return new GeneratorParameter {Name = "content", Type = "string"}; }
private static string ReplaceCustomParameters(Resource resource, string res) { var regex = new Regex(@"\<\<([^>]+)\>\>", RegexOptions.IgnoreCase); var matchCollection = regex.Matches(res); foreach (Match match in matchCollection) { if(resource.Type == null) continue; var paramFound = match.Groups[1].Value; var type = resource.GetResourceType(); if (string.IsNullOrWhiteSpace(type) || !resource.Type.ContainsKey(type) || resource.Type[type] == null || !resource.Type[type].ContainsKey(paramFound)) continue; var value = resource.Type[type][paramFound]; res = res.Replace("<<" + paramFound + ">>", value); } return res; }
private string GetReturnTypeFromResourceType(Method method, Resource resource, string key, string responseCode, string fullUrl) { var returnType = string.Empty; if (resource.Type == null || !resource.Type.Any() || !raml.ResourceTypes.Any(rt => rt.ContainsKey(resource.GetResourceType()))) return returnType; var verb = GetResourceTypeVerb(method, resource); if (verb == null || verb.Responses == null || !verb.Responses.Any(r => r != null && r.Body != null && r.Body.Values.Any(m => !string.IsNullOrWhiteSpace(m.Schema)))) return returnType; var response = verb.Responses.FirstOrDefault(r => r.Code == responseCode); if (response == null) return returnType; var resourceTypeMimeType = GeneratorServiceHelper.GetMimeType(response); if (resourceTypeMimeType != null) { returnType = GetReturnTypeFromResponseWithoutCheckingResourceTypes(method, resource, resourceTypeMimeType, key, responseCode, fullUrl); } return returnType; }
protected Verb GetResourceTypeVerb(Method method, Resource resource) { var resourceTypes = raml.ResourceTypes.First(rt => rt.ContainsKey(resource.GetResourceType())); var resourceType = resourceTypes[resource.GetResourceType()]; Verb verb; switch (method.Verb) { case "get": verb = resourceType.Get; break; case "delete": verb = resourceType.Delete; break; case "post": verb = resourceType.Post; break; case "put": verb = resourceType.Put; break; case "patch": verb = resourceType.Patch; break; case "options": verb = resourceType.Options; break; default: throw new InvalidOperationException("Verb not found " + method.Verb); } return verb; }
private string ReplaceParametersWithFunctions(Resource resource, string res, string url) { var regex = new Regex(@"\<\<([a-z_-]+)\s?\|\s?\!(pluralize|singularize)\>\>", RegexOptions.IgnoreCase); var matchCollection = regex.Matches(res); foreach (Match match in matchCollection) { string replacementWord; switch (match.Groups[1].Value) { case "resourcePathName": replacementWord = url.Substring(1); break; case "resourcePath": replacementWord = url.Substring(1); break; case "methodName": replacementWord = url.Substring(1); break; default: var paramFound = match.Groups[1].Value; var type = resource.GetResourceType(); if (string.IsNullOrWhiteSpace(type)) continue; if (!resource.Type[type].ContainsKey(paramFound)) continue; replacementWord = resource.Type[type][paramFound]; break; } if (match.Groups[2].Value == "singularize") { res = res.Replace(match.Groups[0].Value, Singularize(replacementWord)); } else if (match.Groups[2].Value == "pluralize") { res = res.Replace(match.Groups[0].Value, Pluralize(replacementWord)); } } return res; }