private void AddObjectToObjectCollectionOrLink(ApiObject obj, string key, IDictionary <string, ApiObject> objects, IDictionary <string, ApiObject> otherObjects) { if (obj == null || (!obj.Properties.Any() && obj.Type == null)) { return; } if (schemaObjects.All(o => o.Value.Name != obj.Name) && objects.All(o => o.Value.Name != obj.Name) && otherObjects.All(o => o.Value.Name != obj.Name)) { objects.Add(key, obj); } else { if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects)) { if (string.IsNullOrWhiteSpace(obj.GeneratedCode) && !linkKeysWithObjectNames.ContainsKey(key)) { linkKeysWithObjectNames.Add(key, obj.Name); } } else if (!objects.ContainsKey(key)) { obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, schemaObjects, schemaObjects); objects.Add(key, obj); } } }
public static string GetTypeFromApiObject(ApiObject apiObject) { if (apiObject.IsArray) { if (string.IsNullOrWhiteSpace(apiObject.Type)) { return(CollectionTypeHelper.GetCollectionType(apiObject.Name)); } if (CollectionTypeHelper.IsCollection(apiObject.Type)) { return(apiObject.Type); } return(CollectionTypeHelper.GetCollectionType(apiObject.Type)); } if (apiObject.IsMap) { return(apiObject.Type); } if (apiObject.IsScalar) { return(apiObject.Properties.First().Type); } if (!string.IsNullOrWhiteSpace(apiObject.Type) && apiObject.Type != apiObject.Name) { return(apiObject.Type); } return(apiObject.Name); }
public static ApiObject GetQueryObject(ClientGeneratorMethod generatedMethod, Method method, string objectName) { var queryObject = new ApiObject { Name = generatedMethod.Name + objectName + "Query" }; queryObject.Properties = ParseParameters(method); return queryObject; }
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); }
public static bool HasSameProperties(ApiObject apiObject, IDictionary <string, ApiObject> objects, string key, IDictionary <string, ApiObject> otherObjects, IDictionary <string, ApiObject> schemaObjects) { var obj = FindObjectWithSameProperties(apiObject, key, objects, otherObjects, schemaObjects); if (obj == null) { throw new InvalidOperationException("Could not find object with key " + key); } if (!string.IsNullOrWhiteSpace(obj.GeneratedCode)) { if (objects.Any(o => o.Value.GeneratedCode == obj.GeneratedCode) || otherObjects.Any(o => o.Value.GeneratedCode == obj.GeneratedCode) || schemaObjects.Any(o => o.Value.GeneratedCode == obj.GeneratedCode)) { return(true); } } if (obj.Properties.Count != apiObject.Properties.Count) { return(false); } return(apiObject.Properties.All(property => obj.Properties.Any(p => p.Name == property.Name && p.Type == property.Type))); }
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 bool IsUsedAsReferenceInAnyObject(ApiObject requestObj) { return schemaRequestObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) || x.Type == requestObj.BaseClass || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass)) || schemaResponseObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) || x.Type == requestObj.BaseClass || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass)); }
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 ApiObject GetQueryObject(ClientGeneratorMethod generatedMethod, Method method, string objectName) { var queryObject = new ApiObject { Name = generatedMethod.Name + objectName + "Query" }; queryObject.Properties = ParseParameters(method); return(queryObject); }
public ApiObject Parse(string key, string jsonSchema, IDictionary<string, ApiObject> objects, IDictionary<string, string> warnings, IDictionary<string, ApiEnum> enums, IDictionary<string, ApiObject> otherObjects, IDictionary<string, ApiObject> schemaObjects) { this.otherObjects = otherObjects; this.schemaObjects = schemaObjects; var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = new List<Property>(), JSONSchema = jsonSchema.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\n", "").Replace("\"", "\\\"") }; JsonSchema schema = null; Newtonsoft.JsonV4.Schema.JsonSchema v4Schema = null; if (jsonSchema.Contains("\"oneOf\":")) { v4Schema = ParseV4Schema(key, jsonSchema, warnings, objects); } else { schema = ParseV3OrV4Schema(key, jsonSchema, warnings, ref v4Schema, objects); } if (schema == null && v4Schema == null) return obj; if (schema != null) { if (schema.Type == JsonSchemaType.Array) { obj.IsArray = true; if (schema.Items != null && schema.Items.Any()) ParseProperties(objects, obj.Properties, schema.Items.First().Properties, enums); } else { ParseProperties(objects, obj.Properties, schema.Properties, enums); } } else { if (v4Schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Array) { obj.IsArray = true; if (v4Schema.Items != null && v4Schema.Items.Any()) ParseProperties(objects, obj.Properties, v4Schema.Items.First(), enums); } else { ParseProperties(objects, obj.Properties, v4Schema, enums); } } return obj; }
private static string GetTypeFromApiObject(ApiObject apiObject) { if (!apiObject.IsArray) { return(apiObject.Name); } if (apiObject.Type == null) { return(CollectionTypeHelper.GetCollectionType(apiObject.Name)); } return(CollectionTypeHelper.GetCollectionType(apiObject.Type)); }
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); }
public static ApiObject FindObjectWithSameProperties(ApiObject apiObject, string key, IDictionary <string, ApiObject> objects, IDictionary <string, ApiObject> otherObjects, IDictionary <string, ApiObject> schemaObjects) { var obj = FindObject(apiObject, schemaObjects, key); if (obj == null) { obj = FindObject(apiObject, objects, key); } if (obj == null) { obj = FindObject(apiObject, otherObjects, key); } return(obj); }
private bool IsUsedAsReferenceInAnyObject(ApiObject requestObj) { return(schemaObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) || x.Type == requestObj.BaseClass || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass)) || schemaRequestObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) || x.Type == requestObj.BaseClass || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass)) || schemaResponseObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) || x.Type == requestObj.BaseClass || x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass))); }
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); }
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; }
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); }
// To use with XML Schema objects public static bool HasSameProperties(ApiObject apiObject, IDictionary<string, ApiObject> objects, IDictionary<string, ApiObject> otherObjects, IDictionary<string, ApiObject> schemaObjects) { return objects.Any(o => o.Value.GeneratedCode == apiObject.GeneratedCode) || otherObjects.Any(o => o.Value.GeneratedCode == apiObject.GeneratedCode) || schemaObjects.Any(o => o.Value.GeneratedCode == apiObject.GeneratedCode); }
public static bool HasSameProperties(ApiObject apiObject, IDictionary<string, ApiObject> objects, string key, IDictionary<string, ApiObject> otherObjects, IDictionary<string, ApiObject> schemaObjects) { var obj = FindObject(apiObject, schemaObjects, key); if (obj == null) obj = FindObject(apiObject, objects, key); if (obj == null) obj = FindObject(apiObject, otherObjects, key); if (obj == null) throw new InvalidOperationException("Could not find object with key " + key); if (!string.IsNullOrWhiteSpace(obj.GeneratedCode)) { if(objects.Any(o => o.Value.GeneratedCode == obj.GeneratedCode) || otherObjects.Any(o => o.Value.GeneratedCode == obj.GeneratedCode) || schemaObjects.Any(o => o.Value.GeneratedCode == obj.GeneratedCode)) return true; } if (obj.Properties.Count != apiObject.Properties.Count) return false; return apiObject.Properties.All(property => obj.Properties.Any(p => p.Name == property.Name && p.Type == property.Type)); }
// To use with XML Schema objects public static bool HasSameProperties(ApiObject apiObject, IDictionary <string, ApiObject> objects, IDictionary <string, ApiObject> otherObjects, IDictionary <string, ApiObject> schemaObjects) { return(objects.Any(o => o.Value.GeneratedCode == apiObject.GeneratedCode) || otherObjects.Any(o => o.Value.GeneratedCode == apiObject.GeneratedCode) || schemaObjects.Any(o => o.Value.GeneratedCode == apiObject.GeneratedCode)); }
private void AddObjectToObjectCollectionOrLink(ApiObject obj, string key, IDictionary<string, ApiObject> objects, IDictionary<string, ApiObject> otherObjects) { if (obj == null || (!obj.Properties.Any() && obj.Type == null)) return; if (schemaObjects.All(o => o.Value.Name != obj.Name) && objects.All(o => o.Value.Name != obj.Name) && otherObjects.All(o => o.Value.Name != obj.Name)) { objects.Add(key, obj); } else { if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects)) { if (string.IsNullOrWhiteSpace(obj.GeneratedCode) && !linkKeysWithObjectNames.ContainsKey(key)) linkKeysWithObjectNames.Add(key, obj.Name); } else if(!objects.ContainsKey(key)) { obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, schemaObjects, schemaObjects); objects.Add(key, obj); } } }
public ApiObject Parse(string key, string jsonSchema, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings) { var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = new List <Property>() }; JsonSchema schema = null; Newtonsoft.JsonV4.Schema.JsonSchema v4Schema = null; try { schema = JsonSchema.Parse(jsonSchema); } catch (Exception exv3) // NewtonJson does not support Json Schema v4 { try { schema = null; v4Schema = Newtonsoft.JsonV4.Schema.JsonSchema.Parse(jsonSchema); } catch (Exception exv4) { //dynamic dynObj = JsonConvert.DeserializeObject(jsonSchema); //foreach (var kv in dynObj) //{ // //TODO: manual parse schema ? or parse using example ? //} if (!warnings.ContainsKey(key)) { warnings.Add(key, "Could not parse JSON Schema. v3 parser message: " + exv3.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty) + ". v4 parser message: " + exv4.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty)); } } } if (schema == null && v4Schema == null) { return(obj); } if (schema != null) { if (schema.Type == JsonSchemaType.Array) { obj.IsArray = true; if (schema.Items != null && schema.Items.Any()) { ParseProperties(objects, obj.Properties, schema.Items.First().Properties); } } else { ParseProperties(objects, obj.Properties, schema.Properties); } } else { if (v4Schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Array) { obj.IsArray = true; if (v4Schema.Items != null && v4Schema.Items.Any()) { ParseProperties(objects, obj.Properties, v4Schema.Items.First().Properties); } } else { ParseProperties(objects, obj.Properties, v4Schema.Properties); } } return(obj); }
private static GeneratorParameter CreateGeneratorParameter(ApiObject apiObject) { var generatorParameter = new GeneratorParameter { Name = apiObject.Name.ToLower(), Type = apiObject.IsArray ? CollectionTypeHelper.GetCollectionType(apiObject.Name) : apiObject.Name, Description = apiObject.Description }; return generatorParameter; }
private static string GetTypeFromApiObject(ApiObject apiObject) { if (!apiObject.IsArray) return apiObject.Name; if(apiObject.Type == null) return CollectionTypeHelper.GetCollectionType(apiObject.Name); return CollectionTypeHelper.GetCollectionType(apiObject.Type); }
public static bool HasSameProperties(ApiObject apiObject, IDictionary<string, ApiObject> objects, string key, IDictionary<string, ApiObject> otherObjects, IDictionary<string, ApiObject> schemaObjects) { var obj = FindObject(apiObject, schemaObjects, key); if (obj == null) obj = FindObject(apiObject, objects, key); if (obj == null) obj = FindObject(apiObject, otherObjects, key); if (obj == null) throw new InvalidOperationException("Could not find object with key " + key); if (obj.Properties.Count != apiObject.Properties.Count) return false; return apiObject.Properties.All(property => obj.Properties.Any(p => p.Name == property.Name && p.Type == property.Type)); }
public bool IsUsedAsParameterInAnyMethod(IEnumerable <ClassObject> classes, ApiObject requestObj) { return(classes.Any(c => c.Methods .Any(m => m.Parameter != null && (m.Parameter.Type == requestObj.Name || m.Parameter.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name))))); }
private void ParseObject(string key, IDictionary<string, JsonSchema> schema, IDictionary<string, ApiObject> objects, IDictionary<string, ApiEnum> enums) { if (schema == null) return; var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = ParseSchema(schema, objects, enums) }; // 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); }
public bool IsUsedAsParameterInAnyMethod(IEnumerable<ClassObject> classes, ApiObject requestObj) { return classes.Any(c => c.Methods .Any(m => m.Parameter != null && (m.Parameter.Type == requestObj.Name || m.Parameter.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name)))); }
private static GeneratorParameter CreateGeneratorParameter(ApiObject apiObject) { var generatorParameter = new GeneratorParameter { Name = apiObject.Name.ToLower(), Type = GetTypeFromApiObject(apiObject), Description = apiObject.Description }; return generatorParameter; }
private void AddObjectToObjectCollectionOrLink(ApiObject obj, string key, IDictionary<string, ApiObject> objects) { if (obj == null || !obj.Properties.Any()) return; if (objects.All(o => o.Value.Name != obj.Name)) { objects.Add(key, obj); } else { if (!linkKeysWithObjectNames.ContainsKey(key)) linkKeysWithObjectNames.Add(key, obj.Name); } }
private string ParseObject(string key, IDictionary<string, JsonSchema> schema, IDictionary<string, ApiObject> objects, IDictionary<string, ApiEnum> enums) { if (schema == null) return null; var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = ParseSchema(schema, objects, enums) }; if (obj == null) return null; if(!obj.Properties.Any()) return null; // Avoid duplicated keys and names or no properties if (objects.ContainsKey(key) || objects.Any(o => o.Value.Name == obj.Name) || otherObjects.ContainsKey(key) || otherObjects.Any(o => o.Value.Name == obj.Name) || schemaObjects.ContainsKey(key) || schemaObjects.Any(o => o.Value.Name == obj.Name)) { if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects)) return key; obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, otherObjects, schemaObjects); key = UniquenessHelper.GetUniqueKey(objects, key, otherObjects); } objects.Add(key, obj); return key; }
public bool IsUsedAsResponseInAnyMethod(IEnumerable<ClassObject> classes, ApiObject requestObj) { return classes.Any(c => c.Methods.Any(m => m.ReturnType == requestObj.Name || m.ReturnType == CollectionTypeHelper.GetCollectionType(requestObj.Name))); }
private void ParseObject(string key, IDictionary<string, Newtonsoft.JsonV4.Schema.JsonSchema> schema, IDictionary<string, ApiObject> objects, IDictionary<string, ApiEnum> enums, string baseClass = null) { var obj = new ApiObject { Name = NetNamingMapper.GetObjectName(key), Properties = ParseSchema(schema, objects, enums), BaseClass = baseClass }; // Avoid duplicated keys and names if (objects.ContainsKey(key) || objects.Any(o => o.Value.Name == obj.Name) || otherObjects.ContainsKey(key) || otherObjects.Any(o => o.Value.Name == obj.Name) || schemaObjects.ContainsKey(key) || schemaObjects.Any(o => o.Value.Name == obj.Name) || !obj.Properties.Any()) { if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects)) return; obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, otherObjects, schemaObjects); key = UniquenessHelper.GetUniqueKey(objects, key, otherObjects); } objects.Add(key, obj); }
public bool IsUsedAsResponseInAnyMethod(IEnumerable <ClassObject> classes, ApiObject requestObj) { return(classes.Any(c => c.Methods.Any(m => m.ReturnType == requestObj.Name || m.ReturnType == CollectionTypeHelper.GetCollectionType(requestObj.Name)))); }