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);
        }
        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);
        }
Exemple #3
0
        public void SetProperties(HttpResponseHeaders headers)
        {
            var properties = this.GetType().GetProperties().Where(p => p.GetValue(this) != null);

            foreach (var prop in properties.Where(prop => headers.Any(h => h.Key == prop.Name)))
            {
                prop.SetValue(this, headers.First(h => NetNamingMapper.GetPropertyName(h.Key) == prop.Name));
            }
        }
 private static Property CreateProperty(Newtonsoft.JsonV4.Schema.JsonSchema schema, KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property, bool isEnum, string enumName)
 {
     return(new Property
     {
         Name = NetNamingMapper.GetPropertyName(property.Key),
         Type = GetType(property, isEnum, enumName, schema.Required),
         OriginalName = property.Key,
         Description = property.Value.Description,
         IsEnum = isEnum,
         Required = schema.Required != null && schema.Required.Contains(property.Key),
         MaxLength = property.Value.MaximumLength,
         MinLength = property.Value.MinimumLength,
         Maximum = property.Value.Maximum,
         Minimum = property.Value.Minimum
     });
 }
 private static Property CreateProperty(string key, string type, KeyValuePair <string, JsonSchema> property, bool isEnum)
 {
     return(new Property
     {
         Name = NetNamingMapper.GetPropertyName(key),
         OriginalName = key,
         Type = type,
         Description = property.Value.Description,
         IsEnum = isEnum,
         MaxLength = property.Value.MaximumLength,
         MinLength = property.Value.MinimumLength,
         Maximum = property.Value.Maximum,
         Minimum = property.Value.Minimum,
         Required = property.Value.Required != null && property.Value.Required.Value
     });
 }
        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);
        }
        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);
        }
 public void Should_Avoid_Brackets_In_Property_Name()
 {
     Assert.Equal("Sales", NetNamingMapper.GetPropertyName("sales[]"));
     Assert.Equal("Salesperson", NetNamingMapper.GetPropertyName("(sales|person)[]"));
 }
 public void Should_Remove_QuestionMark_From_Property_Names()
 {
     Assert.Equal("Optional", NetNamingMapper.GetPropertyName("optional?"));
 }
 public void Should_Convert_Property_Names()
 {
     Assert.Equal("XRateMediaAbcDef", NetNamingMapper.GetPropertyName("X-Rate-Media:Abc/Def"));
 }