Exemple #1
0
        private void WriteProperty(StreamWriter writer, CSharpProperty property, int propertyOrder)
        {
            if (!string.IsNullOrEmpty(property.Comment))
            {
                WritePropertyComment(writer, property.Comment);
            }

            var optionalDataType =
                (
                    property.Type == CSharpDataTypes.Boolean ||
                    property.Type == CSharpDataTypes.DateTime ||
                    property.Type == CSharpDataTypes.Decimal ||
                    property.Type == CSharpDataTypes.Integer
                ) ? property.Type + "?" : property.Type;

            if (property.Required)
            {
                writer.WriteLine(Tab(2) + "[DataMember(IsRequired = true, Order = {0})]", propertyOrder);
                writer.WriteLine(Tab(2) + "[JsonProperty(Required = Required.Always, Order = {0})]", propertyOrder);
                writer.WriteLine(Tab(2) + "public {0} {1} {{ get; set; }}", property.Type, property.Name);
            }
            else
            {
                writer.WriteLine(Tab(2) + "[DataMember(EmitDefaultValue = false, Order = {0})]", propertyOrder);
                writer.WriteLine(Tab(2) + "[JsonProperty(NullValueHandling = NullValueHandling.Ignore, Order = {0})]", propertyOrder);
                writer.WriteLine(Tab(2) + "public {0} {1} {{ get; set; }}", optionalDataType, property.Name);
            }
            writer.WriteLine();
        }
        protected override CSharpType RamlTypeToSchemaType(RamlType ramlType, ConversionOptions options)
        {
            if (ramlType == null)
            {
                return(null);
            }

            var cSharpType = new CSharpType();

            cSharpType.Name = ramlType.Name;

            // check if it is enum
            if (ramlType.Enum != null)
            {
                cSharpType.Enum = new CSharpEnum();

                var enumTypeName = RamlDataTypeToCSharpDataType(ramlType.Enum.ItemsTypeName);

                if (enumTypeName == null)
                {
                    enumTypeName = GetRefDataType(ramlType.Enum.ItemsTypeName);
                }

                cSharpType.Enum.EnumValues = new List <string>();

                foreach (string enumValue in ramlType.Enum.EnumValues)
                {
                    cSharpType.Enum.EnumValues.Add(enumValue);
                }

                return(cSharpType);
            }

            // check if it is array
            if (ramlType.Array != null)
            {
                cSharpType.Array = new CSharpArray();

                cSharpType.Array.ItemName = ramlType.Array.ItemName;

                var arrayElementTypeName = RamlDataTypeToCSharpDataType(ramlType.Array.ItemsTypeName);

                if (arrayElementTypeName == null)
                {
                    arrayElementTypeName = GetRefDataType(ramlType.Array.ItemsTypeName);
                }

                if (arrayElementTypeName != null)
                {
                    cSharpType.Array.ItemsTypeName = arrayElementTypeName;
                }

                return(cSharpType);
            }

            if (ramlType.Description != null && options.GenerateDescriptions)
            {
                cSharpType.Comment = ramlType.Description;
            }

            if (ramlType.Properties == null || ramlType.Properties.Count == 0)
            {
                return(cSharpType);
            }

            cSharpType.Properties = new List <CSharpProperty>();

            foreach (RamlProperty ramlProperty in ramlType.Properties)
            {
                var csharpProperty = new CSharpProperty();
                csharpProperty.Name = ramlProperty.Name;

                var typeName = RamlDataTypeToCSharpDataType(ramlProperty.Type);

                if (typeName == null)
                {
                    typeName = GetRefDataType(ramlProperty.Type);
                }

                if (typeName != null)
                {
                    csharpProperty.Type = typeName;
                }

                if (ramlProperty.Description != null && options.GenerateDescriptions)
                {
                    csharpProperty.Comment = ramlProperty.Description;
                }

                csharpProperty.Required = ramlProperty.Required;

                cSharpType.Properties.Add(csharpProperty);
            }

            return(cSharpType);
        }