Exemplo n.º 1
0
        public EnumTemplateModel(string typeName, JsonSchema4 schema)
        {
            Name  = typeName;
            Enums = GetEnumeration(schema);

            HasDescription = !(schema is JsonProperty) && !string.IsNullOrEmpty(schema.Description);
            Description    = ConversionUtilities.RemoveLineBreaks(schema.Description);
        }
Exemplo n.º 2
0
 internal PropertyModel(JsonProperty property, CSharpTypeResolver resolver, CSharpGeneratorSettings settings) : base(property)
 {
     Name           = property.Name;
     HasDescription = !string.IsNullOrEmpty(property.Description);
     Description    = ConversionUtilities.RemoveLineBreaks(property.Description);
     PropertyName   = ConversionUtilities.ConvertToUpperCamelCase(property.Name);
     FieldName      = ConversionUtilities.ConvertToLowerCamelCase(property.Name);
     Required       = property.IsRequired && settings.RequiredPropertiesMustBeDefined ? "Required.Always" : "Required.Default";
     IsStringEnum   = property.ActualPropertySchema.IsEnumeration && property.ActualPropertySchema.Type == JsonObjectType.String;
     Type           = resolver.Resolve(property.ActualPropertySchema, property.IsNullable, property.Name);
 }
Exemplo n.º 3
0
        /// <summary>Generates the type.</summary>
        /// <param name="fallbackTypeName">The fallback type name.</param>
        /// <returns>The code.</returns>
        public override TypeGeneratorResult GenerateType(string fallbackTypeName)
        {
            var typeName = _schema.GetTypeName(Settings.TypeNameGenerator);

            if (string.IsNullOrEmpty(typeName))
            {
                typeName = fallbackTypeName;
            }

            if (_schema.IsEnumeration)
            {
                if (_schema.Type == JsonObjectType.Integer)
                {
                    typeName = typeName + "AsInteger";
                }

                var template = new EnumTemplate() as ITemplate;
                template.Initialize(new EnumTemplateModel(typeName, _schema));
                return(new TypeGeneratorResult
                {
                    TypeName = typeName,
                    Code = template.Render()
                });
            }
            else
            {
                var properties     = _schema.Properties.Values.Select(property => new PropertyModel(property, typeName, _resolver, Settings)).ToList();
                var hasInheritance = _schema.AllOf.Count == 1;
                var baseClass      = hasInheritance ? _resolver.Resolve(_schema.AllOf.First(), true, string.Empty) : null;

                var template = Settings.CreateTemplate(typeName);
                template.Initialize(new // TODO: Create model class
                {
                    Class     = Settings.ExtendedClasses?.Contains(typeName) == true ? typeName + "Base" : typeName,
                    RealClass = typeName,

                    HasDescription = !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description),
                    Description    = ConversionUtilities.RemoveLineBreaks(_schema.Description),

                    HasInheritance = hasInheritance,
                    Inheritance    = hasInheritance ? " extends " + baseClass : string.Empty,
                    Properties     = properties
                });

                return(new TypeGeneratorResult
                {
                    TypeName = typeName,
                    BaseTypeName = baseClass,
                    Code = template.Render()
                });
            }
        }
Exemplo n.º 4
0
        public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings, CSharpTypeResolver resolver, JsonSchema4 schema, IEnumerable <PropertyModel> properties)
        {
            Class     = typeName;
            Namespace = settings.Namespace;

            HasDescription = !(schema is JsonProperty) && !string.IsNullOrEmpty(schema.Description);
            Description    = ConversionUtilities.RemoveLineBreaks(schema.Description);

            Inpc = settings.ClassStyle == CSharpClassStyle.Inpc;

            var hasInheritance = schema.AllOf.Count == 1;

            HasInheritance = hasInheritance;
            Inheritance    = GenerateInheritanceCode(settings, resolver, schema, hasInheritance);

            Properties = properties;
        }