Browse Source

dynamite: Fix optional security

pull/451/head
jld3103 1 year ago
parent
commit
2a05a1b7f6
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 1
      packages/dynamite/dynamite/lib/dynamite.dart
  2. 27
      packages/dynamite/dynamite/lib/src/openapi_builder.dart
  3. 1
      packages/dynamite/dynamite/pubspec.yaml

1
packages/dynamite/dynamite/lib/dynamite.dart

@ -4,6 +4,7 @@ import 'dart:convert';
import 'package:build/build.dart'; import 'package:build/build.dart';
import 'package:code_builder/code_builder.dart'; import 'package:code_builder/code_builder.dart';
import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'package:dynamite/src/models/open_api.dart'; import 'package:dynamite/src/models/open_api.dart';
import 'package:dynamite/src/models/parameter.dart' as spec_parameter; import 'package:dynamite/src/models/parameter.dart' as spec_parameter;

27
packages/dynamite/dynamite/lib/src/openapi_builder.dart

@ -356,40 +356,37 @@ class OpenAPIBuilder implements Builder {
b.annotations.add(refer('Deprecated').call([refer("''")])); b.annotations.add(refer('Deprecated').call([refer("''")]));
} }
final acceptHeader = operation.responses?.values final acceptHeader = (operation.responses?.values
.map((final response) => response.content?.keys) .map((final response) => response.content?.keys)
.reduce((final a, final b) => [...?a, ...?b]) .whereNotNull()
?.toSet() .expand((final element) => element)
.toSet() ??
{})
.join(','); .join(',');
final code = StringBuffer(''' final code = StringBuffer('''
var path = '$path'; var path = '$path';
final queryParameters = <String, dynamic>{}; final queryParameters = <String, dynamic>{};
final headers = <String, String>{${acceptHeader != null ? "'Accept': '$acceptHeader'," : ''}}; final headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}};
Uint8List? body; Uint8List? body;
'''); ''');
final securityRequirements = final security = operation.security ?? spec.security ?? [];
(operation.security ?? spec.security) ?? <Map<String, List<String>>>[]; final securityRequirements = security.where((final requirement) => requirement.isNotEmpty);
final isOptionalSecurity = final isOptionalSecurity = securityRequirements.length != security.length;
securityRequirements.where((final requirement) => requirement.keys.isEmpty).isNotEmpty;
for (final requirement in securityRequirements) { for (final requirement in securityRequirements) {
if (requirement.keys.isEmpty) {
continue;
}
final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!; final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!;
code.write(''' code.write('''
if (${isRootClient ? '' : 'rootClient.'}authentications.where((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').isNotEmpty) { 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); headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').headers);
} }
'''); ''');
if (!isOptionalSecurity || if (securityRequirements.last != requirement) {
securityRequirements.indexOf(requirement) != securityRequirements.length - 1) {
code.write('else'); code.write('else');
} }
} }
if (securityRequirements.isNotEmpty && !isOptionalSecurity) { if (securityRequirements.isNotEmpty && !isOptionalSecurity) {
code.write(''' code.write('''
{ else {
throw Exception('Missing authentication for ${securityRequirements.map((final r) => r.keys.single).join(' or ')}'); // coverage:ignore-line throw Exception('Missing authentication for ${securityRequirements.map((final r) => r.keys.single).join(' or ')}'); // coverage:ignore-line
} }
'''); ''');

1
packages/dynamite/dynamite/pubspec.yaml

@ -8,6 +8,7 @@ dependencies:
build: ^2.4.0 build: ^2.4.0
built_value: ^8.5.0 built_value: ^8.5.0
code_builder: ^4.4.0 code_builder: ^4.4.0
collection: ^1.17.2
dart_style: ^2.3.1 dart_style: ^2.3.1
intersperse: ^2.0.0 intersperse: ^2.0.0
json_annotation: ^4.8.1 json_annotation: ^4.8.1

Loading…
Cancel
Save