private TypeGeneratorResult GenerateEnum(string typeName)
        {
            var template = new EnumTemplate() as ITemplate;

            template.Initialize(new EnumTemplateModel(typeName, _schema));
            return(new TypeGeneratorResult
            {
                TypeName = typeName,
                Code = template.Render()
            });
        }
Example #2
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()
                });
            }
        }