diff --git a/swagger_parser/CHANGELOG.md b/swagger_parser/CHANGELOG.md index 24295919..3fd04105 100644 --- a/swagger_parser/CHANGELOG.md +++ b/swagger_parser/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.12.2 +- Fixes enum duplicate names (#140)[https://github.com/Carapacik/swagger_parser/issues/140] + ## 1.12.1 - Fixed error with `ref` in a case other than PascalCase ([#139](https://github.com/Carapacik/swagger_parser/issues/139)) diff --git a/swagger_parser/lib/src/generator/models/universal_enum_class.dart b/swagger_parser/lib/src/generator/models/universal_enum_class.dart index 762b7767..f96715e8 100644 --- a/swagger_parser/lib/src/generator/models/universal_enum_class.dart +++ b/swagger_parser/lib/src/generator/models/universal_enum_class.dart @@ -7,9 +7,13 @@ final class UniversalEnumClass extends UniversalDataClass { required super.name, required this.type, required this.items, + final String? originalName, this.defaultValue, super.description, - }); + }) : originalName = originalName ?? name; + + /// Enum original name (without suffix number for duplicates) + final String originalName; /// Enum type final String type; diff --git a/swagger_parser/lib/src/parser/parser.dart b/swagger_parser/lib/src/parser/parser.dart index 04259ab7..ed8fd75e 100644 --- a/swagger_parser/lib/src/parser/parser.dart +++ b/swagger_parser/lib/src/parser/parser.dart @@ -72,7 +72,7 @@ class OpenApiParser { late final Map _definitionFileContent; late final OAS _version; final List _objectClasses = []; - final List _enumClasses = []; + final Set _enumClasses = {}; static const _additionalPropertiesConst = 'additionalProperties'; static const _allOfConst = 'allOf'; @@ -115,6 +115,44 @@ class OpenApiParser { static const _titleConst = 'title'; static const _typeConst = 'type'; static const _versionConst = 'version'; + final usedNamesCount = {}; + + UniversalEnumClass getUniqueEnumClass({ + required final String name, + required final Set items, + required final String type, + required final String? defaultValue, + required final String? description, + }) { + // Search _enumClasses for an enum with the same name and values + final enumClass = _enumClasses.firstWhereOrNull( + (e) => + e.originalName == name && + const DeepCollectionEquality().equals(e.items, items), + ); + + if (enumClass != null) { + return enumClass; + } + + String uniqueName; + if (usedNamesCount.containsKey(name)) { + usedNamesCount[name] = usedNamesCount[name]! + 1; + uniqueName = '$name${usedNamesCount[name]}'; + } else { + usedNamesCount[name] = 1; + uniqueName = name; + } + + return UniversalEnumClass( + originalName: name, + name: uniqueName.toPascal, + type: type, + items: items, + defaultValue: defaultValue, + description: description, + ); + } /// Parse OpenApi parameters into [OpenApiInfo] OpenApiInfo parseOpenApiInfo() { @@ -588,10 +626,10 @@ class OpenApiParser { } dataClasses.add( - UniversalEnumClass( + getUniqueEnumClass( name: key, - type: type, items: items, + type: type, defaultValue: value[_defaultConst]?.toString(), description: value[_descriptionConst]?.toString(), ), @@ -784,19 +822,19 @@ class OpenApiParser { (map[_enumConst] as List).map((e) => '$e'), ); - _enumClasses.add( - UniversalEnumClass( - name: newName.toPascal, - type: map[_typeConst].toString(), - items: items, - defaultValue: protectDefaultValue(map[_defaultConst], isEnum: true), - description: description, - ), + final enumClass = getUniqueEnumClass( + name: newName, + items: items, + type: map[_typeConst].toString(), + defaultValue: protectDefaultValue(map[_defaultConst], isEnum: true), + description: description, ); + _enumClasses.add(enumClass); + return ( type: UniversalType( - type: newName.toPascal, + type: enumClass.name, name: variableName.toCamel, description: description, format: map[_formatConst]?.toString(), @@ -805,7 +843,7 @@ class OpenApiParser { isRequired: isRequired, enumType: map[_typeConst]?.toString(), ), - import: newName, + import: enumClass.name, ); } // Object or additionalProperties diff --git a/swagger_parser/pubspec.yaml b/swagger_parser/pubspec.yaml index 7ee0c617..e88e2f2c 100644 --- a/swagger_parser/pubspec.yaml +++ b/swagger_parser/pubspec.yaml @@ -1,6 +1,6 @@ name: swagger_parser description: Package that generates REST clients and data classes from OpenApi definition file -version: 1.12.1 +version: 1.12.2 repository: https://github.com/Carapacik/swagger_parser/tree/main/swagger_parser homepage: https://omega-r.com topics: