private string GetParamType(MimeType mimeType, ApiObject apiObject)
        {
            if (mimeType.Type.Contains("<<") && mimeType.Type.Contains(">>"))
            {
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }

            return(DecodeRequestRaml1Type(mimeType.Type));
        }
        private GeneratorParameter CreateGeneratorParameter(ApiObject apiObject)
        {
            var generatorParameter = new GeneratorParameter
            {
                Name        = apiObject.Name.ToLower(),
                Type        = RamlTypesHelper.GetTypeFromApiObject(apiObject),
                Description = apiObject.Description
            };

            return(generatorParameter);
        }
        private string GetReturnTypeFromName(string type)
        {
            var toLower = type.ToLowerInvariant();

            toLower = toLower.Replace(".", string.Empty);

            if (schemaObjects.Values.Any(o => o.Name.ToLowerInvariant() == toLower))
            {
                var apiObject = schemaObjects.Values.First(o => o.Name.ToLowerInvariant() == toLower);
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }

            if (schemaResponseObjects.Values.Any(o => o.Name.ToLowerInvariant() == toLower))
            {
                var apiObject = schemaResponseObjects.Values.First(o => o.Name.ToLowerInvariant() == toLower);
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }

            return(string.Empty);
        }
        private string GetReturnTypeFromParameter(Method method, Resource resource, string fullUrl, string schema)
        {
            var type = schemaParameterParser.Parse(schema, resource, method, fullUrl);

            if (schemaObjects.Values.Any(o => o.Name.ToLowerInvariant() == type.ToLowerInvariant()))
            {
                var apiObject = schemaObjects.Values
                                .First(o => o.Name.ToLowerInvariant() == type.ToLowerInvariant());
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }


            if (schemaResponseObjects.Values.Any(o => o.Name.ToLowerInvariant() == type.ToLowerInvariant()))
            {
                var apiObject = schemaResponseObjects.Values
                                .First(o => o.Name.ToLowerInvariant() == type.ToLowerInvariant());
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }

            return(string.Empty);
        }
示例#5
0
        private string GetType(Parameter param)
        {
            if (param.Type == null)
            {
                return("string");
            }

            if (NetTypeMapper.IsPrimitiveType(param.Type))
            {
                return(NetTypeMapper.GetNetType(param.Type, param.Format) +
                       (NetTypeMapper.GetNetType(param.Type, param.Format) == "string" || param.Required ? "" : "?"));
            }

            var pureType = RamlTypesHelper.ExtractType(param.Type);

            if (schemaObjects.ContainsKey(pureType))
            {
                var apiObject = schemaObjects[pureType];
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }

            return(RamlTypesHelper.DecodeRaml1Type(param.Type));
        }
        public GeneratorParameter GetRequestParameter(string key, Method method, Resource resource, string fullUrl, string defaultMediaType)
        {
            var mimeType = GetMimeType(method.Body, defaultMediaType);

            if (mimeType != null)
            {
                if (mimeType.Type != "object" && !string.IsNullOrWhiteSpace(mimeType.Type))
                {
                    var apiObject = GetRequestApiObjectWhenNamed(method, resource, mimeType.Type, fullUrl);
                    if (apiObject != null)
                    {
                        var generatorParameter = new GeneratorParameter
                        {
                            Name = apiObject.Name.ToLower(),
                            Type = GetParamType(mimeType, apiObject),
                            //Type = DecodeRequestRaml1Type(RamlTypesHelper.GetTypeFromApiObject(apiObject)),
                            Description = apiObject.Description
                        };
                        return(generatorParameter);
                    }
                }
                if (!string.IsNullOrWhiteSpace(mimeType.Schema))
                {
                    var apiObject = GetRequestApiObjectWhenNamed(method, resource, mimeType.Schema, fullUrl);
                    if (apiObject != null)
                    {
                        return(CreateGeneratorParameter(apiObject));
                    }
                }
            }

            if (resource.Type != null && resource.Type.Any() &&
                resourceTypes.Any(rt => rt.ContainsKey(resource.GetSingleType())))
            {
                var verb = RamlTypesHelper.GetResourceTypeVerb(method, resource, resourceTypes);
                if (verb != null && verb.Body != null && !string.IsNullOrWhiteSpace(verb.Body.Schema))
                {
                    var apiObject = GetRequestApiObjectWhenNamed(method, resource, verb.Body.Schema, fullUrl);
                    if (apiObject != null)
                    {
                        return(CreateGeneratorParameter(apiObject));
                    }
                }

                if (verb != null && verb.Body != null && !string.IsNullOrWhiteSpace(verb.Body.Type))
                {
                    var apiObject = GetRequestApiObjectWhenNamed(method, resource, verb.Body.Type, fullUrl);
                    if (apiObject != null)
                    {
                        var generatorParameter = new GeneratorParameter
                        {
                            Name        = apiObject.Name.ToLower(),
                            Type        = DecodeRequestRaml1Type(RamlTypesHelper.GetTypeFromApiObject(apiObject)),
                            Description = apiObject.Description
                        };
                        return(generatorParameter);
                    }

                    return(new GeneratorParameter {
                        Name = "content", Type = DecodeRequestRaml1Type(verb.Body.Type)
                    });
                }
            }

            var apiObjectByKey = GetRequestApiObjectByKey(key);

            if (apiObjectByKey != null)
            {
                return(CreateGeneratorParameter(apiObjectByKey));
            }


            var requestKey = key + GeneratorServiceBase.RequestContentSuffix;

            apiObjectByKey = GetRequestApiObjectByKey(requestKey);
            if (apiObjectByKey != null)
            {
                return(CreateGeneratorParameter(apiObjectByKey));
            }

            if (linkKeysWithObjectNames.ContainsKey(key))
            {
                var linkedKey = linkKeysWithObjectNames[key];
                apiObjectByKey = GetRequestApiObjectByKey(linkedKey);
                if (apiObjectByKey != null)
                {
                    return(CreateGeneratorParameter(apiObjectByKey));
                }
            }

            if (linkKeysWithObjectNames.ContainsKey(requestKey))
            {
                var linkedKey = linkKeysWithObjectNames[requestKey];
                apiObjectByKey = GetRequestApiObjectByKey(linkedKey);
                if (apiObjectByKey != null)
                {
                    return(CreateGeneratorParameter(apiObjectByKey));
                }
            }

            if (mimeType != null)
            {
                string type;
                if (!string.IsNullOrWhiteSpace(mimeType.Type))
                {
                    type = mimeType.Type;
                }
                else
                {
                    type = mimeType.Schema;
                }

                if (!string.IsNullOrWhiteSpace(type))
                {
                    var raml1Type = DecodeRequestRaml1Type(type);

                    if (!string.IsNullOrWhiteSpace(raml1Type))
                    {
                        return new GeneratorParameter {
                                   Name = "content", Type = raml1Type
                        }
                    }
                    ;
                }
            }

            return(new GeneratorParameter {
                Name = "content", Type = "string"
            });
        }
        private string DecodeRequestRaml1Type(string type)
        {
            // TODO: can I handle this better ?
            if (type.Contains("(") || type.Contains("|"))
            {
                return("string");
            }

            if (type.EndsWith("[][]")) // array of arrays
            {
                var subtype = type.Substring(0, type.Length - 4);
                if (NetTypeMapper.IsPrimitiveType(subtype))
                {
                    subtype = NetTypeMapper.Map(subtype);
                }
                else
                {
                    subtype = NetNamingMapper.GetObjectName(subtype);
                }

                return(CollectionTypeHelper.GetCollectionType(CollectionTypeHelper.GetCollectionType(subtype)));
            }

            if (type.EndsWith("[]")) // array
            {
                var subtype = type.Substring(0, type.Length - 2);

                if (NetTypeMapper.IsPrimitiveType(subtype))
                {
                    subtype = NetTypeMapper.Map(subtype);
                }
                else
                {
                    subtype = NetNamingMapper.GetObjectName(subtype);
                }


                return(CollectionTypeHelper.GetCollectionType(subtype));
            }

            if (type.EndsWith("{}")) // Map
            {
                var subtype = type.Substring(0, type.Length - 2);
                var netType = NetTypeMapper.Map(subtype);
                if (!string.IsNullOrWhiteSpace(netType))
                {
                    return("IDictionary<string, " + netType + ">");
                }

                var apiObject = GetRequestApiObjectByName(subtype);
                if (apiObject != null)
                {
                    return("IDictionary<string, " + RamlTypesHelper.GetTypeFromApiObject(apiObject) + ">");
                }

                return("IDictionary<string, object>");
            }

            if (NetTypeMapper.IsPrimitiveType(type))
            {
                return(NetTypeMapper.Map(type));
            }

            if (CollectionTypeHelper.IsCollection(type))
            {
                return(type);
            }

            return(NetNamingMapper.GetObjectName(type));
        }
        private string GetReturnTypeFromResponseByKey(string key)
        {
            var apiObject = schemaObjects.ContainsKey(key) ? schemaObjects[key] : schemaResponseObjects[key];

            return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
        }