From ef55044c27a772f77eb6343b867facfdd59dc944 Mon Sep 17 00:00:00 2001 From: Luc Genetier <69138830+LucGenetier@users.noreply.github.com> Date: Wed, 27 Nov 2024 17:32:18 +0100 Subject: [PATCH] Fix TryAddOptionSet (#2754) Was throwing a NullRefException for some connectors having option sets --- .../OpenApiExtensions.cs | 5 ++++ .../SwaggerFileIdentification.cs | 27 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.PowerFx.Connectors/OpenApiExtensions.cs b/src/libraries/Microsoft.PowerFx.Connectors/OpenApiExtensions.cs index 053f573b32..23c0435365 100644 --- a/src/libraries/Microsoft.PowerFx.Connectors/OpenApiExtensions.cs +++ b/src/libraries/Microsoft.PowerFx.Connectors/OpenApiExtensions.cs @@ -734,6 +734,11 @@ internal static OptionSet TryAddOptionSet(this SymbolTable symbolTable, OptionSe throw new ArgumentNullException("optionSet"); } + if (symbolTable == null) + { + return optionSet; + } + string name = optionSet.EntityName; // No existing symbols with that name diff --git a/src/tests/Microsoft.PowerFx.TexlFunctionExporter.Shared/SwaggerFileIdentification.cs b/src/tests/Microsoft.PowerFx.TexlFunctionExporter.Shared/SwaggerFileIdentification.cs index f8a4f91028..1370fa81ba 100644 --- a/src/tests/Microsoft.PowerFx.TexlFunctionExporter.Shared/SwaggerFileIdentification.cs +++ b/src/tests/Microsoft.PowerFx.TexlFunctionExporter.Shared/SwaggerFileIdentification.cs @@ -4,8 +4,10 @@ using System; using System.Collections.Generic; using System.Data; +using System.Globalization; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Readers; using Microsoft.OpenApi.Readers.Exceptions; @@ -110,7 +112,21 @@ public static Dictionary LocateSwaggerFiles(string[] folders, st } else { - throw new InvalidDataException($"Two documents with same number of operations, can't determine which one to select {new KeyValuePair errors)>>(swagger.Key, docs).GetString()}"); + int firstVersion = GetVersion(first.location); + int secondVersion = GetVersion(second.location); + + if (firstVersion > secondVersion) + { + list2.Add(swagger.Key, first); + } + else if (firstVersion < secondVersion) + { + list2.Add(swagger.Key, second); + } + else + { + throw new InvalidDataException($"Two documents with same number of operations, can't determine which one to select {new KeyValuePair errors)>>(swagger.Key, docs).GetString()}"); + } } } } @@ -128,6 +144,15 @@ public static Dictionary LocateSwaggerFiles(string[] folders, st return list2; } + // searches '[v or V]' pattern + private static readonly Regex _versionRegex = new Regex(@"\b[vV](?[0-9]+)\\", RegexOptions.Compiled); + + private static int GetVersion(string str) + { + Match m = _versionRegex.Match(str); + return m.Success && int.TryParse(m.Groups["v"].Value, CultureInfo.InvariantCulture.NumberFormat, out int ver) ? ver : 0; + } + private static void ParseSwagger(string folder, string swaggerFile, Dictionary errors)>> list) { (OpenApiDocument doc, List errors) = ReadSwagger(swaggerFile);