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);
        }
        private void AddProperty(Resource resource, Method method, string key, Response response, ICollection <Property> properties, string fullUrl)
        {
            var mimeType = GeneratorServiceHelper.GetMimeType(response);

            if (mimeType == null)
            {
                return;
            }

            var type = GetReturnTypeFromResponse(method, resource, mimeType, key, response.Code, fullUrl);

            if (string.IsNullOrWhiteSpace(type))
            {
                return;
            }

            var property = new Property
            {
                Name        = CollectionTypeHelper.GetBaseType(type),
                Description = response.Description + " " + mimeType.Description,
                Example     = mimeType.Example,
                Type        = type,
                StatusCode  = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), response.Code),
                JSONSchema  = mimeType.Schema == null ? null : mimeType.Schema.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\n", "").Replace("\"", "\\\"")
            };

            properties.Add(property);
        }
Пример #3
0
        private void ParseResponses(KeyValuePair <string, ResourceType> type, Verb verb)
        {
            if (verb.Responses == null)
            {
                return;
            }

            foreach (var response in verb.Responses.Where(response => response != null))
            {
                var name = Enum.GetName(typeof(VerbType), verb.Type);
                if (name == null)
                {
                    continue;
                }

                var key = type.Key + "-" + name.ToLower() + ParserHelpers.GetStatusCode(response.Code) + ResponseContentSuffix;

                if (response.Body == null || !response.Body.Any(b => b.Value != null && !string.IsNullOrWhiteSpace(b.Value.Schema)))
                {
                    continue;
                }

                var mimeType = GeneratorServiceHelper.GetMimeType(response);

                var obj = objectParser.ParseObject(key, mimeType.Schema, schemaResponseObjects, warnings, enums, schemaRequestObjects, schemaObjects, targetNamespace);

                AddObjectToObjectCollectionOrLink(obj, key, schemaResponseObjects, schemaObjects);
            }
        }
        private void ParseResourceResponsesRecursively(IDictionary <string, ApiObject> objects, IEnumerable <Resource> resources)
        {
            foreach (var resource in resources)
            {
                if (resource.Methods != null)
                {
                    foreach (var method in resource.Methods.Where(m => m.Responses != null && m.Responses.Any()))
                    {
                        foreach (var response in method.Responses.Where(r => r.Body != null && r.Body.Any()))
                        {
                            foreach (var kv in response.Body.Where(b => b.Value.Schema != null))
                            {
                                var key = GeneratorServiceHelper.GetKeyForResource(method, resource) + ParserHelpers.GetStatusCode(response.Code) + ResponseContentSuffix;
                                if (objects.ContainsKey(key))
                                {
                                    continue;
                                }

                                var obj = objectParser.ParseObject(key, kv.Value.Schema, objects, warnings);

                                // Avoid duplicated names and objects without properties
                                if (obj != null && objects.All(o => o.Value.Name != obj.Name) && obj.Properties.Any())
                                {
                                    objects.Add(key, obj);
                                }
                            }
                        }
                    }
                }
                if (resource.Resources != null)
                {
                    ParseResourceResponsesRecursively(objects, resource.Resources);
                }
            }
        }
        private void ParseResponses(IDictionary <string, ApiObject> objects, KeyValuePair <string, ResourceType> type, Verb verb)
        {
            if (verb.Responses == null)
            {
                return;
            }

            foreach (var response in verb.Responses.Where(response => response != null))
            {
                var name = Enum.GetName(typeof(VerbType), verb.Type);
                if (name == null)
                {
                    continue;
                }

                var key = type.Key + "-" + name.ToLower() + ParserHelpers.GetStatusCode(response.Code) + ResponseContentSuffix;

                if (response.Body == null || !response.Body.Any(b => b.Value != null && !string.IsNullOrWhiteSpace(b.Value.Schema)))
                {
                    continue;
                }

                var mimeType = GeneratorServiceHelper.GetMimeType(response);

                var obj = objectParser.ParseObject(key, mimeType.Schema, objects, warnings);
                // Avoid duplicated keys and names
                if (obj != null && !objects.ContainsKey(key) && objects.All(o => o.Value.Name != obj.Name) && obj.Properties.Any())
                {
                    objects.Add(key, obj);
                }
            }
        }
Пример #6
0
        private ControllerMethod BuildControllerMethod(string url, Method method, Resource resource, ControllerObject parent, IDictionary <string, Parameter> parentUriParameters)
        {
            var relativeUri = UrlGeneratorHelper.GetRelativeUri(url, parent.PrefixUri);

            var parentUrl = UrlGeneratorHelper.GetParentUri(url, resource.RelativeUri);

            string b = resource.Methods.FirstOrDefault(m => m.Verb == method.Verb && m.SecuredBy != null && m.SecuredBy.Any()).SecuredBy.FirstOrDefault();

            return(new ControllerMethod
            {
                Name = NetNamingMapper.GetMethodName(method.Verb ?? "Get" + resource.RelativeUri),
                Parameter = GetParameter(GeneratorServiceHelper.GetKeyForResource(method, resource, parentUrl), method, resource, url),
                UriParameters = uriParametersGenerator.GetUriParameters(resource, url, parentUriParameters),
                ReturnType = GetReturnType(GeneratorServiceHelper.GetKeyForResource(method, resource, parentUrl), method, resource, url),
                Comment = GetComment(resource, method, url),
                Url = relativeUri,
                Verb = NetNamingMapper.Capitalize(method.Verb),
                Parent = null,
                UseSecurity =
                    raml.SecuredBy != null && raml.SecuredBy.Any() ||
                    resource.Methods.Any(m => m.Verb == method.Verb && m.SecuredBy != null && m.SecuredBy.Any()),
                SecuredBy =
                    raml.SecuredBy ??
                    resource.Methods.FirstOrDefault(m => m.Verb == method.Verb && m.SecuredBy != null && m.SecuredBy.Any()).SecuredBy,
                SecurityParameters = GetSecurityParameters(raml, method)
            });
        }
        private void AddProperty(Resource resource, Method method, string key, IDictionary <string, ApiObject> schemaResponseObjects,
                                 Response response, List <Property> properties)
        {
            var mimeType = GeneratorServiceHelper.GetMimeType(response);

            if (mimeType == null)
            {
                return;
            }

            var type = GetReturnTypeFromResponse(method, resource, mimeType, key, response.Code, schemaResponseObjects);

            if (string.IsNullOrWhiteSpace(type))
            {
                return;
            }

            var property = new Property
            {
                Name        = type.Replace("[]", string.Empty),
                Description = response.Description + " " + mimeType.Description,
                Example     = mimeType.Example,
                Type        = type,
                StatusCode  = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), response.Code)
            };

            properties.Add(property);
        }
        private void ParseResourceResponsesRecursively(IEnumerable <Resource> resources, string fullUrl)
        {
            foreach (var resource in resources)
            {
                if (resource.Methods != null)
                {
                    foreach (var method in resource.Methods.Where(m => m.Responses != null && m.Responses.Any()))
                    {
                        foreach (var response in method.Responses.Where(r => r.Body != null && r.Body.Any()))
                        {
                            foreach (var kv in response.Body.Where(b => b.Value.Schema != null))
                            {
                                var key = GeneratorServiceHelper.GetKeyForResource(method, resource, fullUrl) + ParserHelpers.GetStatusCode(response.Code) + ResponseContentSuffix;
                                if (schemaResponseObjects.ContainsKey(key))
                                {
                                    continue;
                                }

                                var obj = objectParser.ParseObject(key, kv.Value.Schema, schemaResponseObjects, warnings, enums, schemaRequestObjects, schemaObjects, targetNamespace);

                                AddObjectToObjectCollectionOrLink(obj, key, schemaResponseObjects, schemaObjects);
                            }
                        }
                    }
                }
                if (resource.Resources != null)
                {
                    ParseResourceResponsesRecursively(resource.Resources, fullUrl + resource.RelativeUri);
                }
            }
        }
Пример #9
0
        private void ParseResourceRequestsRecursively(IEnumerable <Resource> resources, string fullUrl)
        {
            foreach (var resource in resources)
            {
                if (resource.Methods != null)
                {
                    var methods = resource.Methods.Where(m => m.Body != null && m.Body.Any()).ToList();

                    foreach (var method in methods)
                    {
                        foreach (var kv in method.Body.Where(b => b.Value != null && b.Value.Schema != null))
                        {
                            var key = GeneratorServiceHelper.GetKeyForResource(method, resource, fullUrl) + RequestContentSuffix;
                            if (schemaRequestObjects.ContainsKey(key))
                            {
                                continue;
                            }

                            var obj = objectParser.ParseObject(key, kv.Value.Schema, schemaRequestObjects, warnings, enums, schemaResponseObjects, schemaObjects, targetNamespace);

                            AddObjectToObjectCollectionOrLink(obj, key, schemaRequestObjects, schemaObjects);
                        }
                        foreach (var kv in method.Body.Where(b => b.Value != null && b.Value.InlineType != null))
                        {
                            var key = GeneratorServiceHelper.GetKeyForResource(method, resource, fullUrl) + RequestContentSuffix;
                            if (schemaRequestObjects.ContainsKey(key))
                            {
                                continue;
                            }

                            raml1TypesParser = new RamlTypeParser(raml.Types, schemaObjects, targetNamespace, enums, warnings);
                            var obj = raml1TypesParser.ParseInline(key, kv.Value.InlineType, schemaRequestObjects);

                            AddObjectToObjectCollectionOrLink(obj, key, schemaRequestObjects, schemaObjects);
                        }
                    }
                }
                if (resource.Resources != null)
                {
                    ParseResourceRequestsRecursively(resource.Resources, fullUrl + resource.RelativeUri);
                }
            }
        }