private static TypeScriptGenerator CreateGenerator()
 {
     var schema = JsonSchema4.FromType<Teacher>();
     var schemaData = schema.ToJson();
     var generator = new TypeScriptGenerator(schema);
     return generator;
 }
 public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host)
 {
     var schema = JsonSchema4.FromJson(InputJson);
     var generator = new TypeScriptGenerator(schema);
     var code = generator.GenerateFile();
     WriteOutput(host, code);
 }
Esempio n. 3
0
 public void Demo()
 {
     var schema = JsonSchema4.FromType<Person>();
     var schemaJsonData = schema.ToJson();
     var errors = schema.Validate("{}");
     var generator = new TypeScriptGenerator(schema);
     var code = generator.GenerateFile();
 }
        public void When_property_has_description_then_csharp_has_xml_comment()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Teacher>();
            schema.Properties["Class"].Description = "PropertyDesc.";
            var generator = new TypeScriptGenerator(schema);

            //// Act
            var output = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(output.Contains(@"/** PropertyDesc. *"));
        }
        public void When_enum_has_description_then_typescript_has_comment()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Teacher>();
            schema.AllOf.First().Properties["Gender"].Description = "EnumDesc.";
            var generator = new TypeScriptGenerator(schema);

            //// Act
            var output = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(output.Contains(@"/** EnumDesc. *"));
        }
        public void When_class_has_description_then_typescript_has_comment()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Teacher>();
            schema.Description = "ClassDesc.";
            var generator = new TypeScriptGenerator(schema);

            //// Act
            var output = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(output.Contains(@"/** ClassDesc. *"));
        }
        private static string Prepare(TypeScriptTypeStyle style)
        {
            var schema = JsonSchema4.FromType<MyClassTest>();
            var data = schema.ToJson();
            var settings = new TypeScriptGeneratorSettings
            {
                TypeStyle = style
            };

            //// Act
            var generator = new TypeScriptGenerator(schema, settings);
            var code = generator.GenerateFile();
            return code;
        }
        public void When_dictionary_value_is_enum_then_typescript_has_enum_value()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<EnumValueDictionaryTest>();
            var data = schema.ToJson();

            //// Act
            var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("Mapping: { [key: string] : Gender; };"));
            Assert.IsTrue(code.Contains("Mapping2: { [key: string] : Gender; };"));
        }
        public void When_string_and_integer_enum_used_then_two_enums_are_generated_in_typescript()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<StringAndIntegerEnumTestClass>(new JsonSchemaGeneratorSettings
            {
                DefaultEnumHandling = EnumHandling.Integer
            });
            var data = schema.ToJson();

            //// Act
            var generator = new TypeScriptGenerator(schema);
            var code = generator.GenerateFile();

            //// Assert
            Assert.AreEqual(3, code.Split(new[] { "export enum " }, StringSplitOptions.None).Count()); // two found
        }
        public void When_date_handling_is_moment_then_moment_property_are_generated_in_interface()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithDateProperty>();

            //// Act
            var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
            {
                TypeStyle = TypeScriptTypeStyle.Interface,
                DateTimeType = TypeScriptDateTimeType.MomentJS
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("MyDateTime: moment.Moment;"));
        }
        public void When_date_handling_is_moment_then_moment_property_are_generated_in_class()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithDateProperty>();

            //// Act
            var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
            {
                TypeStyle = TypeScriptTypeStyle.Class,
                DateTimeType = TypeScriptDateTimeType.MomentJS
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("myDateTime: moment.Moment"));
            Assert.IsTrue(code.Contains("this.myDateTime = data[\"MyDateTime\"] ? moment(data[\"MyDateTime\"].toString()) : null;"));
            Assert.IsTrue(code.Contains("data[\"MyDateTime\"] = this.myDateTime ? this.myDateTime.toISOString() : null;"));
        }
Esempio n. 12
0
        public void When_enum_has_integer_value_then_TS_code_has_string_value()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithIntegerEnum>();
            var schemaData = schema.ToJson();

            //// Act
            var generator = new TypeScriptGenerator(schema);
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("_0562 = 10, "));
            Assert.IsTrue(code.Contains("_0532 = 15, "));
        }
        public void When_date_handling_is_string_then_string_property_are_generated_in_class()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithDateProperty>();

            //// Act
            var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
            {
                TypeStyle = TypeScriptTypeStyle.Class,
                DateTimeType = TypeScriptDateTimeType.String
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("myDateTime: string"));
            Assert.IsTrue(code.Contains("this.myDateTime = data[\"MyDateTime\"] !== undefined ? data[\"MyDateTime\"] : null;"));
            Assert.IsTrue(code.Contains("data[\"MyDateTime\"] = this.myDateTime !== undefined ? this.myDateTime : null;"));
        }
        public void When_schema_contains_discriminator_and_inheritance_hierarchy_then_TypeScript_is_correctly_generated()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Container>();
            var json = schema.ToJson();

            //// Act
            var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
            {
                TypeStyle = TypeScriptTypeStyle.Class
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("export class Container"));
            Assert.IsTrue(code.Contains("export class Animal"));
            Assert.IsTrue(code.Contains("export class Dog"));

            // discriminator is available for deserialization
            Assert.IsTrue(code.Contains("private discriminator: string;")); // discriminator must be private
            Assert.IsTrue(code.Contains("return <any>eval(\"new \" + data[\"discriminator\"] + \"(data)\");")); // type is chosen by discriminator

            // discriminator is assign for serialization
            Assert.IsTrue(code.Contains("this.discriminator = \"Animal\""));
            Assert.IsTrue(code.Contains("this.discriminator = \"Dog\""));
        }