protected void CleanProperties(IDictionary <string, ApiObject> apiObjects) { var keys = apiObjects.Keys.ToList(); var apiObjectsCount = keys.Count - 1; for (var i = apiObjectsCount; i >= 0; i--) { var apiObject = apiObjects[keys[i]]; var count = apiObject.Properties.Count; for (var index = count - 1; index >= 0; index--) { var prop = apiObject.Properties[index]; var type = prop.Type; if (!string.IsNullOrWhiteSpace(type) && IsCollectionType(type)) { type = CollectionTypeHelper.GetBaseType(type); } if (prop.IsAdditionalProperties) { continue; } if (!NewNetTypeMapper.IsPrimitiveType(type) && schemaResponseObjects.All(o => o.Value.Name != type) && schemaRequestObjects.All(o => o.Value.Name != type) && enums.All(e => e.Value.Name != type) && schemaObjects.All(o => o.Value.Name != type)) { apiObject.Properties.Remove(prop); } } } }
private static string GetCollectionType(Property prop, string itemType) { if (NewNetTypeMapper.IsPrimitiveType(itemType)) { return(CollectionTypeHelper.GetCollectionType(itemType)); } if (CollectionTypeHelper.IsCollection(itemType)) { return(CollectionTypeHelper.GetCollectionType(itemType)); } return(CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(itemType))); }
protected void HandleScalarTypes() { foreach (var obj in schemaObjects) { foreach (var prop in obj.Value.Properties) { var baseType = prop.Type; if (CollectionTypeHelper.IsCollection(prop.Type)) { baseType = CollectionTypeHelper.GetBaseType(prop.Type); } if (!prop.IsEnum && !NewNetTypeMapper.IsPrimitiveType(baseType)) { var apiObj = FindObject(baseType, schemaObjects); if (apiObj != null && apiObj.IsScalar) { prop.Type = apiObj.Type; } } } } }
private Property MapProperty(PropertyShape p, string parentClassName) { var name = GetPropertyName(p); var prop = new Property(parentClassName) { Name = NetNamingMapper.GetPropertyName(name), Required = p.Required, Type = NetNamingMapper.GetObjectName(name), InheritanceProvenance = p.InheritanceProvenance }; if (p.Range == null) { return(prop); } prop.OriginalName = p.Range.Name; prop.AmfId = p.Range.Id; prop.Description = p.Range.Description; prop.Example = MapExample(p.Range); prop.Type = NewNetTypeMapper.GetNetType(p.Range, existingObjects, newObjects, existingEnums, newEnums); if (p.Range is ScalarShape scalar) { prop.Pattern = scalar.Pattern; prop.MaxLength = scalar.MaxLength; prop.MinLength = scalar.MinLength; prop.Maximum = string.IsNullOrWhiteSpace(scalar.Maximum) ? (double?)null : Convert.ToDouble(scalar.Maximum); prop.Minimum = string.IsNullOrWhiteSpace(scalar.Minimum) ? (double?)null : Convert.ToDouble(scalar.Minimum); if (scalar.Values != null && scalar.Values.Any()) { prop.IsEnum = true; var apiEnum = CreateEnum(scalar, existingEnums, warnings, newEnums); string amfId = apiEnum.AmfId; if (string.IsNullOrWhiteSpace(amfId)) { amfId = prop.AmfId ?? prop.Name; } prop.Type = apiEnum.Name; if (newEnums.ContainsKey(amfId)) { return(prop); } if (existingEnums.Values.Any(o => o.Name == apiEnum.Name) || newEnums.Values.Any(o => o.Name == apiEnum.Name)) { if (UniquenessHelper.HasSamevalues(apiEnum, existingEnums, newEnums)) { return(prop); } apiEnum.Name = UniquenessHelper.GetUniqueName(apiEnum.Name, existingEnums, newEnums); prop.Type = apiEnum.Name; } newEnums.Add(amfId, apiEnum); } return(prop); } //if (existingObjects.ContainsKey(prop.Type) || newObjects.ContainsKey(prop.Type)) // return prop; if (p.Range is NodeShape) { if (existingObjects.ContainsKey(p.Range.Id)) { prop.Type = existingObjects[p.Range.Id].Type; return(prop); } if (newObjects.ContainsKey(p.Range.Id)) { prop.Type = newObjects[p.Range.Id].Type; return(prop); } var id = Guid.NewGuid(); prop.TypeId = id; var tuple = ParseObject(id, prop.Name, p.Range, existingObjects, warnings, existingEnums); prop.Type = NetNamingMapper.GetObjectName(prop.Name); } if (p.Range is ArrayShape array) { if (array.Items is ScalarShape) { return(prop); } if (!string.IsNullOrWhiteSpace(array.Items.LinkTargetName)) { return(prop); } var itemType = NewNetTypeMapper.GetNetType(array.Items, existingObjects, newObjects, existingEnums, newEnums); GetCollectionType(prop, itemType); if (array.Items is NodeShape && itemType == "Items") { itemType = NetNamingMapper.GetObjectName(array.Name); prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(array.Name)); } if (existingObjects.ContainsKey(array.Id)) { return(prop); } if (array.Items is ScalarShape || array.Items.GetType() == typeof(AnyShape)) { return(prop); } var newId = Guid.NewGuid(); prop.TypeId = newId; ParseObject(newId, array.Name, array.Items, existingObjects, warnings, existingEnums); } foreach (var parent in p.Range.Inherits) { if (!(parent is ScalarShape) && !NewNetTypeMapper.IsPrimitiveType(prop.Type) && !(CollectionTypeHelper.IsCollection(prop.Type) && NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(prop.Type))) && string.IsNullOrWhiteSpace(parent.LinkTargetName)) { var newId = Guid.NewGuid(); ParseObject(newId, prop.Name, parent, existingObjects, warnings, existingEnums); } } return(prop); }
private string DecodeResponseRaml1Type(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 objectType = GetReturnTypeFromName(subtype); if (!string.IsNullOrWhiteSpace(objectType)) { return("IDictionary<string, " + objectType + ">"); } return("IDictionary<string, object>"); } if (NewNetTypeMapper.IsPrimitiveType(type)) { return(NewNetTypeMapper.Map(type)); } return(type); }
internal static bool IsPrimitiveOrSchemaObject(string type, IDictionary <string, ApiObject> schemaObjects) { return(NewNetTypeMapper.IsPrimitiveType(type) || NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(type)) || schemaObjects.ContainsKey(type) || schemaObjects.Any(o => o.Value.Type == type || o.Value.Type == CollectionTypeHelper.GetBaseType(type))); }
private Property MapProperty(PropertyShape p, string parentClassName) { var prop = new Property(parentClassName) { Name = NetNamingMapper.GetObjectName(GetNameFromPath(p.Path)), Required = p.Required, Type = NetNamingMapper.GetObjectName(GetNameFromPath(p.Path)) //TODO: check }; if (p.Range == null) { return(prop); } prop.Name = NetNamingMapper.GetObjectName(string.IsNullOrWhiteSpace(p.Path) ? p.Range.Name : GetNameFromPath(p.Path)); prop.Description = p.Range.Description; prop.Example = MapExample(p.Range); prop.Type = NewNetTypeMapper.GetNetType(p.Range, existingObjects, newObjects, existingEnums, newEnums); if (p.Range is ScalarShape scalar) { prop.Pattern = scalar.Pattern; prop.MaxLength = scalar.MaxLength; prop.MinLength = scalar.MinLength; prop.Maximum = string.IsNullOrWhiteSpace(scalar.Maximum) ? (double?)null : Convert.ToDouble(scalar.Maximum); prop.Minimum = string.IsNullOrWhiteSpace(scalar.Minimum) ? (double?)null : Convert.ToDouble(scalar.Minimum); if (scalar.Values != null && scalar.Values.Any()) { // enum ?? prop.IsEnum = true; var apiEnum = ParseEnum(scalar, existingEnums, warnings, newEnums); if (!newEnums.ContainsKey(apiEnum.Name)) { newEnums.Add(apiEnum.Name, apiEnum); } prop.Type = apiEnum.Name; } return(prop); } if (existingObjects.ContainsKey(prop.Type) || newObjects.ContainsKey(prop.Type)) { return(prop); } if (p.Range is NodeShape) { var tuple = ParseObject(prop.Name, p.Range, existingObjects, warnings, existingEnums); prop.Type = NetNamingMapper.GetObjectName(prop.Name); } if (p.Range is ArrayShape array) { if (array.Items is ScalarShape) { return(prop); } var itemType = NewNetTypeMapper.GetNetType(array.Items, existingObjects, newObjects, existingEnums, newEnums); prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(itemType)); if (array.Items is NodeShape && itemType == "Items") { itemType = NetNamingMapper.GetObjectName(array.Name); prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(array.Name)); } if (existingObjects.ContainsKey(itemType) || newObjects.ContainsKey(itemType)) { return(prop); } ParseObject(array.Name, array.Items, existingObjects, warnings, existingEnums); } foreach (var parent in p.Range.Inherits) { if (!(parent is ScalarShape) && !NewNetTypeMapper.IsPrimitiveType(prop.Type) && !(CollectionTypeHelper.IsCollection(prop.Type) && NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(prop.Type))) && string.IsNullOrWhiteSpace(parent.LinkTargetName)) { ParseObject(prop.Name, parent, existingObjects, warnings, existingEnums); } } return(prop); }