Skip to content

Commit

Permalink
Enum unique name (#142)
Browse files Browse the repository at this point in the history
Co-authored-by: Elena Ferro <[email protected]>
  • Loading branch information
elenaferr0 and Elena Ferro authored Nov 23, 2023
1 parent 24a22b3 commit 3265534
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
3 changes: 3 additions & 0 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 51 additions & 13 deletions swagger_parser/lib/src/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class OpenApiParser {
late final Map<String, dynamic> _definitionFileContent;
late final OAS _version;
final List<UniversalComponentClass> _objectClasses = [];
final List<UniversalEnumClass> _enumClasses = [];
final Set<UniversalEnumClass> _enumClasses = {};

static const _additionalPropertiesConst = 'additionalProperties';
static const _allOfConst = 'allOf';
Expand Down Expand Up @@ -115,6 +115,44 @@ class OpenApiParser {
static const _titleConst = 'title';
static const _typeConst = 'type';
static const _versionConst = 'version';
final usedNamesCount = <String, int>{};

UniversalEnumClass getUniqueEnumClass({
required final String name,
required final Set<UniversalEnumItem> 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() {
Expand Down Expand Up @@ -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(),
),
Expand Down Expand Up @@ -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(),
Expand All @@ -805,7 +843,7 @@ class OpenApiParser {
isRequired: isRequired,
enumType: map[_typeConst]?.toString(),
),
import: newName,
import: enumClass.name,
);
}
// Object or additionalProperties
Expand Down
2 changes: 1 addition & 1 deletion swagger_parser/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 3265534

Please sign in to comment.