Beispiel #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(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 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 IDictionary <string, ApiObject> GetResponseObjects()
        {
            var objects = new Dictionary <string, ApiObject>();

            if (raml.WebApi == null)
            {
                return(objects);
            }

            foreach (var endpoint in raml.WebApi.EndPoints)
            {
                if (endpoint.Operations == null)
                {
                    continue;
                }

                foreach (var operation in endpoint.Operations)
                {
                    if (operation.Responses == null)
                    {
                        continue;
                    }

                    foreach (var response in operation.Responses)
                    {
                        if (response == null && response.Payloads == null)
                        {
                            continue;
                        }

                        var key = GeneratorServiceHelper.GetKeyForResource(operation, endpoint, response);
                        GetShapes(key, objects, response.Payloads);
                    }
                }
            }
            return(objects);
        }