Пример #1
0
        private string GetReturnTypeFromResponseByKey(string key)
        {
            ApiObject apiObject = null;

            if (schemaObjects.ContainsKey(key))
            {
                apiObject = schemaObjects[key];
            }

            if (schemaResponseObjects.ContainsKey(key))
            {
                apiObject = schemaResponseObjects[key];
            }

            var nameKey = NetNamingMapper.GetObjectName(key);

            if (schemaObjects.ContainsKey(nameKey))
            {
                apiObject = schemaObjects[nameKey];
            }

            if (schemaResponseObjects.ContainsKey(nameKey))
            {
                apiObject = schemaResponseObjects[nameKey];
            }

            return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
        }
Пример #2
0
        //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);
        }
Пример #3
0
        private ApiObject GetRequestApiObjectByName(string schema)
        {
            var type = RamlTypesHelper.ExtractType(schema.ToLowerInvariant());

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

            if (schemaRequestObjects.Values.Any(o => o.Name.ToLowerInvariant() == type))
            {
                return(schemaRequestObjects.Values.First(o => o.Name.ToLowerInvariant() == type));
            }

            return(null);
        }
        public string GetResponseType(Operation method, EndPoint resource, Payload mimeType, string key, string responseCode, string fullUrl)
        {
            string returnType = null;

            returnType = GetNamedReturnType(method, resource, mimeType, fullUrl);

            if (!string.IsNullOrWhiteSpace(returnType) && RamlTypesHelper.IsPrimitiveOrSchemaObject(returnType, schemaObjects))
            {
                return(returnType);
            }

            if (ResponseHasKey(key))
            {
                return(GetReturnTypeFromResponseByKey(key));
            }

            var responseKey = key + ParserHelpers.GetStatusCode(responseCode) + GeneratorServiceBase.ResponseContentSuffix;

            if (ResponseHasKey(responseKey))
            {
                return(GetReturnTypeFromResponseByKey(responseKey));
            }

            if (linkKeysWithObjectNames.ContainsKey(key))
            {
                var linkedKey = linkKeysWithObjectNames[key];
                if (ResponseHasKey(linkedKey))
                {
                    return(GetReturnTypeFromResponseByKey(linkedKey));
                }
            }

            if (linkKeysWithObjectNames.ContainsKey(responseKey))
            {
                var linkedKey = linkKeysWithObjectNames[responseKey];
                if (ResponseHasKey(linkedKey))
                {
                    return(GetReturnTypeFromResponseByKey(linkedKey));
                }
            }

            returnType = DecodeResponseRaml1Type(returnType);
            return(returnType);
        }
Пример #5
0
        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(Operation method, EndPoint 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);
        }
        private string GetType(Parameter param)
        {
            if (param.Schema == null)
            {
                return("string");
            }

            if (param.Schema is ScalarShape scalar)
            {
                return(NewNetTypeMapper.GetNetType(scalar) +
                       (NewNetTypeMapper.GetNetType(scalar) == "string" || NewNetTypeMapper.GetNetType(scalar) == "object" || param.Required ? "" : "?"));
            }

            var pureType = param.Schema.Name;

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

            return(pureType);
        }
Пример #8
0
        public GeneratorParameter GetRequestParameter(string key, Operation method, EndPoint resource, string fullUrl, IEnumerable <string> defaultMediaTypes)
        {
            if (method.Request == null || !method.Request.Payloads.Any())
            {
                return new GeneratorParameter {
                           Name = "content", Type = "string"
                }
            }
            ;

            var mimeType = GetMimeType(method.Request.Payloads, defaultMediaTypes);
            var type     = NewNetTypeMapper.GetNetType(mimeType, schemaObjects, null, enums);

            if (RamlTypesHelper.IsPrimitiveOrSchemaObject(type, schemaObjects))
            {
                return(new GeneratorParameter //TODO: check
                {
                    Name = string.IsNullOrWhiteSpace(mimeType.Name) ? GetParameterName(type) : GetParameterName(mimeType.Name),
                    Description = mimeType.Description,
                    Type = type
                });
            }

            var apiObjectByKey = GetRequestApiObjectByKey(key);

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

            apiObjectByKey = GetRequestApiObjectByKey(NetNamingMapper.GetObjectName(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));
                }
            }

            return(new GeneratorParameter {
                Name = "content", Type = "string"
            });

            //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) };
            //    }
            //}

            //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" };
        }
Пример #9
0
        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 (NewNetTypeMapper.IsPrimitiveType(subtype))
                {
                    subtype = NewNetTypeMapper.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 (NewNetTypeMapper.IsPrimitiveType(subtype))
                {
                    subtype = NewNetTypeMapper.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 = NewNetTypeMapper.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 (NewNetTypeMapper.IsPrimitiveType(type))
            {
                return(NewNetTypeMapper.Map(type));
            }

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

            return(NetNamingMapper.GetObjectName(type));
        }
Пример #10
0
        public GeneratorParameter GetRequestParameter(string key, Operation method, EndPoint resource, string fullUrl, IEnumerable <string> defaultMediaTypes)
        {
            if (method.Request == null || !method.Request.Payloads.Any())
            {
                return new GeneratorParameter {
                           Name = "content", Type = "string"
                }
            }
            ;

            var mimeType = GetMimeType(method.Request.Payloads, defaultMediaTypes);
            var type     = NewNetTypeMapper.GetNetType(mimeType, schemaObjects, schemaRequestObjects, enums);

            if (RamlTypesHelper.IsPrimitiveOrSchemaObject(type, schemaObjects) || RamlTypesHelper.IsPrimitiveOrSchemaObject(type, schemaRequestObjects))
            {
                return(new GeneratorParameter
                {
                    Name = string.IsNullOrWhiteSpace(mimeType.Name) ? GetParameterName(type) : GetParameterName(mimeType.Name),
                    Description = mimeType.Description,
                    Type = type
                });
            }

            //var apiObjectByKey = GetRequestApiObjectByKey(key);
            //if (apiObjectByKey != null)
            //    return CreateGeneratorParameter(apiObjectByKey);

            //apiObjectByKey = GetRequestApiObjectByKey(NetNamingMapper.GetObjectName(key));
            //if (apiObjectByKey != null)
            //    return CreateGeneratorParameter(apiObjectByKey);

            var requestKey = key + GeneratorServiceBase.RequestContentSuffix;
            //apiObjectByKey = GetRequestApiObjectByKey(requestKey);
            //if (apiObjectByKey != null)
            //    return CreateGeneratorParameter(apiObjectByKey);
            ApiObject 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));
                }
            }

            return(new GeneratorParameter {
                Name = "content", Type = "string"
            });
        }