Skip to content

Commit

Permalink
Add support for flag enums with more than 32 values (#1530)
Browse files Browse the repository at this point in the history
* Add support for flag enums with more than 32 values

* Update EnumTests.cs

Co-authored-by: Christoph Genneper <c.genneper@chefsculinar.de>
Co-authored-by: Rico Suter <mail@rsuter.com>
  • Loading branch information
3 people authored Sep 16, 2022
1 parent d9d715a commit a43bed2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
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

0 comments on commit a43bed2

Please sign in to comment.