From 5ad62714ad25192b64ab7393f42ba650f589de87 Mon Sep 17 00:00:00 2001 From: Patt O'Brien <12474318+pattobrien@users.noreply.github.com> Date: Tue, 12 Dec 2023 06:09:31 -0500 Subject: [PATCH] Fixed error with path-level parameters cause crash. (#148) --- swagger_parser/lib/src/parser/parser.dart | 25 +++++++++++++++++++++-- swagger_parser/pubspec.yaml | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/swagger_parser/lib/src/parser/parser.dart b/swagger_parser/lib/src/parser/parser.dart index de44d757..598d2fdc 100644 --- a/swagger_parser/lib/src/parser/parser.dart +++ b/swagger_parser/lib/src/parser/parser.dart @@ -478,9 +478,21 @@ class OpenApiParser { } (_definitionFileContent[_pathsConst] as Map) .forEach((path, pathValue) { - (pathValue as Map).forEach((key, requestPath) { + final pathValueMap = pathValue as Map; + + // global parameters are defined at the path level (i.e. /users/{id}) + final globalParameters = []; + + if (pathValueMap.containsKey(_parametersConst)) { + final params = _version == OAS.v2 + ? parametersV2(pathValue) + : parametersV3(pathValue); + globalParameters.addAll(params); + } + + pathValue.forEach((key, requestPath) { // `servers` contains List - if (key == _serversConst) { + if (key == _serversConst || key == _parametersConst) { return; } @@ -494,6 +506,15 @@ class OpenApiParser { ? parametersV2(requestPath) : parametersV3(requestPath); + // Add global parameters that have not been overridden by local parameters + // defined at the request level. + parameters.addAll( + globalParameters.where( + (e) => + parameters.every((p) => p.name != e.name && p.type != e.type), + ), + ); + // Build full description final summary = requestPath[_summaryConst]?.toString().trim(); var description = requestPath[_descriptionConst]?.toString().trim(); diff --git a/swagger_parser/pubspec.yaml b/swagger_parser/pubspec.yaml index 3c226221..83027793 100644 --- a/swagger_parser/pubspec.yaml +++ b/swagger_parser/pubspec.yaml @@ -9,6 +9,7 @@ topics: - codegen - api - json + environment: sdk: ^3.0.0