Esempio n. 1
0
        /// <summary>Gets or generates the type name for the given schema.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The type name.</returns>
        public virtual string GetOrGenerateTypeName(JsonSchema4 schema, string typeNameHint)
        {
            schema = schema.ActualSchema;

            if (!_generatedTypeNames.ContainsKey(schema))
            {
                var typeName = schema.GetTypeName(_typeNameGenerator, typeNameHint);

                if (string.IsNullOrEmpty(typeName))
                {
                    typeName = GenerateTypeName(typeNameHint);
                }
                else
                {
                    if (_generatedTypeNames.ContainsValue(typeName))
                    {
                        typeName = GenerateTypeName(typeName);
                    }
                }

                typeName = typeName
                           .Replace("[", "Of")
                           .Replace("]", string.Empty)
                           .Replace(",", "And")
                           .Replace(" ", string.Empty);

                _generatedTypeNames[schema] = ConversionUtilities.ConvertToUpperCamelCase(typeName, true);
            }

            return(_generatedTypeNames[schema]);
        }
Esempio n. 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()
                });
            }
        }
Esempio n. 3
0
        /// <summary>Appends the schema to the root object.</summary>
        /// <param name="objectToAppend">The object to append.</param>
        public void Append(JsonSchema4 objectToAppend)
        {
            var typeName = objectToAppend.GetTypeName(_typeNameGenerator, string.Empty);

            if (!string.IsNullOrEmpty(typeName) && !_document.Definitions.ContainsKey(typeName))
            {
                _document.Definitions[typeName] = objectToAppend;
            }
            else
            {
                _document.Definitions["ref_" + Guid.NewGuid().ToString().Replace("-", "_")] = objectToAppend;
            }
        }
Esempio n. 4
0
 /// <summary>Appends the schema to the root object.</summary>
 /// <param name="schema">The schema to append.</param>
 /// <param name="typeNameHint">The type name hint.</param>
 public override void AppendSchema(JsonSchema4 schema, string typeNameHint)
 {
     if (!_document.Definitions.Values.Contains(schema))
     {
         var typeName = schema.GetTypeName(_typeNameGenerator, typeNameHint);
         if (!string.IsNullOrEmpty(typeName) && !_document.Definitions.ContainsKey(typeName))
         {
             _document.Definitions[typeName] = schema;
         }
         else
         {
             _document.Definitions["ref_" + Guid.NewGuid().ToString().Replace("-", "_")] = schema;
         }
     }
 }
        /// <summary>Gets or generates the type name for the given schema.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The type name.</returns>
        protected virtual string GetOrGenerateTypeName(JsonSchema4 schema, string typeNameHint)
        {
            var typeName = schema.GetTypeName(_typeNameGenerator);

            if (string.IsNullOrEmpty(typeName))
            {
                if (!_generatedTypeNames.ContainsKey(schema))
                {
                    _generatedTypeNames[schema] = GenerateTypeName(typeNameHint);
                }

                return(_generatedTypeNames[schema]);
            }

            return(typeName);
        }
Esempio n. 6
0
        /// <summary>Generates the type.</summary>
        /// <param name="fallbackTypeName">The fallback type name when TypeName is not available on schema.</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)
            {
                return(GenerateEnum(typeName));
            }
            else
            {
                return(GenerateClass(typeName));
            }
        }
Esempio n. 7
0
        /// <summary>Gets or generates the type name for the given schema.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The type name.</returns>
        public virtual string GetOrGenerateTypeName(JsonSchema4 schema, string typeNameHint)
        {
            schema = schema.ActualSchema;

            if (!_generatedTypeNames.ContainsKey(schema))
            {
                var typeName = schema.GetTypeName(_typeNameGenerator);
                if (!string.IsNullOrEmpty(typeName))
                {
                    _generatedTypeNames[schema] = typeName;
                }
                else
                {
                    _generatedTypeNames[schema] = GenerateTypeName(typeNameHint);
                }
            }

            return(_generatedTypeNames[schema]);
        }
Esempio n. 8
0
        /// <summary>Gets or generates the type name for the given schema.</summary>
        /// <param name="schema">The schema.</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The type name.</returns>
        public virtual string GetOrGenerateTypeName(JsonSchema4 schema, string typeNameHint)
        {
            schema = schema.ActualSchema;

            if (!_generatedTypeNames.ContainsKey(schema))
            {
                var typeName = schema.GetTypeName(_typeNameGenerator);

                if (string.IsNullOrEmpty(typeName))
                {
                    typeName = GenerateTypeName(typeNameHint);
                }

                typeName = Regex.Replace(typeName, "^(.*?)\\[(.*?)\\]$",
                                         match => match.Groups[1].Value + "Of" + match.Groups[2].Value);

                _generatedTypeNames[schema] = typeName;
            }

            return(_generatedTypeNames[schema]);
        }