From c4f02dae769e315e8e22b19cc2710a4299a455b0 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 2 Oct 2023 19:57:00 +1100 Subject: [PATCH] use pattern matching in casts --- .../ParseNodes/JsonPointerExtensions.cs | 7 ++----- src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs | 6 ++---- src/Microsoft.OpenApi.Readers/YamlHelper.cs | 3 +-- src/Microsoft.OpenApi/Validations/OpenApiValidator.cs | 3 +-- src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs | 3 +-- src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs | 3 +-- test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs | 3 +-- 7 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs index d30863955..b8db0fad0 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs @@ -26,16 +26,13 @@ public static YamlNode Find(this JsonPointer currentPointer, YamlNode baseYamlNo var pointer = baseYamlNode; foreach (var token in currentPointer.Tokens) { - var sequence = pointer as YamlSequenceNode; - - if (sequence != null) + if (pointer is YamlSequenceNode sequence) { pointer = sequence.Children[Convert.ToInt32(token)]; } else { - var map = pointer as YamlMappingNode; - if (map != null) + if (pointer is YamlMappingNode map) { if (!map.Children.TryGetValue(new YamlScalarNode(token), out pointer)) { diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs index 0ee5934ce..61a833143 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs @@ -152,8 +152,7 @@ public override Dictionary CreateSimpleMap(Func map) try { Context.StartObject(key); - YamlScalarNode scalarNode = n.Value as YamlScalarNode; - if (scalarNode == null) + if (n.Value is not YamlScalarNode scalarNode) { throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context); } @@ -205,8 +204,7 @@ public string GetReferencePointer() public string GetScalarValue(ValueNode key) { - var scalarNode = _node.Children[new YamlScalarNode(key.GetScalarValue())] as YamlScalarNode; - if (scalarNode == null) + if (_node.Children[new YamlScalarNode(key.GetScalarValue())] is not YamlScalarNode scalarNode) { throw new OpenApiReaderException($"Expected scalar at line {_node.Start.Line} for key {key.GetScalarValue()}", Context); } diff --git a/src/Microsoft.OpenApi.Readers/YamlHelper.cs b/src/Microsoft.OpenApi.Readers/YamlHelper.cs index 90794b080..e49f05e7b 100644 --- a/src/Microsoft.OpenApi.Readers/YamlHelper.cs +++ b/src/Microsoft.OpenApi.Readers/YamlHelper.cs @@ -12,8 +12,7 @@ internal static class YamlHelper { public static string GetScalarValue(this YamlNode node) { - var scalarNode = node as YamlScalarNode; - if (scalarNode == null) + if (node is not YamlScalarNode scalarNode) { throw new OpenApiException($"Expected scalar at line {node.Start.Line}"); } diff --git a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs index a0aee12e7..838450cc0 100644 --- a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs +++ b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs @@ -311,8 +311,7 @@ private void Validate(object item, Type type) } // Validate unresolved references as references - var potentialReference = item as IOpenApiReferenceable; - if (potentialReference != null && potentialReference.UnresolvedReference) + if (item is IOpenApiReferenceable potentialReference && potentialReference.UnresolvedReference) { type = typeof(IOpenApiReferenceable); } diff --git a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs index eca7bc8de..26d9cf0e3 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs @@ -176,8 +176,7 @@ private static ValidationRuleSet BuildDefaultRuleSet() foreach (var property in rules) { var propertyValue = property.GetValue(null); // static property - ValidationRule rule = propertyValue as ValidationRule; - if (rule != null) + if (propertyValue is ValidationRule rule) { ruleSet.Add(rule); } diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs index 537273cac..08aeb4efd 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs @@ -142,8 +142,7 @@ public static void WriteOptionalObject( { if (value != null) { - var values = value as IEnumerable; - if (values != null && !values.GetEnumerator().MoveNext()) + if (value is IEnumerable values && !values.GetEnumerator().MoveNext()) { return; // Don't render optional empty collections } diff --git a/test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs b/test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs index 6d2eafc01..c268dbaf0 100644 --- a/test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs +++ b/test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs @@ -45,8 +45,7 @@ public static IEnumerable GetSchemas() var json = JObject.Parse(listJsonStr); foreach (var item in json.Properties()) { - var versions = GetProp(item.Value, "versions") as JObject; - if (versions == null) + if (GetProp(item.Value, "versions") is not JObject versions) continue; foreach (var prop in versions.Properties()) {