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);
                    }
                }
            }
        }
Example #2
0
        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 shape      = (AnyShape)header.Schema;
                var type       = NewNetTypeMapper.GetNetType(shape);
                var typeSuffix = (type == "string" || type == "object" || header.Required ? "" : "?");

                properties.Add(new Property
                {
                    Type         = type + typeSuffix,
                    Name         = NetNamingMapper.GetPropertyName(header.Name),
                    OriginalName = header.Name,
                    Description  = description,
                    Example      = ObjectParser.MapExample(shape),
                    Required     = header.Required
                });
            }
            return(properties);
        }
        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));
        }
Example #4
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)));
        }
        private IEnumerable <GeneratorParameter> MatchParameters(IDictionary <string, Parameter> parentUriParameters, GeneratorParameter[] urlParameters)
        {
            var parameters = new List <GeneratorParameter>();

            foreach (var param in urlParameters)
            {
                if (parentUriParameters.ContainsKey(param.Name))
                {
                    param.Type        = NewNetTypeMapper.GetNetType(parentUriParameters[param.Name].Schema);
                    param.Description = parentUriParameters[param.Name].Description;
                }
                parameters.Add(param);
            }
            return(parameters);
        }
        public IEnumerable <GeneratorParameter> GetUriParameters(EndPoint resource, string url, IDictionary <string, Parameter> parentUriParameters)
        {
            var parameters = resource.Parameters
                             .Select(p => new GeneratorParameter {
                Name = p.Name, Type = NewNetTypeMapper.GetNetType(p.Schema), Description = p.Description
            })
                             .ToList();

            //TODO: check
            var urlParameters        = ExtractParametersFromUrl(url).ToArray();
            var distincUrlParameters = urlParameters.Where(up => parameters.All(p => up.Name != p.Name)).ToArray();

            var matchedParameters = MatchParameters(parentUriParameters, distincUrlParameters);

            parameters.AddRange(matchedParameters);
            return(parameters);
        }
        public IList <Property> ConvertParametersToProperties(IEnumerable <Parameter> parameters)
        {
            var properties = new List <Property>();

            foreach (var parameter in parameters.Where(parameter => parameter != null && parameter.Schema != null))
            {
                var description = ParserHelpers.RemoveNewLines(parameter.Description);

                properties.Add(new Property
                {
                    Type         = NewNetTypeMapper.GetNetType(parameter.Schema),
                    Name         = NetNamingMapper.GetPropertyName(parameter.Name),
                    OriginalName = parameter.Name,
                    Description  = description,
                    Example      = ObjectParser.MapExample(parameter.Schema),
                    Required     = parameter.Required
                });
            }
            return(properties);
        }
        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 string GetType(Parameter param)
        {
            if (param.Schema == null)
            {
                return("string");
            }

            if (param.Schema is ScalarShape scalar)
            {
                return(NewNetTypeMapper.GetNetType(scalar) +
                       (NewNetTypeMapper.GetNetType(scalar) == "string" || NewNetTypeMapper.GetNetType(scalar) == "object" || param.Required ? "" : "?"));
            }

            var pureType = param.Schema.Name;

            if (schemaObjects.ContainsKey(pureType))
            {
                var apiObject = schemaObjects[pureType];
                return(RamlTypesHelper.GetTypeFromApiObject(apiObject));
            }

            return(pureType);
        }
        public IEnumerable <GeneratorParameter> GetUriParameters(EndPoint resource, string url, IDictionary <string, Parameter> parentUriParameters)
        {
            if (resource == null || resource.Parameters == null)
            {
                return(new List <GeneratorParameter>());
            }

            var parameters = resource.Parameters.Where(p => p != null)
                             .Select(p => new GeneratorParameter {
                OriginalName = p.Name,
                Name         = NetNamingMapper.Capitalize(NetNamingMapper.RemoveInvalidChars(p.Name)),
                Type         = p.Schema != null ? NewNetTypeMapper.GetNetType(p.Schema) : "string",
                Description  = p.Description
            })
                             .ToList();

            var urlParameters        = ExtractParametersFromUrl(url).ToArray();
            var distincUrlParameters = urlParameters.Where(up => parameters.All(p => up.Name != p.Name)).ToArray();

            var matchedParameters = MatchParameters(parentUriParameters, distincUrlParameters);

            parameters.AddRange(matchedParameters);
            return(parameters);
        }
        private string GetNamedReturnType(Operation method, EndPoint resource, Payload mimeType, string fullUrl)
        {
            return(NewNetTypeMapper.GetNetType(mimeType.Schema, schemaObjects, null, enums));

            //if (mimeType.Schema != null && mimeType.Schema.Contains("<<") && mimeType.Schema.Contains(">>"))
            //    return GetReturnTypeFromParameter(method, resource, fullUrl, mimeType.Schema);

            //if (mimeType.Schema != null && !mimeType.Schema.Contains("<") && !mimeType.Schema.Contains("{"))
            //    return GetReturnTypeFromName(mimeType.Schema);

            //if (!string.IsNullOrWhiteSpace(mimeType.Type))
            //{
            //    if (mimeType.Type.Contains("<<") && mimeType.Type.Contains(">>"))
            //        return GetReturnTypeFromParameter(method, resource, fullUrl, mimeType.Type);

            //    var type = GetReturnTypeFromName(mimeType.Type);
            //    if (!string.IsNullOrWhiteSpace(type))
            //        return type;

            //    return DecodeResponseRaml1Type(mimeType.Type);
            //}

            //return string.Empty;
        }
Example #12
0
 private static GeneratorParameter ConvertAmfParameterToGeneratorParameter(Parameter parameter)
 {
     return(new GeneratorParameter {
         OriginalName = parameter.Name, Name = NetNamingMapper.Capitalize(NetNamingMapper.RemoveInvalidChars(parameter.Name)), Type = NewNetTypeMapper.GetNetType(parameter.Schema), Description = parameter.Description
     });
 }
Example #13
0
 private static GeneratorParameter ConvertAmfParameterToGeneratorParameter(Parameter parameter)
 {
     return(new GeneratorParameter {
         Name = parameter.Name, Type = NewNetTypeMapper.GetNetType(parameter.Schema), Description = parameter.Description
     });
 }
Example #14
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));
        }
Example #15
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);
        }
        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);
        }
 private string GetNamedReturnType(Operation method, EndPoint resource, Payload mimeType, string fullUrl)
 {
     return(NewNetTypeMapper.GetNetType(mimeType.Schema, schemaObjects, schemaResponseObjects, enums));
 }
Example #18
0
        public GeneratorParameter GetRequestParameter(string key, Operation method, EndPoint resource, string fullUrl, IEnumerable <string> defaultMediaTypes)
        {
            if (method.Request == null || !method.Request.Payloads.Any())
            {
                return new GeneratorParameter {
                           Name = "content", Type = "string"
                }
            }
            ;

            var mimeType = GetMimeType(method.Request.Payloads, defaultMediaTypes);
            var type     = NewNetTypeMapper.GetNetType(mimeType, schemaObjects, null, enums);

            if (RamlTypesHelper.IsPrimitiveOrSchemaObject(type, schemaObjects))
            {
                return(new GeneratorParameter //TODO: check
                {
                    Name = string.IsNullOrWhiteSpace(mimeType.Name) ? GetParameterName(type) : GetParameterName(mimeType.Name),
                    Description = mimeType.Description,
                    Type = type
                });
            }

            var apiObjectByKey = GetRequestApiObjectByKey(key);

            if (apiObjectByKey != null)
            {
                return(CreateGeneratorParameter(apiObjectByKey));
            }

            apiObjectByKey = GetRequestApiObjectByKey(NetNamingMapper.GetObjectName(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));
                }
            }

            return(new GeneratorParameter {
                Name = "content", Type = "string"
            });

            //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) };
            //    }
            //}

            //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" };
        }
Example #19
0
        public GeneratorParameter GetRequestParameter(string key, Operation method, EndPoint resource, string fullUrl, IEnumerable <string> defaultMediaTypes)
        {
            if (method.Request == null || !method.Request.Payloads.Any())
            {
                return new GeneratorParameter {
                           Name = "content", Type = "string"
                }
            }
            ;

            var mimeType = GetMimeType(method.Request.Payloads, defaultMediaTypes);
            var type     = NewNetTypeMapper.GetNetType(mimeType, schemaObjects, schemaRequestObjects, enums);

            if (RamlTypesHelper.IsPrimitiveOrSchemaObject(type, schemaObjects) || RamlTypesHelper.IsPrimitiveOrSchemaObject(type, schemaRequestObjects))
            {
                return(new GeneratorParameter
                {
                    Name = string.IsNullOrWhiteSpace(mimeType.Name) ? GetParameterName(type) : GetParameterName(mimeType.Name),
                    Description = mimeType.Description,
                    Type = type
                });
            }

            //var apiObjectByKey = GetRequestApiObjectByKey(key);
            //if (apiObjectByKey != null)
            //    return CreateGeneratorParameter(apiObjectByKey);

            //apiObjectByKey = GetRequestApiObjectByKey(NetNamingMapper.GetObjectName(key));
            //if (apiObjectByKey != null)
            //    return CreateGeneratorParameter(apiObjectByKey);

            var requestKey = key + GeneratorServiceBase.RequestContentSuffix;
            //apiObjectByKey = GetRequestApiObjectByKey(requestKey);
            //if (apiObjectByKey != null)
            //    return CreateGeneratorParameter(apiObjectByKey);
            ApiObject 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));
                }
            }

            return(new GeneratorParameter {
                Name = "content", Type = "string"
            });
        }
Example #20
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);
        }
 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)));
 }
Example #22
0
        private Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> > ParseObject(string key, Shape shape,
                                                                                                    IDictionary <string, ApiObject> existingObjects, bool isRootType)
        {
            var apiObj = new ApiObject
            {
                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.IsArray || apiObj.IsScalar))
            {
                apiObj.Type = NewNetTypeMapper.GetNetType(shape, existingObjects);
            }

            if (shape is NodeShape node && node.Properties.Count() == 1 && 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}>";
                new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums);
            }

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

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

                apiObj.Name = UniquenessHelper.GetUniqueName(existingObjects, apiObj.Name, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());
                foreach (var prop in apiObj.Properties)
                {
                    prop.ParentClassName = apiObj.Name;
                }
            }
            if (existingObjects.Values.Any(o => o.Type == apiObj.Type))
            {
                if (UniquenessHelper.HasSameProperties(apiObj, existingObjects, key, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>()))
                {
                    return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums));
                }

                apiObj.Type = UniquenessHelper.GetUniqueName(existingObjects, apiObj.Type, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>());
            }

            if (!newObjects.ContainsKey(apiObj.Type))
            {
                newObjects.Add(apiObj.Type, apiObj);
            }

            return(new Tuple <IDictionary <string, ApiObject>, IDictionary <string, ApiEnum> >(newObjects, newEnums));
        }