Skip to content

Commit

Permalink
Change default settings (v10) (#806)
Browse files Browse the repository at this point in the history
* Change default settings

* Updated array and dictionary settings

* Fixed tests

* Change default settings

* Fix tests
  • Loading branch information
RicoSuter authored May 20, 2019
1 parent 14678b5 commit 240f601
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public async Task When_array_property_is_not_nullable_then_it_does_not_have_a_se
var code = generator.GenerateFile();

//// Assert
Assert.Contains("public System.Collections.ObjectModel.ObservableCollection<string> Collection { get; } = new System.Collections.ObjectModel.ObservableCollection<string>();", code);
Assert.Contains("public System.Collections.Generic.Dictionary<string, object> Dictionary { get; } = new System.Collections.Generic.Dictionary<string, object>();", code);
Assert.Contains("public System.Collections.Generic.ICollection<string> Collection { get; } = new System.Collections.ObjectModel.Collection<string>();", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> Dictionary { get; } = new System.Collections.Generic.Dictionary<string, object>();", code);
}
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task When_array_item_is_nullable_then_generated_CSharp_is_correct()

// Assert
Assert.True(schema.Properties["Items"].Item.IsNullable(SchemaType.JsonSchema));
Assert.Contains("System.Collections.ObjectModel.ObservableCollection<int?> Items", output);
Assert.Contains("System.Collections.Generic.ICollection<int?> Items", output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public async Task When_dictionary_key_is_enum_then_csharp_has_enum_key()
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public System.Collections.Generic.Dictionary<PropertyName, string> EnumDictionary\n", code);
Assert.Contains("public System.Collections.Generic.Dictionary<PropertyName, string> EnumInterfaceDictionary\n", code);
Assert.Contains("public System.Collections.Generic.IDictionary<PropertyName, string> EnumDictionary ", code);
Assert.Contains("public System.Collections.Generic.IDictionary<PropertyName, string> EnumInterfaceDictionary ", code);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("private MyClassStatus? _status;", code);
Assert.Contains("public MyClassStatus? Status { get; set; }", code);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_
var output = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public System.Collections.ObjectModel.ObservableCollection<object> EmptySchema { get; set; } = ", output);
Assert.Contains("public System.Collections.Generic.ICollection<object> EmptySchema { get; set; } = ", output);

AssertCompile(output);
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public async Task When_schema_contains_ref_to_definition_that_refs_another_defin
var output = gen.GenerateFile("MyClass");

//// Assert
Assert.Contains("public System.Collections.ObjectModel.ObservableCollection<PRef>", output);
Assert.Contains("public System.Collections.Generic.ICollection<PRef>", output);

AssertCompile(output);
}
Expand Down Expand Up @@ -252,7 +252,7 @@ public async Task When_allOf_contains_one_schema_then_csharp_inheritance_is_gene
var output = generator.GenerateFile("Teacher");

//// Assert
Assert.Contains(@"class Teacher : Person, ", output);
Assert.Contains(@"class Teacher : Person", output);

AssertCompile(output);
}
Expand Down Expand Up @@ -612,7 +612,7 @@ public async Task When_patternProperties_is_set_with_string_value_type_then_corr
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public System.Collections.Generic.Dictionary<string, string> Dict { get; set; } = new System.Collections.Generic.Dictionary<string, string>();", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, string> Dict { get; set; } = new System.Collections.Generic.Dictionary<string, string>();", code);

AssertCompile(code);
}
Expand Down Expand Up @@ -739,8 +739,8 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public System.Collections.ObjectModel.ObservableCollection<string> A { get; set; } = new System.Collections.ObjectModel.ObservableCollection<string>();", code);
Assert.DoesNotContain("public System.Collections.ObjectModel.ObservableCollection<string> B { get; set; } = new System.Collections.ObjectModel.ObservableCollection<string>();", code);
Assert.Contains("public System.Collections.Generic.ICollection<string> A { get; set; } = new System.Collections.ObjectModel.Collection<string>();", code);
Assert.DoesNotContain("public System.Collections.Generic.ICollection<string> B { get; set; } = new System.Collections.ObjectModel.Collection<string>();", code);

AssertCompile(code);
}
Expand Down Expand Up @@ -785,8 +785,8 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public System.Collections.Generic.Dictionary<string, string> A { get; set; } = new System.Collections.Generic.Dictionary<string, string>();", code);
Assert.DoesNotContain("public System.Collections.Generic.Dictionary<string, string> B { get; set; } = new System.Collections.Generic.Dictionary<string, string>();", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, string> A { get; set; } = new System.Collections.Generic.Dictionary<string, string>();", code);
Assert.DoesNotContain("public System.Collections.Generic.IDictionary<string, string> B { get; set; } = new System.Collections.Generic.Dictionary<string, string>();", code);

AssertCompile(code);
}
Expand Down Expand Up @@ -890,7 +890,7 @@ public async Task When_property_is_ObservableCollection_then_generated_code_uses
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<ObsClass>();
var settings = new CSharpGeneratorSettings();
var settings = new CSharpGeneratorSettings { ArrayType = "ObservableCollection" };
var generator = new CSharpGenerator(schema, settings);

//// Act
Expand Down Expand Up @@ -1502,11 +1502,11 @@ public async Task When_tuple_types_has_ints_then_it_is_generated_correctly(bool

if (inlineNamedTuples)
{
Assert.Contains("ObservableCollection<System.Tuple<int, int>>", code);
Assert.Contains("Collection<System.Tuple<int, int>>", code);
}
else
{
Assert.Contains("ObservableCollection<InnerList>", code);
Assert.Contains("Collection<InnerList>", code);
Assert.Contains("partial class InnerList : System.Tuple<int, int>", code);
}

Expand Down
17 changes: 10 additions & 7 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ public CSharpGeneratorSettings()
{
Namespace = "MyNamespace";

DateType = "System.DateTime";
DateTimeType = "System.DateTime";
DateType = "System.DateTimeOffset";
DateTimeType = "System.DateTimeOffset";
TimeType = "System.TimeSpan";
TimeSpanType = "System.TimeSpan";

ArrayType = "System.Collections.ObjectModel.ObservableCollection";
DictionaryType = "System.Collections.Generic.Dictionary";
ArrayType = "System.Collections.Generic.ICollection";
ArrayInstanceType = "System.Collections.ObjectModel.Collection";
ArrayBaseType = "System.Collections.ObjectModel.Collection";

ArrayBaseType = "System.Collections.ObjectModel.ObservableCollection";
DictionaryType = "System.Collections.Generic.IDictionary";
DictionaryInstanceType = "System.Collections.Generic.Dictionary";
DictionaryBaseType = "System.Collections.Generic.Dictionary";

ClassStyle = CSharpClassStyle.Poco;

RequiredPropertiesMustBeDefined = true;
GenerateDataAnnotations = true;
ClassStyle = CSharpClassStyle.Inpc;
TypeAccessModifier = "public";
PropertySetterAccessModifier = string.Empty;
GenerateJsonMethods = true;
GenerateJsonMethods = false;
EnforceFlagEnums = false;

ValueGenerator = new CSharpValueGenerator(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public async Task When_enum_has_string_value_then_TS_code_has_string_value()
var schemaData = schema.ToJson();

//// Act
var generator = new TypeScriptGenerator(schema);
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 1.8m });
var code = generator.GenerateFile("MyClass");

//// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public async Task When_generating_TypeScript_classes_then_output_is_correct()
[Fact]
public async Task When_default_value_is_available_then_variable_is_initialized()
{
var code = await PrepareAsync(new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class });
var code = await PrepareAsync(new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
TypeScriptVersion = 1.8m
});

//// Assert
Assert.Contains("name: string;", code);
Expand Down Expand Up @@ -121,7 +125,8 @@ public void When_array_property_is_required_or_not_then_the_code_has_correct_ini
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
SchemaType = SchemaType.Swagger2
SchemaType = SchemaType.Swagger2,
TypeScriptVersion = 1.8m
});
var code = generator.GenerateFile("MyClass");

Expand Down Expand Up @@ -166,7 +171,8 @@ public void When_dictionary_property_is_required_or_not_then_the_code_has_correc
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
SchemaType = SchemaType.Swagger2
SchemaType = SchemaType.Swagger2,
TypeScriptVersion = 1.8m
});
var code = generator.GenerateFile("MyClass");

Expand Down Expand Up @@ -219,7 +225,8 @@ public void When_object_property_is_required_or_not_then_the_code_has_correct_in
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
SchemaType = SchemaType.Swagger2
SchemaType = SchemaType.Swagger2,
TypeScriptVersion = 1.8m
});
var code = generator.GenerateFile("MyClass");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public async Task When_constructor_interface_and_conversion_code_is_generated_th
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
GenerateConstructorInterface = true,
ConvertConstructorInterfaceData = true
ConvertConstructorInterfaceData = true,
TypeScriptVersion = 1.8m
});

var output = generator.GenerateFile("MyClass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public async Task When_property_is_dto_dictionary_then_assignment_may_create_new
var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
NullValue = TypeScriptNullValue.Null
NullValue = TypeScriptNullValue.Null,
TypeScriptVersion = 1.8m
});
var code = codeGenerator.GenerateFile("Test");

Expand Down Expand Up @@ -188,7 +189,8 @@ public async Task When_property_is_string_dictionary_then_assignment_is_correct(
var codeGenerator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
NullValue = TypeScriptNullValue.Undefined
NullValue = TypeScriptNullValue.Undefined,
TypeScriptVersion = 1.8m
});
var code = codeGenerator.GenerateFile("Test");

Expand Down Expand Up @@ -222,7 +224,8 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener
TypeStyle = TypeScriptTypeStyle.Class,
NullValue = TypeScriptNullValue.Undefined,
ConvertConstructorInterfaceData = convertConstructorInterfaceData,
InlineNamedDictionaries = inlineNamedDictionaries
InlineNamedDictionaries = inlineNamedDictionaries,
TypeScriptVersion = 1.8m
});
var code = codeGenerator.GenerateFile("Test");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public async Task When_dictionary_key_is_enum_then_typescript_has_string_key()
var data = schema.ToJson();

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

//// Assert
Expand Down Expand Up @@ -76,7 +80,11 @@ public async Task When_dictionary_value_is_enum_then_typescript_has_enum_value()
var data = schema.ToJson();

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

//// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_a

//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class });
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
TypeScriptVersion = 1.8m
});
var code = generator.GenerateFile("Foo").Replace("\r\n", "\n");

//// Assert
Expand Down Expand Up @@ -222,7 +226,11 @@ private static async Task<TypeScriptGenerator> CreateGeneratorAsync()
{
var schema = await JsonSchema4.FromTypeAsync<Teacher>();
var schemaData = schema.ToJson();
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface });
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Interface,
TypeScriptVersion = 1.8m
});
return generator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public async Task When_property_is_object_then_jsonProperty_has_no_reference_and
var data = schema.ToJson();

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

//// Assert
Expand All @@ -40,7 +44,11 @@ public async Task When_dictionary_value_is_object_then_typescript_uses_any()
var data = schema.ToJson();

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

//// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public TypeScriptGeneratorSettings()
TypeStyle = TypeScriptTypeStyle.Class;
DateTimeType = TypeScriptDateTimeType.Date;
ExtensionCode = string.Empty;
TypeScriptVersion = 1.8m;
TypeScriptVersion = 2.7m;
GenerateConstructorInterface = true;
ConvertConstructorInterfaceData = false;
ExportTypes = true;
Expand Down

0 comments on commit 240f601

Please sign in to comment.