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

Add support for flag enums with more than 32 values #1530

Merged
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
69 changes: 57 additions & 12 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated()
{
//// Arrange
var json =
@"{
@"{
""type"": ""object"",
""properties"": {
""category"" : {
Expand All @@ -41,7 +41,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated_with_flags()
{
//// Arrange
var json =
@"{
@"{
""type"": ""object"",
""properties"": {
""category"" : {
Expand All @@ -57,7 +57,7 @@ public async Task When_enum_has_no_type_then_enum_is_generated_with_flags()
}";

var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { EnforceFlagEnums = true });
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings {EnforceFlagEnums = true});

//// Act
var code = generator.GenerateFile("MyClass");
Expand Down Expand Up @@ -139,7 +139,8 @@ public async Task When_enum_list_uses_string_enums_then_ItemConverterType_is_set
//// Arrange
var schema = JsonSchema.FromType<MyStringEnumListTest>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var generator =
new CSharpGenerator(schema, new CSharpGeneratorSettings {ClassStyle = CSharpClassStyle.Poco});

//// Act
var code = generator.GenerateFile();
Expand All @@ -154,16 +155,22 @@ public async Task When_enum_is_nullable_then_StringEnumConverter_is_set()
//// Arrange
var schema = JsonSchema.FromType<MyStringEnumListTest>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });
var generator =
new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });

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

//// Assert
Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]", code);
Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]",
code);
}

public enum SomeEnum { Thing1, Thing2 }
public enum SomeEnum
{
Thing1,
Thing2
}

public class SomeClass
{
Expand All @@ -179,7 +186,8 @@ public async Task When_class_has_enum_array_property_then_enum_name_is_preserved
var json = schema.ToJson();

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

//// Assert
Expand Down Expand Up @@ -232,7 +240,7 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null
{
//// Arrange
var json =
@"{
@"{
""type"": ""object"",
""required"": [
""name"",
Expand Down Expand Up @@ -523,7 +531,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};
var generator = new CSharpGenerator(schema, settings);

var code = generator.GenerateFile("Foo");
Expand All @@ -539,7 +547,7 @@ public async Task When_enum_is_nullable_and_has_default_then_question_mark_is_om
{
//// Arrange
var json =
@"{
@"{
""type"": ""object"",
""properties"": {
""category"" : {
Expand All @@ -565,7 +573,44 @@ public async Task When_enum_is_nullable_and_has_default_then_question_mark_is_om
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public MyClassCategory? Category { get; set; } = MyNamespace.MyClassCategory.Commercial;", code);
Assert.Contains("public MyClassCategory? Category { get; set; } = MyNamespace.MyClassCategory.Commercial;",
code);
}

[Fact]
public async Task When_enum_has_a_format_then_enum_is_generated_with_correct_basetype()
{
// Arrange
var json = @"
{
""properties"": {
""foo"": {
""$ref"": ""#/definitions/ManyValuesTestEnum""
}
},
""definitions"": {
""ManyValuesTestEnum"": {
""type"": ""integer"",
""format"": ""int64"",
""x-enumNames"": [
""None"",
""FirstBit""
],
""enum"": [
0,
1
]
}
}
}";
var schema = await JsonSchema.FromJsonAsync(json);
var generator = new CSharpGenerator(schema);

//// Act
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("public enum ManyValuesTestEnum : long", code);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public EnumTemplateModel(string typeName, JsonSchema schema, CSharpGeneratorSett
/// <summary>Gets or sets if we output as Bit Flags.</summary>
public bool IsEnumAsBitFlags => _settings.EnforceFlagEnums || _schema.IsFlagEnumerable;

/// <summary>Gets a value indicating whether the enum needs an other base type to representing an extended value range.</summary>
public bool HasExtendedValueRange => _schema.Format == JsonFormatStrings.Long;

/// <summary>Gets the enum values.</summary>
public IEnumerable<EnumerationItemModel> Enums
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{%- if IsEnumAsBitFlags -%}
[System.Flags]
{%- endif -%}
{{ TypeAccessModifier }} enum {{ Name }}
{{ TypeAccessModifier }} enum {{ Name }}{%- if HasExtendedValueRange %} : long{% endif %}
{
{%- for enum in Enums %}
{%- if IsStringEnum -%}
Expand Down