Skip to content

Commit

Permalink
Patch/improved enum handling (RicoSuter#1170)
Browse files Browse the repository at this point in the history
* patch/improved-enum-handling

* Improved enum handling resolves RicoSuter#1357

* Added tests to cover operators and plus or minus characters

* Update DefaultEnumNameGenerator.cs

Co-authored-by: Rico Suter <mail@rsuter.com>
  • Loading branch information
truck0321 and RicoSuter authored May 27, 2020
1 parent 36667dd commit 36eba80
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,91 @@ public async Task When_enum_property_is_not_required_in_Swagger2_then_it_is_null
Assert.Contains("public MyClassStatus? Status { get; set; }", code);
}

[Fact]
public async Task When_enum_contains_operator_convert_to_string_equivalent()
{
////Arrange
var json = @"{
""type"": ""object"",
""properties"": {
""foo"": {
""$ref"": ""#/definitions/OperatorTestEnum""
}
},
""definitions"": {
""OperatorTestEnum"": {
""type"": ""string"",
""description"": ""The operator between the field and operand."",
""enum"": [
""="",
""!="",
"">"",
""<"",
"">="",
""<="",
""in"",
""not in"",
null,
""~="",
""is"",
""is not""
]
}
}
}";

/// Act
var schema = await JsonSchema.FromJsonAsync(json);

var settings = new CSharpGeneratorSettings();
var generator = new CSharpGenerator(schema, settings);

var code = generator.GenerateFile("Foo");

/// Assert
Assert.DoesNotContain("__", code);
Assert.Contains("Eq = 0", code);

}

[Fact]
public async Task When_enum_starts_with_plus_or_minus_convert_to_string_equivalent()
{
////Arrange
var json = @"{
""type"": ""object"",
""properties"": {
""foo"": {
""$ref"": ""#/definitions/PlusMinusTestEnum""
}
},
""definitions"": {
""PlusMinusTestEnum"": {
""type"": ""string"",
""description"": ""Add or subtract from property"",
""enum"": [
""-Foo"",
""+Foo""
]
}
}
}";

/// Act
var schema = await JsonSchema.FromJsonAsync(json);

var settings = new CSharpGeneratorSettings();
var generator = new CSharpGenerator(schema, settings);

var code = generator.GenerateFile("Foo");

/// Assert
Assert.DoesNotContain("__", code);
Assert.Contains("MinusFoo = 0", code);
Assert.Contains("PlusFoo = 1", code);

}

[Fact]
public async Task When_array_item_enum_is_not_referenced_then_type_name_hint_is_property_name()
{
Expand Down
35 changes: 35 additions & 0 deletions src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ public string Generate(int index, string name, object value, JsonSchema schema)
return "Empty";
}

switch (name)
{
case ("="):
name = "Eq";
break;
case ("!="):
name = "Ne";
break;
case (">"):
name = "Gt";
break;
case ("<"):
name = "Lt";
break;
case (">="):
name = "Ge";
break;
case ("<="):
name = "Le";
break;
case ("~="):
name = "Approx";
break;
}

if (name.StartsWith("-"))
{
name = "Minus" + name.Substring(1);
}

if (name.StartsWith("+"))
{
name = "Plus" + name.Substring(1);
}

if (name.StartsWith("_-"))
{
name = "__" + name.Substring(2);
Expand Down

0 comments on commit 36eba80

Please sign in to comment.