Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional options for property naming. #1404

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration.CSharp.Tests/AllOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public async Task When_all_of_has_multiple_refs_then_the_properties_should_expan

//// Act
var schema = await JsonSchema.FromJsonAsync(json);
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns" };
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns", PropertyNamingStyle = CSharpNamingStyle.PascalCase };
var generator = new CSharpGenerator(schema, settings);
var output = generator.GenerateFile("Foo");

Expand Down
3 changes: 2 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp.Tests/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public async Task When_array_property_is_required_then_array_instance_can_be_cha
{
ClassStyle = CSharpClassStyle.Poco,
ArrayType = "Foo",
ArrayInstanceType = "Bar"
ArrayInstanceType = "Bar",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public async Task When_property_has_interger_default_it_is_reflected_in_the_poco
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "ns",
GenerateDefaultValues = true
GenerateDefaultValues = true,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
};
var gen = new CSharpGenerator(schema, settings);
var output = gen.GenerateFile("MyClass");
Expand All @@ -44,7 +45,8 @@ public async Task When_property_has_boolean_default_it_is_reflected_in_the_poco(
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "ns",
GenerateDefaultValues = true
GenerateDefaultValues = true,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
};
var gen = new CSharpGenerator(schema, settings);
var output = gen.GenerateFile("MyClass");
Expand All @@ -67,7 +69,8 @@ public async Task When_property_has_boolean_default_and_default_value_generation
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "ns",
GenerateDefaultValues = false
GenerateDefaultValues = false,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
};
var gen = new CSharpGenerator(schema, settings);
var output = gen.GenerateFile("MyClass");
Expand All @@ -93,6 +96,7 @@ public async Task When_generating_CSharp_code_then_default_value_generates_expec
//// Act
var settings = new CSharpGeneratorSettings();
settings.GenerateDefaultValues = true;
settings.PropertyNamingStyle = CSharpNamingStyle.PascalCase;

var generator = new CSharpGenerator(document, settings);
var code = generator.GenerateFile();
Expand All @@ -119,6 +123,7 @@ public async Task When_generating_CSharp_code_then_default_value_with_decimal_ge
//// Act
var settings = new CSharpGeneratorSettings();
settings.GenerateDefaultValues = true;
settings.PropertyNamingStyle = CSharpNamingStyle.PascalCase;

var generator = new CSharpGenerator(document, settings);
var code = generator.GenerateFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task When_dictionary_key_is_enum_then_csharp_has_enum_key()
var data = schema.ToJson();

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { PropertyNamingStyle = CSharpNamingStyle.PascalCase });
var code = generator.GenerateFile("MyClass");

//// Assert
Expand All @@ -49,7 +49,8 @@ public async Task When_dictionary_property_is_required_then_dictionary_instance_
{
ClassStyle = CSharpClassStyle.Poco,
DictionaryType = "Foo",
DictionaryInstanceType = "Bar"
DictionaryInstanceType = "Bar",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand Down
5 changes: 3 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public async Task When_type_name_hint_has_generics_then_they_are_converted()
var schema = await JsonSchema.FromJsonAsync(json);

var settings = new CSharpGeneratorSettings();
settings.PropertyNamingStyle = CSharpNamingStyle.PascalCase;
var generator = new CSharpGenerator(schema, settings);

var code = generator.GenerateFile("Foo");
Expand Down Expand Up @@ -485,7 +486,7 @@ public async Task When_enum_is_nullable_not_required_it_should_be_nullable_with_
//// Act
var schema = await JsonSchema.FromJsonAsync(json);

var settings = new CSharpGeneratorSettings {EnforceFlagEnums = true};
var settings = new CSharpGeneratorSettings { EnforceFlagEnums = true, PropertyNamingStyle = CSharpNamingStyle.PascalCase };
var generator = new CSharpGenerator(schema, settings);

var code = generator.GenerateFile("Foo");
Expand Down Expand Up @@ -523,7 +524,7 @@ public async Task When_enum_is_nullable_required_it_should_be_nullable_with_conv
//// Act
var schema = await JsonSchema.FromJsonAsync(json);

var settings = new CSharpGeneratorSettings { EnforceFlagEnums = true };
var settings = new CSharpGeneratorSettings { EnforceFlagEnums = true, PropertyNamingStyle = CSharpNamingStyle.PascalCase };
var generator = new CSharpGenerator(schema, settings);

var code = generator.GenerateFile("Foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns", };
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns", PropertyNamingStyle = CSharpNamingStyle.PascalCase };
var generator = new CSharpGenerator(schema, settings);
var output = generator.GenerateFile("MyClass");

Expand Down Expand Up @@ -232,7 +232,10 @@ public async Task When_property_is_timespan_than_csharp_timespan_is_used()
//// Arrange
var schema = JsonSchema.FromType<Person>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

//// Act
var output = generator.GenerateFile("MyClass");
Expand Down Expand Up @@ -357,7 +360,10 @@ public void When_name_contains_dash_then_it_is_converted_to_upper_case()
Type = JsonObjectType.String
};

var generator = new CSharpGenerator(schema);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

// Act
var output = generator.GenerateFile("MyClass");
Expand All @@ -370,14 +376,14 @@ public void When_name_contains_dash_then_it_is_converted_to_upper_case()
}

[Theory]
[InlineData("foo@bar", "Foobar")]
[InlineData("foo$bar", "Foobar")]
[InlineData("foo@bar", "FooBar")]
[InlineData("foo$bar", "FooBar")]
[InlineData("foobars[]", "Foobars")]
[InlineData("foo.bar", "FooBar")]
[InlineData("foo=bar", "FooBar")]
[InlineData("foo+bar", "Fooplusbar")]
[InlineData("foo*bar", "FooStarbar")]
[InlineData("foo:bar", "Foo_bar")]
[InlineData("foo+bar", "FooplusBar")]
[InlineData("foo*bar", "FoostarBar")]
[InlineData("foo:bar", "Foo_Bar")]
public void When_name_contains_unallowed_characters_then_they_are_converted_to_valid_csharp(string jsonPropertyName, string expectedCSharpName)
{
// Arrange
Expand All @@ -387,7 +393,10 @@ public void When_name_contains_unallowed_characters_then_they_are_converted_to_v
Type = JsonObjectType.String
};

var generator = new CSharpGenerator(schema);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

// Act
var output = generator.GenerateFile("MyClass");
Expand Down Expand Up @@ -421,6 +430,7 @@ private static async Task<CSharpGenerator> CreateGeneratorAsync()
var schemaData = schema.ToJson();
var settings = new CSharpGeneratorSettings();
settings.Namespace = "MyNamespace";
settings.PropertyNamingStyle = CSharpNamingStyle.PascalCase;
var generator = new CSharpGenerator(schema, settings);
return generator;
}
Expand Down Expand Up @@ -489,7 +499,8 @@ public async Task When_enum_property_has_default_and_int_serialization_then_corr
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "Foo"
Namespace = "Foo",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand All @@ -510,7 +521,8 @@ public async Task When_enum_property_has_default_and_string_serialization_then_c
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "Foo"
Namespace = "Foo",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand Down Expand Up @@ -552,7 +564,8 @@ public async Task When_enum_type_name_is_missing_then_default_value_is_still_cor
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
Namespace = "Foo"
Namespace = "Foo",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand Down Expand Up @@ -662,7 +675,10 @@ public async Task When_property_is_required_then_CSharp_code_is_correct()
var schemaJson = schema.ToJson();

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

//// Assert
Expand Down Expand Up @@ -961,6 +977,7 @@ public async Task When_property_has_not_supported_characters_then_they_are_remov
var schema = await JsonSchema.FromJsonAsync(schemaJson);

var settings = new CSharpGeneratorSettings();
settings.PropertyNamingStyle = CSharpNamingStyle.PascalCase;
var generator = new CSharpGenerator(schema, settings);

//// Act
Expand Down Expand Up @@ -1654,7 +1671,8 @@ public async Task When_class_is_abstract_constructor_is_protected_for_record()
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record
ClassStyle = CSharpClassStyle.Record,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

//// Act
Expand All @@ -1681,7 +1699,8 @@ public async Task When_record_has_inheritance()
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record
ClassStyle = CSharpClassStyle.Record,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

//// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan
var schema = JsonSchema.FromType<MyContainer>();
var data = schema.ToJson();

var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings());
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

//// Act
var code = generator.GenerateFile();
Expand Down
6 changes: 4 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/InterfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public async Task When_interface_has_properties_then_properties_are_included_in_
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
SchemaType = SchemaType.Swagger2,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("Person");

Expand All @@ -48,7 +49,8 @@ public async Task When_class_implements_interface_then_properties_are_included_i
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
SchemaType = SchemaType.Swagger2,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("Person");

Expand Down
5 changes: 4 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp.Tests/UriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public async Task When_property_is_uri_then_csharp_output_is_also_uri()
//// Arrange
var schema = JsonSchema.FromType<ClassWithUri>();
var json = schema.ToJson();
var generator = new CSharpGenerator(schema);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});

//// Act
var code = generator.GenerateFile("MyClass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public async Task When_property_is_integer_and_no_format_is_available_then_defau
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
SchemaType = SchemaType.Swagger2,
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand Down Expand Up @@ -86,7 +87,8 @@ public async Task When_property_is_string_and_format_is_date_time_then_assign_de
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2,
DateTimeType = "System.DateTime"
DateTimeType = "System.DateTime",
PropertyNamingStyle = CSharpNamingStyle.PascalCase
});
var code = generator.GenerateFile("MyClass");

Expand Down
11 changes: 10 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public CSharpGeneratorSettings()
GenerateJsonMethods = false;
EnforceFlagEnums = false;

PropertyNamingStyle = CSharpNamingStyle.PascalSnakeCase;
ClassNamingStyle = CSharpNamingStyle.PascalCase;

ValueGenerator = new CSharpValueGenerator(this);
PropertyNameGenerator = new CSharpPropertyNameGenerator();
PropertyNameGenerator = new CSharpPropertyNameGenerator(this);
TemplateFactory = new DefaultTemplateFactory(this, new Assembly[]
{
typeof(CSharpGeneratorSettings).GetTypeInfo().Assembly
Expand Down Expand Up @@ -144,5 +147,11 @@ public CSharpGeneratorSettings()

/// <summary>Gets or sets a value indicating whether to generate Nullable Reference Type annotations (default: false).</summary>
public bool GenerateNullableReferenceTypes { get; set; }

/// <summary>Gets or sets a value indicating what type of naming style to use for properties (default: PascalSnakeCase).</summary>
public CSharpNamingStyle PropertyNamingStyle { get; set; }

/// <summary>Gets or sets a value indicating what type of naming style to use for classes (default: PascalCase).</summary>
public CSharpNamingStyle ClassNamingStyle { get; set; }
}
}
32 changes: 32 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpNamingStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//-----------------------------------------------------------------------
// <copyright file="CSharpClassStyle.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

namespace NJsonSchema.CodeGeneration.CSharp
{
/// <summary>The CSharp naming styles.</summary>
public enum CSharpNamingStyle
{
/// <summary>Generate Names with flat case (twowords).</summary>
FlatCase,

/// <summary>Generate Names with upper flat case (TWOWORDS).</summary>
UpperFlatCase,

/// <summary>Generate Names with camel case (twoWords).</summary>
CamelCase,

/// <summary>Generate Names with pascal case (TwoWords).</summary>
PascalCase,

/// <summary>Generate Names with snake case (two_words).</summary>
SnakeCase,

/// <summary>Generate Names with pascal snake case (Two_Words).</summary>
PascalSnakeCase
}
}
Loading