From 1b320ae56b004dabeafdbd91d76bfede8ec7cc57 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 6 Oct 2023 21:55:01 +1100 Subject: [PATCH 1/2] dont return null on exception in JsonPointerExtensions.Find --- .../ParseNodes/JsonPointerExtensions.cs | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs index 412d6901b..655555f04 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs @@ -21,33 +21,26 @@ public static YamlNode Find(this JsonPointer currentPointer, YamlNode baseYamlNo return baseYamlNode; } - try + var pointer = baseYamlNode; + foreach (var token in currentPointer.Tokens) { - var pointer = baseYamlNode; - foreach (var token in currentPointer.Tokens) + if (pointer is YamlSequenceNode sequence) { - if (pointer is YamlSequenceNode sequence) - { - pointer = sequence.Children[Convert.ToInt32(token)]; - } - else + pointer = sequence.Children[Convert.ToInt32(token)]; + } + else + { + if (pointer is YamlMappingNode map) { - if (pointer is YamlMappingNode map) + if (!map.Children.TryGetValue(new YamlScalarNode(token), out pointer)) { - if (!map.Children.TryGetValue(new YamlScalarNode(token), out pointer)) - { - return null; - } + return null; } } } - - return pointer; - } - catch (Exception) - { - return null; } + + return pointer; } } } From 06059ab894ccb892802195002bd6b37e81437c21 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 10 Oct 2023 09:11:22 -0400 Subject: [PATCH 2/2] - refactors node lookup to make it more resilient to looking up unexistent nodes --- .../ParseNodes/JsonPointerExtensions.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs index 655555f04..f0ca6a667 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System; +using System.Linq; using SharpYaml.Serialization; namespace Microsoft.OpenApi.Readers.ParseNodes @@ -24,19 +25,13 @@ public static YamlNode Find(this JsonPointer currentPointer, YamlNode baseYamlNo var pointer = baseYamlNode; foreach (var token in currentPointer.Tokens) { - if (pointer is YamlSequenceNode sequence) + if (pointer is YamlSequenceNode sequence && sequence.Children is not null && int.TryParse(token, out var index) && index < sequence.Children.Count) { - pointer = sequence.Children[Convert.ToInt32(token)]; + pointer = sequence.Children[index]; } - else + else if (pointer is YamlMappingNode map && map.Children is not null && !map.Children.TryGetValue(new YamlScalarNode(token), out pointer)) { - if (pointer is YamlMappingNode map) - { - if (!map.Children.TryGetValue(new YamlScalarNode(token), out pointer)) - { - return null; - } - } + return null; } }