Repository: epam-cross-platform-lab/swagger-dart-code-generator (dart)

Issue:
[BUG] Regression in 2.10 with enums
**Describe the bug**
Generating code for enums which have illegal variable names can make collisions

**To Reproduce**
json schema:
```
"myType": {
  "type": "object",
  "required": [
    "myProperty",
  ],
  "properties": {
    "myProperty": {
      "type": "string",
      "enum": [
        "R",
        "-R",
      ]
    },
  }
}
```

Generated code:
```
enum myType {
  @JsonValue(null)
  swaggerGeneratedUnknown(null),

  @JsonValue('R')
  r('R'),
  @JsonValue('-R')
  r('-R'),

  final String? value;

  const myType(this.value);
}
```

A similar collision happens with an integer enumeration that includes 1 and -1.
```
 "enum": [
              -1,
               0,
              -1
```
which generates members like this:
```
  @JsonValue(-1)
  value_1(-1);
  @JsonValue(0)
  value_0(0),
  @JsonValue(1)
  value_1(1),
```

**Expected behavior**
2.9.0 worked by generating code with $ for illegal characters like:
```
  @JsonValue('R')
  r,
  @JsonValue('-R')
  r$,
```

and 

```
  @JsonValue(-1)
  value_1$
  @JsonValue(0)
  value_0,
  @JsonValue(1)
  value_1,
```

**Additional context**
Add any other context about the problem here.

Pull request description:
Fixed #542

Patch:
diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml
new file mode 100644
index 00000000..92b23a51
--- /dev/null
+++ b/example/analysis_options.yaml
@@ -0,0 +1,15 @@
+# https://www.dartlang.org/guides/language/analysis-options
+
+include: package:lints/recommended.yaml
+
+analyzer:
+  strong-mode:
+    implicit-casts: false
+  exclude:
+    - example/**/*.dart
+    - lib/**/*.g2.dart
+linter:
+  rules:
+    - type_annotate_public_apis
+    - void_checks
+    - unnecessary_new
\ No newline at end of file
diff --git a/lib/src/code_generators/enum_model.dart b/lib/src/code_generators/enum_model.dart
index d6737ba6..9061c8d9 100644
--- a/lib/src/code_generators/enum_model.dart
+++ b/lib/src/code_generators/enum_model.dart
@@ -23,13 +23,23 @@ class EnumModel {
   String _getEnumContent() {
     final resultStrings = <String>[];
 
+    final allFieldNames = <String>[];
+
     for (int i = 0; i < values.length; i++) {
       final value = values[i];
 
       var validatedValue = enumNames.isNotEmpty ? enumNames[i] : value;
 
-      validatedValue =
-          getValidatedEnumFieldName(validatedValue, value, isInteger);
+      validatedValue = getValidatedEnumFieldName(
+        validatedValue,
+        value,
+        isInteger,
+        allFieldNames,
+      );
+
+      allFieldNames
+          .add(validatedValue.substring(0, validatedValue.indexOf('(')));
+
 
       if (isInteger) {
         resultStrings.add(
@@ -53,9 +63,13 @@ const $name(this.value);
 }''';
   }
 
-
   static String getValidatedEnumFieldName(
-      String fieldName, String fieldValue, bool isInteger) {
+    String fieldName,
+    String fieldValue,
+    bool isInteger,
+    List<String> allFieldNames,
+  ) {
+
     if (fieldName.isEmpty) {
       fieldName = 'null';
     }
@@ -71,15 +85,21 @@ const $name(this.value);
       result = defaultEnumFieldName + result;
     }
 
-    if (exceptionWords.contains(result.toLowerCase())) {
-      return '\$${result.lower}(${isInteger ? fieldValue : '\'$fieldValue\''})';
+    result = result.lower;
+
+    while (allFieldNames.contains(result.lower)) {
+      result = '\$$result';
+    }
+
+    if (exceptionWords.contains(result)) {
+      return '\$$result(${isInteger ? fieldValue : '\'$fieldValue\''})';
     }
 
     if (result.isEmpty) {
       return 'undefined(${isInteger ? fieldValue : '\'$fieldValue\''})';
     }
 
-    return '${result.lower}(${isInteger ? fieldValue : '\'$fieldValue\''})';
+    return '$result(${isInteger ? fieldValue : '\'$fieldValue\''})';
 
   }
 
diff --git a/lib/src/code_generators/swagger_models_generator.dart b/lib/src/code_generators/swagger_models_generator.dart
index fef01452..c8e5cd58 100644
--- a/lib/src/code_generators/swagger_models_generator.dart
+++ b/lib/src/code_generators/swagger_models_generator.dart
@@ -474,9 +474,12 @@ abstract class SwaggerModelsGenerator extends SwaggerGeneratorBase {
           valueType = 'Object';
           returnType = validatedTypeName;
           final defaultValueCamelCase = EnumModel.getValidatedEnumFieldName(
-              defaultValue?.toString() ?? '',
-              defaultValue?.toString() ?? '',
-              false);
+            defaultValue?.toString() ?? '',
+            defaultValue?.toString() ?? '',
+            false,
+            [],
+          );
+
 
           defaultValueString =
               '$validatedTypeName.${defaultValueCamelCase.substring(0, defaultValueCamelCase.indexOf('('))}';


Test patch:
diff --git a/test/generator_tests/enums_generator_test.dart b/test/generator_tests/enums_generator_test.dart
index f4f640b0..0101caa3 100644
--- a/test/generator_tests/enums_generator_test.dart
+++ b/test/generator_tests/enums_generator_test.dart
@@ -20,6 +20,7 @@ void main() {
         name,
         name,
         false,
+        [],
       );
 
       expect(result, contains(output));
@@ -32,6 +33,7 @@ void main() {
         name,
         name,
         false,
+        [],
       );
 
       expect(result, contains(output));