private ApiObject ParseMap(RamlType ramlType, string key) { var name = NetNamingMapper.GetObjectName(key ?? ramlType.Name); var type = ramlType.Object.Properties.First().Value.Type; if (ramlType.Object.Properties.First().Value.Object != null && ramlType.Object.Properties.First().Value.Type == "object") { var itemName = name + "Item"; var nestedObject = ParseObject(itemName, ramlType.Object.Properties.First().Value); type = nestedObject.Name; schemaObjects.Add(itemName, nestedObject); } type = RamlTypesHelper.DecodeRaml1Type(type); if (NetTypeMapper.IsPrimitiveType(type)) { type = NetTypeMapper.Map(type); } return(new ApiObject { Type = name, Name = name, BaseClass = "Dictionary<string," + type + ">", Description = ramlType.Description, Example = GetExample(ramlType.Example, ramlType.Examples), Properties = new Property[0], IsMap = true }); }
private string GetReturnTypeFromResourceType(Method method, Resource resource, string key, string responseCode, string fullUrl) { var returnType = string.Empty; if (resource.Type == null || !resource.Type.Any() || !resourceTypes.Any(rt => rt.ContainsKey(resource.GetSingleType()))) { return(returnType); } var verb = RamlTypesHelper.GetResourceTypeVerb(method, resource, resourceTypes); if (verb == null || verb.Responses == null || !verb.Responses.Any(r => r != null && r.Body != null && r.Body.Values.Any(m => !string.IsNullOrWhiteSpace(m.Schema) && !string.IsNullOrWhiteSpace(m.Type)))) { 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 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 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); }
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); }
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)); }
private ApiObject ParseUnion(string key, RamlType ramlType) { var name = NetNamingMapper.GetObjectName(key); var apiObject = new ApiObject { IsUnionType = true, Name = name, Description = ramlType.Description, Example = GetExample(ramlType.Example, ramlType.Examples), Type = NetNamingMapper.GetObjectName(key) }; var originalType = ramlType.Type; var isArray = false; if (originalType.StartsWith("(") && originalType.EndsWith(")[]")) { isArray = true; originalType = originalType.Substring(0, originalType.Length - 2); } originalType = originalType.Replace("(", string.Empty).Replace(")", string.Empty); var types = originalType.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries); foreach (var type in types) { apiObject.Properties.Add(new Property(name) { Name = NetNamingMapper.GetPropertyName(type.Trim()), Type = isArray ? CollectionTypeHelper.GetCollectionType(RamlTypesHelper.DecodeRaml1Type(type.Trim())) : RamlTypesHelper.DecodeRaml1Type(type.Trim()) }); } return(apiObject); }
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)); }