public void When_property_is_timespan_than_csharp_timespan_is_used()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Person>();
            var generator = new CSharpGenerator(schema);

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

            //// Assert
            Assert.IsTrue(output.Contains(@"public TimeSpan TimeSpan"));
        }
        public void When_enum_has_no_names_and_string_value_starts_with_number_then_underline_is_generated()
        {
            //// Arrange
            var schemaData = @"{
              ""type"": ""object"",
              ""properties"": {
            ""Bar"": {
              ""oneOf"": [
            {
              ""$ref"": ""#/definitions/StringEnum""
            }
              ]
            }
              },
              ""definitions"": {
            ""StringEnum"": {
              ""type"": ""string"",
              ""enum"": [
            ""0562"",
            ""0532""
              ],
              ""description"": """"
            }
              }
            }";
            var schema = JsonSchema4.FromJson(schemaData);

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

            //// Assert
            Assert.IsTrue(code.Contains("[EnumMember(Value = \"0562\")]"));
            Assert.IsTrue(code.Contains("_0562 = 0,"));
            Assert.IsTrue(code.Contains("[EnumMember(Value = \"0532\")]"));
            Assert.IsTrue(code.Contains("_0532 = 1,"));
        }
        public void When_schema_contains_discriminator_and_inheritance_hierarchy_then_CSharp_is_correctly_generated()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Container>();

            //// Act
            var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsFalse(code.Contains("public string Discriminator")); // discriminator property is not generated
            Assert.IsTrue(code.Contains("[JsonConverter(typeof(JsonInheritanceConverter), \"discriminator\")]")); // attribute is generated
            Assert.IsTrue(code.Contains("class JsonInheritanceConverter")); // converter is generated
        }
 public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host)
 {
     var schema = JsonSchema4.FromJson(InputJson);
     var generator = new CSharpGenerator(schema);
     generator.Namespace = Namespace; 
     var code = generator.GenerateFile();
     WriteOutput(host, code);
 }
Example #5
-1
        public override Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            var schema = JsonSchema4.FromJson(InputJson);
            var generator = new CSharpGenerator(schema, Settings);

            var code = generator.GenerateFile(Name); 
            if (TryWriteFileOutput(host, () => code) == false)
                return Task.FromResult<object>(code);

            return Task.FromResult<object>(null);
        }
        public void When_enum_has_description_then_csharp_has_xml_comment()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<Teacher>();
            schema.AllOf.First().Properties["Gender"].Description = "EnumDesc.";
            var generator = new CSharpGenerator(schema);

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

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

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

            //// Assert
            Assert.IsTrue(output.Contains(@"/// <summary>ClassDesc.</summary>"));
        }
 public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host)
 {
     var settings = new CSharpGeneratorSettings
     {
         Namespace = Namespace,
         RequiredPropertiesMustBeDefined = RequiredPropertiesMustBeDefined,
         DateTimeType = DateTimeType
     };
     
     var schema = JsonSchema4.FromJson(InputJson);
     var generator = new CSharpGenerator(schema, settings);
     var code = generator.GenerateFile();
     WriteOutput(host, code);
 }
        public void When_enum_has_integer_value_then_CS_code_has_EnumMember_attribute()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithIntegerEnum>();
            var schemaData = schema.ToJson();

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

            //// Assert
            Assert.IsFalse(code.Contains("[EnumMember(Value = \"0562\")]"));
            Assert.IsTrue(code.Contains("_0562 = 10,"));
            Assert.IsFalse(code.Contains("[EnumMember(Value = \"0532\")]"));
            Assert.IsTrue(code.Contains("_0532 = 15,"));
        }
        public void When_byte_enum_is_generated_then_no_exception_occurs()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<DifferentEnumTypeTestClass>(new JsonSchemaGeneratorSettings
            {
                DefaultEnumHandling = EnumHandling.Integer
            });
            var data = schema.ToJson();

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

            //// Assert
            // No exception
        }
        public void When_property_has_default_attribute_then_default_value_is_set_in_generated_Poco_CSharp_code()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<DefaultPropertyGenerationClass>(new JsonSchemaGeneratorSettings
            {
                DefaultEnumHandling = EnumHandling.Integer
            });

            //// Act
            var generator = new CSharpGenerator(schema);
            generator.Settings.ClassStyle = CSharpClassStyle.Poco;
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("public string Test { get; set; } = \"foo\";"));
        }
        public void When_property_is_required_then_required_attribute_is_rendered()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithRequiredObject>();
            var schemaData = schema.ToJson();

            //// Act
            var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
            {
                ClassStyle = CSharpClassStyle.Poco
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("[Required]"));
            Assert.IsTrue(code.Contains("public string Property { get; set; }"));
        }
        public void When_string_and_integer_enum_used_then_one_enum_is_generated_in_CSharp()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<StringAndIntegerEnumTestClass>(new JsonSchemaGeneratorSettings
            {
                DefaultEnumHandling = EnumHandling.Integer
            });
            var data = schema.ToJson();

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

            //// Assert
            Assert.IsTrue(code.IndexOf("B = 5,") > 0); // B must be 5 even if B = 1 is first defined
            Assert.AreEqual(2, code.Split(new[] { "public enum " }, StringSplitOptions.None).Count()); // one found
            Assert.AreEqual(3, code.Split(new[] { "[JsonConverter(typeof(StringEnumConverter))]" }, StringSplitOptions.None).Count()); // two found
        }
        public void When_property_is_not_required_then_required_attribute_is_not_rendered_in_Swagger_mode()
        {
            //// Arrange
            var schema = JsonSchema4.FromType<ClassWithoutRequiredObject>(new JsonSchemaGeneratorSettings
            {
                NullHandling = NullHandling.Swagger
            });
            var schemaData = schema.ToJson();

            //// Act
            var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
            {
                ClassStyle = CSharpClassStyle.Poco,
                NullHandling = NullHandling.Swagger
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsFalse(code.Contains("[Required]"));
            Assert.IsTrue(code.Contains("public string Property { get; set; }"));
        }