From 9aa78970d208b2c6c5221ae8dffb56e06c8a835c Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 2 Oct 2023 19:49:56 +1100 Subject: [PATCH 1/2] inline some out variables --- .../ParseNodes/MapNode.cs | 7 ++----- .../ParseNodes/PropertyNode.cs | 3 +-- src/Microsoft.OpenApi.Readers/ParsingContext.cs | 3 +-- src/Microsoft.OpenApi/Services/LoopDetector.cs | 3 +-- .../Validations/ValidationRuleSet.cs | 3 +-- .../V2Tests/OpenApiContactTests.cs | 3 +-- .../V3Tests/OpenApiContactTests.cs | 3 +-- .../V3Tests/OpenApiSchemaTests.cs | 15 +++++---------- 8 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs index 0ee5934ce..c822e05dd 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs @@ -47,8 +47,7 @@ public PropertyNode this[string key] { get { - YamlNode node; - if (this._node.Children.TryGetValue(new YamlScalarNode(key), out node)) + if (this._node.Children.TryGetValue(new YamlScalarNode(key), out var node)) { return new PropertyNode(Context, key, node); } @@ -193,9 +192,7 @@ public T GetReferencedObject(ReferenceType referenceType, string referenceId) public string GetReferencePointer() { - YamlNode refNode; - - if (!_node.Children.TryGetValue(new YamlScalarNode("$ref"), out refNode)) + if (!_node.Children.TryGetValue(new YamlScalarNode("$ref"), out var refNode)) { return null; } diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs index 2dd2c7e8a..0bd42e25f 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs @@ -30,8 +30,7 @@ public void ParseField( IDictionary> fixedFields, IDictionary, Action> patternFields) { - Action fixedFieldMap; - var found = fixedFields.TryGetValue(Name, out fixedFieldMap); + var found = fixedFields.TryGetValue(Name, out var fixedFieldMap); if (fixedFieldMap != null) { diff --git a/src/Microsoft.OpenApi.Readers/ParsingContext.cs b/src/Microsoft.OpenApi.Readers/ParsingContext.cs index 0cd98c67e..ccf97117d 100644 --- a/src/Microsoft.OpenApi.Readers/ParsingContext.cs +++ b/src/Microsoft.OpenApi.Readers/ParsingContext.cs @@ -206,8 +206,7 @@ public void StartObject(string objectName) /// If method returns false a loop was detected and the key is not added. public bool PushLoop(string loopId, string key) { - Stack stack; - if (!_loopStacks.TryGetValue(loopId, out stack)) + if (!_loopStacks.TryGetValue(loopId, out var stack)) { stack = new Stack(); _loopStacks.Add(loopId, stack); diff --git a/src/Microsoft.OpenApi/Services/LoopDetector.cs b/src/Microsoft.OpenApi/Services/LoopDetector.cs index 249cab51d..4fb1b8b4e 100644 --- a/src/Microsoft.OpenApi/Services/LoopDetector.cs +++ b/src/Microsoft.OpenApi/Services/LoopDetector.cs @@ -17,8 +17,7 @@ internal class LoopDetector /// If method returns false a loop was detected and the key is not added. public bool PushLoop(T key) { - Stack stack; - if (!_loopStacks.TryGetValue(typeof(T), out stack)) + if (!_loopStacks.TryGetValue(typeof(T), out var stack)) { stack = new Stack(); _loopStacks.Add(typeof(T), stack); diff --git a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs index eca7bc8de..3d469d3c2 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs @@ -30,8 +30,7 @@ public sealed class ValidationRuleSet : IEnumerable /// Either the rules related to the type, or an empty list. public IList FindRules(Type type) { - IList results = null; - _rules.TryGetValue(type, out results); + _rules.TryGetValue(type, out var results); return results ?? _emptyRules; } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs index 71489d39f..6e0ae0472 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiContactTests.cs @@ -21,10 +21,9 @@ public void ParseStringContactFragmentShouldSucceed() } "; var reader = new OpenApiStringReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var contact = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi2_0, out diagnostic); + var contact = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi2_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs index be78f942b..1e8cd0934 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiContactTests.cs @@ -21,10 +21,9 @@ public void ParseStringContactFragmentShouldSucceed() } "; var reader = new OpenApiStringReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var contact = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var contact = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs index 0101d9c6e..bcb088c3f 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs @@ -54,10 +54,9 @@ public void ParsePrimitiveSchemaFragmentShouldSucceed() using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "primitiveSchema.yaml"))) { var reader = new OpenApiStreamReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var schema = reader.ReadFragment(stream, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var schema = reader.ReadFragment(stream, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -81,10 +80,9 @@ public void ParsePrimitiveStringSchemaFragmentShouldSucceed() } "; var reader = new OpenApiStringReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var schema = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var schema = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -107,10 +105,9 @@ public void ParseExampleStringFragmentShouldSucceed() ""baz"": [ 1,2] }"; var reader = new OpenApiStringReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -135,10 +132,9 @@ public void ParseEnumFragmentShouldSucceed() ""baz"" ]"; var reader = new OpenApiStringReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); @@ -212,10 +208,9 @@ public void ParsePathFragmentShouldSucceed() description: Ok "; var reader = new OpenApiStringReader(); - var diagnostic = new OpenApiDiagnostic(); // Act - var openApiAny = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out diagnostic); + var openApiAny = reader.ReadFragment(input, OpenApiSpecVersion.OpenApi3_0, out var diagnostic); // Assert diagnostic.Should().BeEquivalentTo(new OpenApiDiagnostic()); From 087a5333bcd28f56c18ff08d6434a16d95723f0c Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 3 Oct 2023 10:08:56 +1100 Subject: [PATCH 2/2] Update PropertyNode.cs --- src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs index 0bd42e25f..1d665ce50 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs @@ -30,9 +30,7 @@ public void ParseField( IDictionary> fixedFields, IDictionary, Action> patternFields) { - var found = fixedFields.TryGetValue(Name, out var fixedFieldMap); - - if (fixedFieldMap != null) + if (fixedFields.TryGetValue(Name, out var fixedFieldMap)) { try {