public bool IsUsedAsParameterInAnyMethod(IEnumerable <Core.WebApiGenerator.ControllerObject> controllers, ApiObject requestObj)
 {
     return(controllers.Any(c => c.Methods
                            .Any(m => m.Parameter != null &&
                                 (m.Parameter.Type == requestObj.Type || m.Parameter.Type == CollectionTypeHelper.GetCollectionType(requestObj.Type) ||
                                  CollectionTypeHelper.GetCollectionType(m.Parameter.Type) == requestObj.Type))));
 }
Пример #2
0
        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);
        }
Пример #3
0
        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);
                    }
                }
            }
        }
Пример #4
0
 private void UpdateObjects(ApiObject obj, ICollection <ApiObject> values)
 {
     foreach (var type in values)
     {
         foreach (var prop in type.Properties.Where(p => p.TypeId != null))
         {
             if (NeedsFixing(obj, prop))
             {
                 if (CollectionTypeHelper.IsCollection(prop.Type))
                 {
                     prop.Type = CollectionTypeHelper.GetCollectionType(obj.Name);
                 }
                 else
                 {
                     prop.Type = obj.Name;
                 }
             }
             else if (linkIdsWithTypes.ContainsKey(prop.TypeId))
             {
                 if (CollectionTypeHelper.IsCollection(prop.Type))
                 {
                     prop.Type = CollectionTypeHelper.GetCollectionType(linkIdsWithTypes[prop.TypeId]);
                 }
                 else
                 {
                     prop.Type = linkIdsWithTypes[prop.TypeId];
                 }
             }
         }
     }
 }
        private bool IsUsedAsReferenceInAnyObject(ApiObject obj)
        {
            return(schemaObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == obj.Type ||
                                                                         x.Type == CollectionTypeHelper.GetCollectionType(obj.Type) ||
                                                                         x.Type == obj.BaseClass ||
                                                                         CollectionTypeHelper.GetCollectionType(x.Type) == obj.Type ||
                                                                         x.Type == CollectionTypeHelper.GetCollectionType(obj.BaseClass) ||
                                                                         CollectionTypeHelper.GetCollectionType(x.Type) == obj.BaseClass
                                                                         )

                   || schemaObjects.Values.Any(o => o.BaseClass == obj.Type)

                   || schemaRequestObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == obj.Type ||
                                                                                   x.Type == CollectionTypeHelper.GetCollectionType(obj.Type) ||
                                                                                   x.Type == obj.BaseClass ||
                                                                                   CollectionTypeHelper.GetCollectionType(x.Type) == obj.Type ||
                                                                                   x.Type == CollectionTypeHelper.GetCollectionType(obj.BaseClass) ||
                                                                                   CollectionTypeHelper.GetCollectionType(x.Type) == obj.BaseClass)

                   || schemaResponseObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == obj.Type ||
                                                                                    x.Type == CollectionTypeHelper.GetCollectionType(obj.Type) ||
                                                                                    x.Type == obj.BaseClass ||
                                                                                    CollectionTypeHelper.GetCollectionType(x.Type) == obj.Type ||
                                                                                    x.Type == CollectionTypeHelper.GetCollectionType(obj.BaseClass) ||
                                                                                    CollectionTypeHelper.GetCollectionType(x.Type) == obj.BaseClass));
        }
 public bool IsUsedAsParameterInAnyMethod(IEnumerable <ClassObject> classes, ApiObject requestObj)
 {
     return(classes.Any(c => c.Methods
                        .Any(m => m.Parameter != null &&
                             (m.Parameter.Type == requestObj.Type || m.Parameter.Type == CollectionTypeHelper.GetCollectionType(requestObj.Type) ||
                              CollectionTypeHelper.GetCollectionType(m.Parameter.Type) == requestObj.Type))));
 }
Пример #7
0
        private void AddProperty(RAML.Parser.Model.EndPoint resource, Operation 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.StatusCode, fullUrl);

            if (string.IsNullOrWhiteSpace(type))
            {
                return;
            }

            var name = NetNamingMapper.GetPropertyName(CollectionTypeHelper.GetBaseType(type));

            if (properties.Any(p => p.Name == name))
            {
                name = name + response.StatusCode;
            }

            var property = new Property
            {
                Name        = name,
                Description = response.Description + " " + mimeType.Schema.Description,
                Example     = ObjectParser.MapExample(mimeType.Schema),
                Type        = type,
                StatusCode  = GetStatusCode(response),
                JSONSchema  = mimeType.Schema as SchemaShape == null ? null : ((SchemaShape)mimeType.Schema).Raw.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\n", "").Replace("\"", "\\\"")
            };

            properties.Add(property);
        }
 public bool IsUsedAsResponseInAnyMethod(IEnumerable <ClassObject> classes, ApiObject apiObj)
 {
     return(classes.Any(c => c.Methods.Any(m => m.ReturnType == apiObj.Type ||
                                           m.ReturnType == CollectionTypeHelper.GetCollectionType(apiObj.Type) ||
                                           CollectionTypeHelper.GetCollectionType(m.ReturnType) == apiObj.Type ||
                                           m.ReturnType == apiObj.Name ||
                                           (m.ReturnTypeObject != null && m.ReturnTypeObject.Properties != null &&
                                            m.ReturnTypeObject.Properties.Any(x => x.Type == apiObj.Type || x.Type == CollectionTypeHelper.GetCollectionType(apiObj.Type) ||
                                                                              CollectionTypeHelper.GetCollectionType(x.Type) == apiObj.Type)))));
 }
Пример #9
0
        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 (NewNetTypeMapper.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 (NewNetTypeMapper.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 = NewNetTypeMapper.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));
        }
Пример #10
0
        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)));
        }
Пример #11
0
        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;
                        }
                    }
                }
            }
        }
Пример #12
0
        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);
        }
Пример #13
0
        public static string GetNetType(Shape shape, IDictionary <string, ApiObject> existingObjects = null,
                                        IDictionary <string, ApiObject> newObjects = null, IDictionary <string, ApiEnum> existingEnums = null, IDictionary <string, ApiEnum> newEnums = null)
        {
            if (!string.IsNullOrWhiteSpace(shape.LinkTargetName))
            {
                return(NetNamingMapper.GetObjectName(GetTypeFromLinkOrId(shape.LinkTargetName)));
            }

            if (shape is ScalarShape scalar)
            {
                if (shape.Values != null && shape.Values.Any())
                {
                    var key = GetTypeFromLinkOrId(shape.Id);
                    if (existingEnums != null && existingEnums.ContainsKey(key))
                    {
                        return(existingEnums[key].Name);
                    }
                    if (newEnums != null && newEnums.ContainsKey(key))
                    {
                        return(newEnums[key].Name);
                    }
                }
                return(GetNetType(scalar.DataType.Substring(scalar.DataType.LastIndexOf('#') + 1), scalar.Format));
            }
            if (shape is ArrayShape array)
            {
                return(CollectionTypeHelper.GetCollectionType(GetNetType(array.Items, existingObjects, newObjects, existingEnums, newEnums)));
            }

            if (shape is FileShape file)
            {
                return(TypeStringConversion["file"]);
            }

            if (shape.Id.Contains("#/declarations"))
            {
                var key = GetTypeFromLinkOrId(shape.Id);
                if (existingObjects != null && (existingObjects.ContainsKey(key) ||
                                                existingObjects.Keys.Any(k => k.ToLowerInvariant() == key.ToLowerInvariant())))
                {
                    if (existingObjects.ContainsKey(key))
                    {
                        return(existingObjects[key].Type);
                    }

                    return(existingObjects.First(kv => kv.Key.ToLowerInvariant() == key.ToLowerInvariant()).Value.Type);
                }
                if (newObjects != null && newObjects.ContainsKey(key))
                {
                    return(newObjects[key].Type);
                }
            }

            if (shape.Inherits.Count() == 1)
            {
                if (shape is NodeShape nodeShape)
                {
                    if (nodeShape.Properties.Count() == 0)
                    {
                        return(GetNetType(nodeShape.Inherits.First(), existingObjects, newObjects, existingEnums, newEnums));
                    }
                }
                if (shape.Inherits.First() is ArrayShape arrayShape)
                {
                    return(CollectionTypeHelper.GetCollectionType(GetNetType(arrayShape.Items, existingObjects, newObjects, existingEnums, newEnums)));
                }

                if (shape is AnyShape any)
                {
                    var key = NetNamingMapper.GetObjectName(any.Inherits.First().Name);
                    if (existingObjects != null && existingObjects.ContainsKey(key))
                    {
                        return(key);
                    }
                    if (newObjects != null && newObjects.ContainsKey(key))
                    {
                        return(key);
                    }
                }
            }
            if (shape.Inherits.Count() > 0)
            {
                //TODO: check
            }

            if (shape.GetType() == typeof(AnyShape))
            {
                return(GetNetType("any", null));
            }

            if (shape is UnionShape)
            {
                return("object");
            }

            return(NetNamingMapper.GetObjectName(shape.Name));
        }
Пример #14
0
 private bool NeedsFixing(ApiObject obj, Property prop)
 {
     return(prop.TypeId == obj.Id && ((!CollectionTypeHelper.IsCollection(prop.Type) && prop.Type != obj.Name) ||
                                      (CollectionTypeHelper.IsCollection(prop.Type) && CollectionTypeHelper.GetCollectionType(obj.Type) != prop.Type)));
 }
Пример #15
0
 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)));
 }
Пример #16
0
        private Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> > ParseObject(Guid id, string key, Shape shape,
                                                                                                                                IDictionary <string, ApiObject> existingObjects, bool isRootType)
        {
            if (AlreadyExists(existingObjects, existingEnums, shape.Id))
            {
                return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >
                           (newObjects, newEnums, new Dictionary <Guid, string>()));
            }

            // if is array of scalar or array of object there's no new object, except when is a root type
            if (!isRootType && shape is ArrayShape array && (array.Items is ScalarShape || array.Items is AnyShape))
            {
                return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >
                           (newObjects, newEnums, new Dictionary <Guid, string>()));
            }

            var linkedType = new Dictionary <Guid, string>();

            var apiObj = new ApiObject
            {
                Id          = id,
                AmfId       = shape.Id,
                BaseClass   = GetBaseClass(shape),
                IsArray     = shape is ArrayShape,
                IsScalar    = shape is ScalarShape,
                IsUnionType = shape is UnionShape,
                Name        = NetNamingMapper.GetObjectName(key),
                Type        = NetNamingMapper.GetObjectName(key),
                Description = shape.Description,
                Example     = MapExample(shape)
            };

            if (isRootType && apiObj.IsScalar)
            {
                apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects);
            }

            if (isRootType && apiObj.IsArray && shape is ArrayShape arrShape)
            {
                var itemShape = arrShape.Items;
                if (itemShape is NodeShape)
                {
                    var itemId = Guid.NewGuid();
                    ParseObject(itemId, itemShape.Name, itemShape, existingObjects, false);

                    apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects);
                }
                else
                {
                    //if (newObjects.ContainsKey(itemShape.Id))
                    //    apiObj.Type = CollectionTypeHelper.GetCollectionType(newObjects[itemShape.Id].Type);
                    //else if(existingObjects.ContainsKey(itemShape.Id))
                    //    apiObj.Type = CollectionTypeHelper.GetCollectionType(existingObjects[itemShape.Id].Type);
                    //else
                    apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects);
                }
            }

            if (shape is NodeShape node && node.Properties.Count() == 1 && node.Properties.First().Path != null && node.Properties.First().Path.StartsWith("/") &&
                node.Properties.First().Path.EndsWith("/"))
            {
                apiObj.IsMap = true;
                var valueType = "object";
                if (node.Properties.First().Range != null)
                {
                    valueType = NewNetTypeMapper.GetNetType(node.Properties.First().Range, existingObjects);
                }

                apiObj.Type = $"Dictionary<string, {valueType}>";
                AddNewObject(shape, apiObj);
                return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType));
            }

            apiObj.Properties = MapProperties(shape, apiObj.Name).ToList();

            ApiObject match = null;

            if (existingObjects.Values.Any(o => o.Name == apiObj.Name) || newObjects.Values.Any(o => o.Name == apiObj.Name))
            {
                if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, newObjects, emptyDic))
                {
                    return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType));
                }

                match = HandleNameDuplication(id, apiObj, key, linkedType);
            }

            if (existingObjects.Values.Any(o => o.Type == apiObj.Type) || newObjects.Values.Any(o => o.Type == apiObj.Type))
            {
                if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, newObjects, emptyDic))
                {
                    return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType));
                }

                match = HandleTypeDuplication(id, apiObj, key, linkedType);
            }

            if (match != null)
            {
                return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType));
            }

            //if (match == null)
            //{
            //    match = UniquenessHelper.FirstOrDefaultWithSameProperties(apiObj, existingObjects, key, newObjects, emptyDic);
            //    if (match != null)
            //    {
            //        if (!linkedType.ContainsKey(id))
            //            linkedType.Add(id, match.Type);
            //    }
            //}

            if (shape.Inherits != null && shape.Inherits.Count() == 1)
            {
                var baseClass = NewNetTypeMapper.GetNetType(shape.Inherits.First(), existingObjects, newObjects, existingEnums, newEnums);
                if (!string.IsNullOrWhiteSpace(baseClass))
                {
                    apiObj.BaseClass = CollectionTypeHelper.GetConcreteType(baseClass);
                }
            }

            AddNewObject(shape, apiObj);

            return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum>, IDictionary <Guid, string> >(newObjects, newEnums, linkedType));
        }
Пример #17
0
        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);
        }
Пример #18
0
        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);
        }