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 static string GetObjectNameForParameter(Resource resource) { var objectNameForParameter = resource.RelativeUri.Substring(1).Replace("{", string.Empty).Replace("}", string.Empty); objectNameForParameter = NetNamingMapper.GetObjectName(objectNameForParameter); return(objectNameForParameter); }
public static IList <Property> ConvertHeadersToProperties(IEnumerable <Parameter> headers) { var properties = new List <Property>(); if (headers == null) { return(properties); } foreach (var header in headers) { var description = ParserHelpers.RemoveNewLines(header.Description); var type = NetTypeMapper.Map(header.Type); var typeSuffix = (type == "string" || header.Required ? "" : "?"); properties.Add(new Property { Type = type + typeSuffix, Name = NetNamingMapper.GetPropertyName(header.DisplayName), OriginalName = header.DisplayName, Description = description, Example = header.Example, Required = header.Required }); } return(properties); }
private Property GetPropertyFromScalar(RamlType prop, KeyValuePair <string, RamlType> kv) { if (prop.Scalar.Enum != null && prop.Scalar.Enum.Any()) { if (!enums.ContainsKey(kv.Key)) { var apiEnum = new ApiEnum { Name = NetNamingMapper.GetPropertyName(kv.Key), Description = kv.Value.Description, Values = GetEnumValues(kv.Value.Scalar) }; enums.Add(kv.Key, apiEnum); } } return(new Property { Minimum = ToDouble(prop.Scalar.Minimum), Maximum = ToDouble(prop.Scalar.Maximum), Type = GetPropertyType(prop, kv), MaxLength = prop.Scalar.MaxLength, MinLength = prop.Scalar.MinLength, Name = NetNamingMapper.GetPropertyName(kv.Key), Required = prop.Required || kv.Value.Scalar.Required, Example = prop.Example, Description = prop.Description, IsEnum = prop.Scalar.Enum != null && prop.Scalar.Enum.Any(), OriginalName = kv.Key.TrimEnd('?') }); }
private string GetScalarType(RamlType ramlType) { var type = NetTypeMapper.GetNetType(ramlType.Scalar.Type, ramlType.Scalar.Format); if (type != null) { return(type); } if (!ramlTypes.ContainsKey(ramlType.Scalar.Type)) { return("object"); } var subRamlType = ramlTypes[ramlType.Scalar.Type]; if (subRamlType.Scalar == null) { return(NetNamingMapper.GetObjectName(ramlType.Scalar.Type)); } type = GetScalarType(subRamlType); if (type != null) { return(type); } throw new InvalidOperationException("Cannot determine type of scalar " + ramlType.Name); }
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 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 = responseTypesService.GetResponseType(method, resource, mimeType, key, response.Code, fullUrl); if (string.IsNullOrWhiteSpace(type)) { return; } var name = NetNamingMapper.GetPropertyName(CollectionTypeHelper.GetBaseType(type)); if (properties.Any(p => p.Name == name)) { name = name + response.Code; } var property = new Property { Name = name, 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); }
private ApiObject ParseArray(string key, RamlType ramlType) { var typeOfArray = GetTypeOfArray(key, ramlType); var baseType = CollectionTypeHelper.GetBaseType(typeOfArray); if (!NetTypeMapper.IsPrimitiveType(baseType) && ramlType.Array.Items != null && ramlType.Array.Items.Type == "object") { if (baseType == typeOfArray) { baseType = typeOfArray + "Item"; } baseType = NetNamingMapper.GetObjectName(baseType); var itemType = ParseNestedType(ramlType.Array.Items, baseType); schemaObjects.Add(baseType, itemType); typeOfArray = CollectionTypeHelper.GetCollectionType(baseType); } return(new ApiObject { IsArray = true, Name = NetNamingMapper.GetObjectName(key), Description = ramlType.Description, Example = ramlType.Example, Type = typeOfArray }); }
private string HandleMultipleSchemaType(IEnumerable <Response> responses, Resource resource, Method method, string key, string fullUrl) { var properties = GetProperties(responses, resource, method, key, fullUrl); if (properties.Count == 0) { return("string"); } if (properties.Count == 1) { return(properties.First().Type); } // Build a new response object containing all types 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); }
protected string GetUniqueName(ICollection <string> methodsNames, string methodName, string relativeUri) { var nameWithResource = NetNamingMapper.GetMethodName(methodName + relativeUri); if (!methodsNames.Contains(nameWithResource)) { return(nameWithResource); } for (var i = 0; i < 7; i++) { var unique = methodName + suffixes[i]; if (!methodsNames.Contains(unique)) { return(unique); } } for (var i = 0; i < 100; i++) { var unique = methodName + i; if (!methodsNames.Contains(unique)) { return(unique); } } throw new InvalidOperationException("Could not find a unique name for method " + methodName); }
protected string GetUniqueObjectName(Resource resource, Resource parent) { string objectName; if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}")) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(resource)); } else { objectName = NetNamingMapper.GetObjectName(resource.RelativeUri); if (classesNames.Contains(objectName)) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(resource)); } } if (string.IsNullOrWhiteSpace(objectName)) { throw new InvalidOperationException("object name is null for " + resource.RelativeUri); } if (!classesNames.Contains(objectName)) { return(objectName); } if (parent == null || string.IsNullOrWhiteSpace(parent.RelativeUri)) { return(GetUniqueObjectName(objectName)); } if (resource.RelativeUri.StartsWith("/{") && parent.RelativeUri.EndsWith("}")) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(parent)) + objectName; } else { objectName = NetNamingMapper.GetObjectName(parent.RelativeUri) + objectName; if (classesNames.Contains(objectName)) { objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(parent)); } } if (string.IsNullOrWhiteSpace(objectName)) { throw new InvalidOperationException("object name is null for " + resource.RelativeUri); } if (!classesNames.Contains(objectName)) { return(objectName); } return(GetUniqueObjectName(objectName)); }
private static Property ConvertGeneratorParamToProperty(GeneratorParameter p) { return(new Property { Name = NetNamingMapper.Capitalize(p.Name), Description = p.Description, Type = p.Type, Required = true }); }
public void Generate(Resource resource, string url, ClientGeneratorMethod clientGeneratorMethod, IDictionary <string, ApiObject> uriParameterObjects, IDictionary <string, Parameter> parentUriParameters) { var parameters = GetUriParameters(resource, url, parentUriParameters).ToArray(); clientGeneratorMethod.UriParameters = parameters; if (!parameters.Any()) { return; } var name = NetNamingMapper.GetObjectName(url) + "UriParameters"; clientGeneratorMethod.UriParametersType = name; if (uriParameterObjects.ContainsKey(name)) { return; } var properties = new List <Property>(); if (resource.BaseUriParameters != null) { properties.AddRange(QueryParametersParser.ConvertParametersToProperties(resource.BaseUriParameters)); } if (resource.UriParameters != null) { properties.AddRange(QueryParametersParser.ConvertParametersToProperties(resource.UriParameters) .Where(up => properties.All(p => !String.Equals(up.Name, p.Name, StringComparison.InvariantCultureIgnoreCase)))); } var urlParameters = ExtractParametersFromUrl(url).ToArray(); var matchedParameters = MatchParameters(parentUriParameters, urlParameters); foreach (var urlParameter in matchedParameters) { var property = ConvertGeneratorParamToProperty(urlParameter); if (properties.All(p => !String.Equals(property.Name, p.Name, StringComparison.InvariantCultureIgnoreCase))) { properties.Add(property); } } var apiObject = new ApiObject { Name = name, Description = "Uri Parameters for resource " + resource.RelativeUri, Properties = properties }; uriParameterObjects.Add(name, apiObject); }
public static string DecodeRaml1Type(string type) { // TODO: can I handle this better ? if (type.Contains("(") || type.Contains("|")) { return("object"); } if (type.EndsWith("[][]")) // array of arrays { var strippedType = type.Substring(0, type.Length - 4); if (NetTypeMapper.Map(strippedType) == null) { strippedType = NetNamingMapper.GetObjectName(strippedType); } var decodeRaml1Type = CollectionTypeHelper.GetCollectionType(CollectionTypeHelper.GetCollectionType(strippedType)); return(decodeRaml1Type); } if (type.EndsWith("[]")) // array { var strippedType = type.Substring(0, type.Length - 2); if (NetTypeMapper.Map(strippedType) == null) { strippedType = NetNamingMapper.GetObjectName(strippedType); } var decodeRaml1Type = CollectionTypeHelper.GetCollectionType(strippedType); return(decodeRaml1Type); } if (type.EndsWith("{}")) // Map { var subtype = type.Substring(0, type.Length - 2); var netType = NetTypeMapper.Map(subtype); if (netType != null) { return("IDictionary<string, " + netType + ">"); } return("IDictionary<string, " + NetNamingMapper.GetObjectName(subtype) + ">"); } if (CollectionTypeHelper.IsCollection(type)) { return(type); } return(NetNamingMapper.GetObjectName(type)); }
public ApiObject ParseObject(string key, string value, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings) { var obj = ParseSchema(key, value, objects, warnings); if (obj == null) { return(null); } obj.Name = NetNamingMapper.GetObjectName(key); return(obj); }
private void ParseComplexTypes(IDictionary <string, ApiObject> objects, JsonSchema schema, Property prop, KeyValuePair <string, JsonSchema> property, string key) { if (schema.Type == JsonSchemaType.Object && schema.Properties != null) { ParseObject(key, schema.Properties, objects); prop.Type = NetNamingMapper.GetObjectName(key); } if (schema.Type == JsonSchemaType.Array) { ParseArray(objects, schema, prop, property); } }
public static string GetPropertyName(string name) { var propName = name.Replace(":", string.Empty); propName = propName.Replace("/", string.Empty); propName = propName.Replace("-", string.Empty); propName = NetNamingMapper.Capitalize(propName); if (StartsWithNumber(propName)) { propName = "P" + propName; } return(propName); }
private void ParseObject(string key, IDictionary <string, Newtonsoft.JsonV4.Schema.JsonSchema> schema, IDictionary <string, ApiObject> objects) { var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = ParseSchema(schema, objects) }; // Avoid duplicated keys and names if (objects.ContainsKey(key) || objects.Any(o => o.Value.Name == obj.Name) || !obj.Properties.Any()) { return; } objects.Add(key, obj); }
private void ParseArray(IDictionary <string, ApiObject> objects, Newtonsoft.JsonV4.Schema.JsonSchema schema, Property prop, KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property) { var netType = NetTypeMapper.Map(schema.Items.First().Type); if (netType != null) { prop.Type = netType + "[]"; } else { prop.Type = NetNamingMapper.GetObjectName(property.Key) + "[]"; foreach (var item in schema.Items) { ParseObject(property.Key, item.Properties, objects); } } }
private IList <Property> ParseSchema(IDictionary <string, Newtonsoft.JsonV4.Schema.JsonSchema> schema, IDictionary <string, ApiObject> objects) { var props = new List <Property>(); foreach (var kv in schema) { var prop = new Property { Name = NetNamingMapper.GetPropertyName(kv.Key), Type = NetTypeMapper.Map(kv.Value.Type), Description = kv.Value.Description }; ParseComplexTypes(objects, kv.Value, prop, kv); props.Add(prop); } return(props); }
private static string GetTypeOfArray(string key, RamlType ramlType) { if (!string.IsNullOrWhiteSpace(ramlType.Type)) { var pureType = ramlType.Type.EndsWith("[]") ? ramlType.Type.Substring(0, ramlType.Type.Length - 2) : ramlType.Type; if (pureType != "array" && pureType != "object") { if (NetTypeMapper.IsPrimitiveType(pureType)) { pureType = NetTypeMapper.Map(pureType); } else { pureType = NetNamingMapper.GetObjectName(pureType); } return(CollectionTypeHelper.GetCollectionType(pureType)); } } if (!string.IsNullOrWhiteSpace(ramlType.Array.Items.Type)) { if (ramlType.Array.Items.Type != "object") { var netType = ramlType.Array.Items.Type; if (NetTypeMapper.IsPrimitiveType(netType)) { netType = NetTypeMapper.Map(netType); } else { netType = NetNamingMapper.GetObjectName(netType); } return(CollectionTypeHelper.GetCollectionType(netType)); } } if (!string.IsNullOrWhiteSpace(ramlType.Array.Items.Name)) { return(CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(ramlType.Array.Items.Name))); } return(NetNamingMapper.GetObjectName(key)); }
private ApiObject ParseScalar(string key, RamlType ramlType) { if (ramlType.Scalar.Enum != null && ramlType.Scalar.Enum.Any()) { if (enums.ContainsKey(key)) { return(null); } enums.Add(key, new ApiEnum { Name = NetNamingMapper.GetObjectName(key), Description = ramlType.Description, Values = GetEnumValues(ramlType.Scalar, NetNamingMapper.GetObjectName(key)) }); return(null); } var type = GetScalarType(ramlType); var name = NetNamingMapper.GetObjectName(key); return(new ApiObject { Type = NetNamingMapper.GetObjectName(key), Name = name, Example = ramlType.Example, Description = ramlType.Description, Properties = new List <Property> { new Property(name) { Name = "Value", Type = type, Minimum = (double?)ramlType.Scalar.Minimum, Maximum = (double?)ramlType.Scalar.Maximum, MinLength = ramlType.Scalar.MinLength, MaxLength = ramlType.Scalar.MaxLength, OriginalName = key, Required = ramlType.Scalar.Required || ramlType.Required } }, IsScalar = true, }); }
private ApiObject GetApiObjectFromObject(RamlType ramlType, string defaultName = "") { var name = defaultName == "" ? ramlType.Name : defaultName; if (string.IsNullOrWhiteSpace(name)) { name = "Type" + DateTime.Now.Ticks; } return(new ApiObject { Type = NetNamingMapper.GetObjectName(name), Name = NetNamingMapper.GetObjectName(name), BaseClass = ramlType.Type != "object" ? NetNamingMapper.GetObjectName(ramlType.Type) : string.Empty, Description = ramlType.Description, Example = GetExample(ramlType.Example, ramlType.Examples), Properties = GetProperties(ramlType.Object.Properties) }); }
private string GetPropertyType(RamlType prop, KeyValuePair <string, RamlType> kv) { if (string.IsNullOrWhiteSpace(prop.Type)) { return("string"); } if (prop.Type == "object" || (prop.Scalar.Enum != null && prop.Scalar.Enum.Any())) { return(NetNamingMapper.GetPropertyName(kv.Key)); } var propertyType = NetTypeMapper.GetNetType(prop.Scalar.Type, prop.Scalar.Format); if (propertyType != null) { if (!prop.Required && !prop.Scalar.Required && propertyType != "string" && prop.Type != "file") { return(propertyType + "?"); } return(propertyType); } if (schemaObjects.ContainsKey(prop.Type)) { var obj = schemaObjects[prop.Type]; if (obj.IsScalar) { return(obj.Properties.First().Type); } return(obj.Type); } if (enums.ContainsKey(prop.Type)) { return(prop.Type); } return("object"); }
public IList <Property> ConvertParametersToProperties(IEnumerable <KeyValuePair <string, Parameter> > parameters) { var properties = new List <Property>(); foreach (var parameter in parameters.Where(parameter => parameter.Value != null && parameter.Value.Type != null)) { var description = ParserHelpers.RemoveNewLines(parameter.Value.Description); properties.Add(new Property { Type = GetType(parameter.Value), Name = NetNamingMapper.GetPropertyName(parameter.Key), OriginalName = parameter.Key, Description = description, Example = parameter.Value.Example, Required = parameter.Value.Required }); } return(properties); }
private void ParseProperties(IDictionary <string, ApiObject> objects, IList <Property> props, IDictionary <string, Newtonsoft.JsonV4.Schema.JsonSchema> properties) { foreach (var property in properties) { if (property.Value.Type == null || property.Value.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Null || property.Value.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.None) { continue; } var prop = new Property { Name = NetNamingMapper.GetPropertyName(property.Key), Type = NetTypeMapper.Map(property.Value.Type), Description = property.Value.Description }; ParseComplexTypes(objects, property.Value, prop, property); props.Add(prop); } }
public static IList <Property> ConvertParametersToProperties(IEnumerable <KeyValuePair <string, Parameter> > parameters) { var properties = new List <Property>(); foreach (var parameter in parameters) { var description = ParserHelpers.RemoveNewLines(parameter.Value.Description); properties.Add(new Property { Type = NetTypeMapper.Map(parameter.Value.Type) + (NetTypeMapper.Map(parameter.Value.Type) == "string" || parameter.Value.Required ? "" : "?"), Name = NetNamingMapper.GetPropertyName(parameter.Key), Description = description, Example = parameter.Value.Example, Required = parameter.Value.Required }); } return(properties); }
private ApiObject ParseArray(string key, RamlType ramlType) { var typeOfArray = GetTypeOfArray(key, ramlType); var baseType = CollectionTypeHelper.GetBaseType(typeOfArray); if (!NetTypeMapper.IsPrimitiveType(baseType) && ramlType.Array.Items != null && ramlType.Array.Items.Type == "object") { if (baseType == typeOfArray) { baseType = typeOfArray + "Item"; } baseType = NetNamingMapper.GetObjectName(baseType); var itemType = ParseNestedType(ramlType.Array.Items, baseType); if (schemaObjects.ContainsKey(baseType) && !UniquenessHelper.HasSameProperties(itemType, schemaObjects, baseType, emptyDic, emptyDic)) { baseType = UniquenessHelper.GetUniqueName(schemaObjects, baseType, emptyDic, emptyDic); itemType.Name = baseType; } if (!schemaObjects.ContainsKey(baseType)) { schemaObjects.Add(baseType, itemType); } typeOfArray = CollectionTypeHelper.GetCollectionType(baseType); } return(new ApiObject { IsArray = true, Name = NetNamingMapper.GetObjectName(key), Description = ramlType.Description, Example = ramlType.Example, Type = typeOfArray }); }
public static IList <Property> ConvertHeadersToProperties(IDictionary <string, Parameter> headers) { var properties = new List <Property>(); if (headers == null) { return(properties); } foreach (var header in headers) { var description = ParserHelpers.RemoveNewLines(header.Value.Description); var type = "string"; var typeSuffix = ""; if (header.Value.Type != null && header.Value.Type.Contains("|")) { type = "object"; } else { type = NetTypeMapper.GetNetType(header.Value.Type, header.Value.Format); typeSuffix = (type == "string" || header.Value.Required ? "" : "?"); } properties.Add(new Property { Type = type + typeSuffix, Name = NetNamingMapper.GetPropertyName(header.Key), OriginalName = header.Value.DisplayName, Description = description, Example = header.Value.Example, Required = header.Value.Required }); } return(properties); }
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); }