From 6c9a2bb5729b88d986130ca5ed17039aeaa26fe0 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 5 Apr 2023 07:03:36 +0200 Subject: [PATCH] dynamite: Fix optional security --- .../dynamite/lib/src/openapi_builder.dart | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 813ab987..52a6f433 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -675,27 +675,26 @@ class OpenAPIBuilder implements Builder { (operation.security ?? spec.security) ?? >>[]; final isOptionalSecurity = securityRequirements.where((final requirement) => requirement.keys.isEmpty).isNotEmpty; - if (isOptionalSecurity) { + for (final requirement in securityRequirements) { + if (requirement.keys.isEmpty) { + continue; + } 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}')) { 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 ')}'); } '''); - } } for (final parameter in parameters) {