From 36eba8013a202757373d3df9b4f3d3341b00fffc Mon Sep 17 00:00:00 2001 From: Darin Truckenmiller Date: Wed, 27 May 2020 01:27:41 -0700 Subject: [PATCH] Patch/improved enum handling (#1170) * patch/improved-enum-handling * Improved enum handling resolves RicoSuter/NJsonSchema#1357 * Added tests to cover operators and plus or minus characters * Update DefaultEnumNameGenerator.cs Co-authored-by: Rico Suter --- .../EnumTests.cs | 85 +++++++++++++++++++ .../DefaultEnumNameGenerator.cs | 35 ++++++++ 2 files changed, 120 insertions(+) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs index 10842fac1..9d55ca7d5 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/EnumTests.cs @@ -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() { diff --git a/src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs b/src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs index 3486cfae6..d5266e34d 100644 --- a/src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs +++ b/src/NJsonSchema.CodeGeneration/DefaultEnumNameGenerator.cs @@ -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);