Browse Source

Merge pull request #451 from provokateurin/fix/optional-security

dynamite: Fix optional security
pull/457/head
Kate 1 year ago committed by GitHub
parent
commit
2b777dbb86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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: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;

27
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 = <String, dynamic>{};
final headers = <String, String>{${acceptHeader != null ? "'Accept': '$acceptHeader'," : ''}};
final headers = <String, String>{${acceptHeader.isNotEmpty ? "'Accept': '$acceptHeader'," : ''}};
Uint8List? body;
''');
final securityRequirements =
(operation.security ?? spec.security) ?? <Map<String, List<String>>>[];
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
}
''');

1
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

Loading…
Cancel
Save