Browse Source

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

dynamite: Fix optional security
pull/234/head
Kate 2 years ago committed by GitHub
parent
commit
c3ac8e1bbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      packages/dynamite/lib/src/openapi_builder.dart

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

@ -675,27 +675,26 @@ class OpenAPIBuilder implements Builder {
(operation.security ?? spec.security) ?? <Map<String, List<String>>>[]; (operation.security ?? spec.security) ?? <Map<String, List<String>>>[];
final isOptionalSecurity = final isOptionalSecurity =
securityRequirements.where((final requirement) => requirement.keys.isEmpty).isNotEmpty; securityRequirements.where((final requirement) => requirement.keys.isEmpty).isNotEmpty;
if (isOptionalSecurity) { for (final requirement in securityRequirements) {
if (requirement.keys.isEmpty) {
continue;
}
code.write(''' code.write('''
if (${isRootClient ? '' : 'rootClient.'}authentications.isNotEmpty) {
headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.first.headers);
}
''');
} else {
for (final requirement in securityRequirements) {
code.write('''
if (${isRootClient ? '' : 'rootClient.'}authentications.map((final a) => a.id).contains('${requirement.keys.single}')) { if (${isRootClient ? '' : 'rootClient.'}authentications.map((final a) => a.id).contains('${requirement.keys.single}')) {
headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.id == '${requirement.keys.single}').headers); headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.id == '${requirement.keys.single}').headers);
} else }
'''); ''');
if (!isOptionalSecurity ||
securityRequirements.indexOf(requirement) != securityRequirements.length - 1) {
code.write('else');
} }
if (securityRequirements.isNotEmpty) { }
code.write(''' if (securityRequirements.isNotEmpty && !isOptionalSecurity) {
code.write('''
{ {
throw Exception('Missing authentication for ${securityRequirements.map((final r) => r.keys.single).join(' or ')}'); throw Exception('Missing authentication for ${securityRequirements.map((final r) => r.keys.single).join(' or ')}');
} }
'''); ''');
}
} }
for (final parameter in parameters) { for (final parameter in parameters) {

Loading…
Cancel
Save