Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicate fields on DTO and API Client #200

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ String dartDartMappableDtoTemplate(
final className = dataClass.name.toPascal;
return '''
${generatedFileComment(markFileAsGenerated: markFileAsGenerated)}
${ioImport(dataClass)}
${dartImportDtoTemplate(JsonSerializer.dartMappable)}
${dartImports(imports: dataClass.imports)}
part '${dataClass.name.toSnake}.mapper.dart';
Expand Down
83 changes: 82 additions & 1 deletion swagger_parser/lib/src/parser/parser/open_api_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,36 @@ class OpenApiParser {
}
}

final protectedParameters = <UniversalRequestType>[];
final groupedParameters = groupBy(parameters, (e) => e.type.name);
for (final entry in groupedParameters.values) {
if (entry.length == 1) {
protectedParameters.add(entry.first);
} else {
var counter = 0;
for (final parameter in entry) {
final protectedParameter = UniversalRequestType(
name: parameter.name,
type:
// ignore: prefer_single_quotes
parameter.type.copyWith(name: "${parameter.name}$counter"),
parameterType: parameter.parameterType,
description: parameter.description,
);
protectedParameters.add(protectedParameter);
counter++;
}
}
}

final request = UniversalRequest(
name: requestName,
description: description,
requestType: HttpRequestType.fromString(key)!,
route: path,
contentType: resultContentType,
returnType: returnType,
parameters: parameters,
parameters: protectedParameters,
isDeprecated:
requestPath[_deprecatedConst].toString().toBool() ?? false,
);
Expand Down Expand Up @@ -819,6 +841,65 @@ class OpenApiParser {
}
allOfClass.parameters.addAll(allOfClass.allOf!.properties);
}

// Rename parameters/ enum members with the same name
for (final (index, dataclass) in dataClasses.indexed) {
if (dataclass is UniversalComponentClass) {
final protectedParameters = <UniversalType>[];
final groupedParameters = groupBy(dataclass.parameters, (e) => e.name);

for (final MapEntry(value: groupParameters)
in groupedParameters.entries) {
if (groupParameters.length == 1) {
protectedParameters.add(groupParameters[0]);
} else {
var counter = 0;
for (final parameter in groupParameters) {
protectedParameters
.add(parameter.copyWith(name: '${parameter.name}$counter'));
counter++;
}
}
}
dataClasses[index] = UniversalComponentClass(
name: dataclass.name,
imports: dataclass.imports,
allOf: dataclass.allOf,
description: dataclass.description,
typeDef: dataclass.typeDef,
parameters: protectedParameters,
);
} else if (dataclass is UniversalEnumClass) {
final protectedItems = <UniversalEnumItem>{};
final groupedItems = groupBy(dataclass.items, (e) => e.name);
for (final MapEntry(value: groupItems) in groupedItems.entries) {
if (groupItems.length == 1) {
protectedItems.add(groupItems.first);
} else {
var counter = 0;
for (final item in groupItems) {
protectedItems.add(
UniversalEnumItem(
jsonKey: item.jsonKey,
name: '${item.name}$counter',
description: item.description,
),
);
counter++;
}
}
}
dataClasses[index] = UniversalEnumClass(
originalName: dataclass.originalName,
name: dataclass.name,
type: dataclass.type,
items: protectedItems,
defaultValue: dataclass.defaultValue,
description: dataclass.description,
);
}
}

return dataClasses;
}

Expand Down
4 changes: 4 additions & 0 deletions swagger_parser/lib/src/parser/utils/dart_keywords.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const dartKeywords = {
'async',
'await',
'base',
'bool',
'break',
'case',
'catch',
Expand All @@ -17,6 +18,7 @@ const dartKeywords = {
'default',
'deferred',
'do',
'double',
'dynamic',
'else',
'enum',
Expand All @@ -37,13 +39,15 @@ const dartKeywords = {
'implements',
'import',
'in',
'int',
'interface',
'is',
'late',
'library',
'mixin',
'new',
'null',
'num',
'on',
'operator',
'part',
Expand Down
2 changes: 1 addition & 1 deletion swagger_parser/lib/src/utils/base_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ const _generatedCodeComment = '''
''';

const _ignoreLintsComment = '''
// ignore_for_file: type=lint, unused_import
// ignore_for_file: type=lint, unused_import, invalid_annotation_target
''';
Loading