diff --git a/packages/dynamite/dynamite/lib/dynamite.dart b/packages/dynamite/dynamite/lib/dynamite.dart index ae668694..190cddf9 100644 --- a/packages/dynamite/dynamite/lib/dynamite.dart +++ b/packages/dynamite/dynamite/lib/dynamite.dart @@ -4,6 +4,7 @@ import 'dart:convert'; import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart'; +import 'package:collection/collection.dart'; import 'package:dart_style/dart_style.dart'; import 'package:dynamite/src/models/open_api.dart'; import 'package:dynamite/src/models/parameter.dart' as spec_parameter; diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 3b57f3f4..764d2a71 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -356,40 +356,37 @@ class OpenAPIBuilder implements Builder { b.annotations.add(refer('Deprecated').call([refer("''")])); } - final acceptHeader = operation.responses?.values - .map((final response) => response.content?.keys) - .reduce((final a, final b) => [...?a, ...?b]) - ?.toSet() + final acceptHeader = (operation.responses?.values + .map((final response) => response.content?.keys) + .whereNotNull() + .expand((final element) => element) + .toSet() ?? + {}) .join(','); final code = StringBuffer(''' var path = '$path'; final queryParameters = {}; - final headers = {${acceptHeader != null ? "'Accept': '$acceptHeader'," : ''}}; + final headers = {${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}}; Uint8List? body; '''); - final securityRequirements = - (operation.security ?? spec.security) ?? >>[]; - final isOptionalSecurity = - securityRequirements.where((final requirement) => requirement.keys.isEmpty).isNotEmpty; + final security = operation.security ?? spec.security ?? []; + final securityRequirements = security.where((final requirement) => requirement.isNotEmpty); + final isOptionalSecurity = securityRequirements.length != security.length; for (final requirement in securityRequirements) { - if (requirement.keys.isEmpty) { - continue; - } final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!; code.write(''' if (${isRootClient ? '' : 'rootClient.'}authentications.where((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').isNotEmpty) { headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').headers); } '''); - if (!isOptionalSecurity || - securityRequirements.indexOf(requirement) != securityRequirements.length - 1) { + if (securityRequirements.last != requirement) { code.write('else'); } } if (securityRequirements.isNotEmpty && !isOptionalSecurity) { code.write(''' - { + else { throw Exception('Missing authentication for ${securityRequirements.map((final r) => r.keys.single).join(' or ')}'); // coverage:ignore-line } '''); diff --git a/packages/dynamite/dynamite/pubspec.yaml b/packages/dynamite/dynamite/pubspec.yaml index 0a134f6b..36cd1277 100644 --- a/packages/dynamite/dynamite/pubspec.yaml +++ b/packages/dynamite/dynamite/pubspec.yaml @@ -8,6 +8,7 @@ dependencies: build: ^2.4.0 built_value: ^8.5.0 code_builder: ^4.4.0 + collection: ^1.17.2 dart_style: ^2.3.1 intersperse: ^2.0.0 json_annotation: ^4.8.1