Exemple #1
0
        private ControllerMethod BuildControllerMethod(string url, Operation method, 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.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(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, 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);
        }
Exemple #4
0
        private static string GetObjectNameForParameter(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, 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, 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");
        }
Exemple #7
0
        public IEnumerable <ControllerMethod> GetMethods(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);
        }
        protected string GetComment(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);
        }
Exemple #9
0
        public ICollection <ClientGeneratorMethod> GetMethods(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);
        }
Exemple #10
0
        protected string GetUniqueObjectName(Parser.Model.EndPoint resource, 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));
        }
Exemple #11
0
 protected static void GetInheritedUriParams(IDictionary <string, Parameter> parentUriParameters, 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);
         }
     }
 }
 protected GeneratorParameter GetParameter(string key, Operation method, Parser.Model.EndPoint resource, string fullUrl)
 {
     return(requestTypesService.GetRequestParameter(key, method, resource, fullUrl, raml.WebApi.Accepts));
 }