From 1292e9ec7ccf54741a101bde0f9b8fb910bdb2a4 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sun, 8 Oct 2023 22:14:37 +1100 Subject: [PATCH 1/2] use some pattern matching --- .../OpenApiSpecVersionHelper.cs | 2 +- .../OpenApiRemoteReferenceCollector.cs | 9 +++---- .../V2/OpenApiDocumentDeserializer.cs | 4 +-- .../V2/OpenApiParameterDeserializer.cs | 2 +- .../Models/OpenApiComponents.cs | 27 +++++++------------ .../Models/OpenApiDocument.cs | 12 +++------ .../Models/OpenApiOperation.cs | 2 +- .../Models/OpenApiParameter.cs | 2 +- .../Services/OpenApiReferenceResolver.cs | 2 +- .../Services/OpenApiWalker.cs | 7 ++--- .../Validations/OpenApiValidator.cs | 2 +- .../Validations/Rules/OpenApiContactRules.cs | 2 +- .../Writers/OpenApiJsonWriter.cs | 4 +-- .../Writers/OpenApiYamlWriter.cs | 8 +++--- 14 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs b/src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs index 95ffceeaa..174c9c93e 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs @@ -18,7 +18,7 @@ public static OpenApiSpecVersion TryParseOpenApiSpecVersion(string value) if (int.TryParse(res, out int result)) { - if (result >= 2 && result < 3) + if (result is >= 2 and < 3) { return OpenApiSpecVersion.OpenApi2_0; } diff --git a/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs b/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs index 90cd3632c..12b5f8a99 100644 --- a/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs +++ b/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs @@ -39,14 +39,11 @@ public override void Visit(IOpenApiReferenceable referenceable) /// private void AddReference(OpenApiReference reference) { - if (reference != null) + if (reference is {IsExternal: true}) { - if (reference.IsExternal) + if (!_references.ContainsKey(reference.ExternalResource)) { - if (!_references.ContainsKey(reference.ExternalResource)) - { - _references.Add(reference.ExternalResource, reference); - } + _references.Add(reference.ExternalResource, reference); } } } diff --git a/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs b/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs index 92643cb92..779309d53 100644 --- a/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs +++ b/src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs @@ -172,7 +172,7 @@ private static void MakeServers(IList servers, ParsingContext con } // Create the Server objects - if (schemes != null && schemes.Count > 0) + if (schemes is {Count: > 0}) { foreach (var scheme in schemes) { @@ -295,7 +295,7 @@ private static void FixRequestBodyReferences(OpenApiDocument doc) { // Walk all unresolved parameter references // if id matches with request body Id, change type - if (doc.Components?.RequestBodies != null && doc.Components?.RequestBodies.Count > 0) + if (doc.Components?.RequestBodies is {Count: > 0}) { var fixer = new RequestBodyReferenceFixer(doc.Components?.RequestBodies); var walker = new OpenApiWalker(fixer); diff --git a/src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs index b0013d9ae..99ce35cbb 100644 --- a/src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs +++ b/src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs @@ -166,7 +166,7 @@ internal static partial class OpenApiV2Deserializer new( p => p.Schema?.Enum, (p, v) => { - if (p.Schema != null || v != null && v.Count > 0) + if (p.Schema != null || v is {Count: > 0}) { GetOrCreateSchema(p).Enum = v; } diff --git a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs index 31b7282f3..22b766398 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs @@ -128,8 +128,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Schemas, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Schema && + if (component.Reference is {Type: ReferenceType.Schema} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -146,8 +145,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Responses, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Response && + if (component.Reference is {Type: ReferenceType.Response} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -164,8 +162,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Parameters, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Parameter && + if (component.Reference is {Type: ReferenceType.Parameter} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -182,8 +179,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Examples, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Example && + if (component.Reference is {Type: ReferenceType.Example} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -200,8 +196,7 @@ public void SerializeAsV3(IOpenApiWriter writer) RequestBodies, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.RequestBody && + if (component.Reference is {Type: ReferenceType.RequestBody} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -218,8 +213,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Headers, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Header && + if (component.Reference is {Type: ReferenceType.Header} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -236,8 +230,7 @@ public void SerializeAsV3(IOpenApiWriter writer) SecuritySchemes, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.SecurityScheme && + if (component.Reference is {Type: ReferenceType.SecurityScheme} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -254,8 +247,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Links, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Link && + if (component.Reference is {Type: ReferenceType.Link} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); @@ -272,8 +264,7 @@ public void SerializeAsV3(IOpenApiWriter writer) Callbacks, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Callback && + if (component.Reference is {Type: ReferenceType.Callback} && component.Reference.Id == key) { component.SerializeAsV3WithoutReference(w); diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index c2fa42890..13d74f6f4 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -189,8 +189,7 @@ public void SerializeAsV2(IOpenApiWriter writer) Components?.Schemas, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Schema && + if (component.Reference is {Type: ReferenceType.Schema} && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); @@ -218,8 +217,7 @@ public void SerializeAsV2(IOpenApiWriter writer) parameters, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Parameter && + if (component.Reference is {Type: ReferenceType.Parameter} && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); @@ -236,8 +234,7 @@ public void SerializeAsV2(IOpenApiWriter writer) Components?.Responses, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.Response && + if (component.Reference is {Type: ReferenceType.Response} && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); @@ -254,8 +251,7 @@ public void SerializeAsV2(IOpenApiWriter writer) Components?.SecuritySchemes, (w, key, component) => { - if (component.Reference != null && - component.Reference.Type == ReferenceType.SecurityScheme && + if (component.Reference is {Type: ReferenceType.SecurityScheme} && component.Reference.Id == key) { component.SerializeAsV2WithoutReference(w); diff --git a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs index cdf18cddb..1d477b69d 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs @@ -280,7 +280,7 @@ public void SerializeAsV2(IOpenApiWriter writer) .SelectMany(static r => r.Value.Content?.Keys) .Concat( Responses - .Where(static r => r.Value.Reference != null && r.Value.Reference.HostDocument != null) + .Where(static r => r.Value.Reference is {HostDocument: not null}) .SelectMany(static r => r.Value.GetEffective(r.Value.Reference.HostDocument)?.Content?.Keys)) .Distinct() .ToList(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs index 553fd3da5..263548a15 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs @@ -246,7 +246,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer) } // explode - writer.WriteProperty(OpenApiConstants.Explode, _explode, _style.HasValue && _style.Value == ParameterStyle.Form); + writer.WriteProperty(OpenApiConstants.Explode, _explode, _style is ParameterStyle.Form); // allowReserved writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false); diff --git a/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs b/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs index 19e463e36..88e1b45f9 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs @@ -300,7 +300,7 @@ private void ResolveTags(IList tags) private bool IsUnresolvedReference(IOpenApiReferenceable possibleReference) { - return (possibleReference != null && possibleReference.UnresolvedReference); + return possibleReference is {UnresolvedReference: true}; } } } diff --git a/src/Microsoft.OpenApi/Services/OpenApiWalker.cs b/src/Microsoft.OpenApi/Services/OpenApiWalker.cs index 4811f647c..04d23ed37 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiWalker.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiWalker.cs @@ -614,12 +614,9 @@ internal void Walk(OpenApiRequestBody requestBody, bool isComponent = false) _visitor.Visit(requestBody); - if (requestBody != null) + if (requestBody is {Content: not null}) { - if (requestBody.Content != null) - { - Walk(OpenApiConstants.Content, () => Walk(requestBody.Content)); - } + Walk(OpenApiConstants.Content, () => Walk(requestBody.Content)); } Walk(requestBody as IOpenApiExtensible); } diff --git a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs index ae2c52721..641162af3 100644 --- a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs +++ b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs @@ -304,7 +304,7 @@ private void Validate(object item, Type type) } // Validate unresolved references as references - if (item is IOpenApiReferenceable potentialReference && potentialReference.UnresolvedReference) + if (item is IOpenApiReferenceable {UnresolvedReference: true}) { type = typeof(IOpenApiReferenceable); } diff --git a/src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs b/src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs index b64704375..cfa8d9927 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs @@ -21,7 +21,7 @@ public static class OpenApiContactRules (context, item) => { context.Enter("email"); - if (item != null && item.Email != null) + if (item is {Email: not null}) { if (!item.Email.IsEmailAddress()) { diff --git a/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs b/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs index 1ff94b319..ef8ef47b5 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs @@ -59,7 +59,7 @@ public override void WriteStartObject() var currentScope = StartScope(ScopeType.Object); - if (previousScope != null && previousScope.Type == ScopeType.Array) + if (previousScope is {Type: ScopeType.Array}) { currentScope.IsInArray = true; @@ -110,7 +110,7 @@ public override void WriteStartArray() var currentScope = StartScope(ScopeType.Array); - if (previousScope != null && previousScope.Type == ScopeType.Array) + if (previousScope is {Type: ScopeType.Array}) { currentScope.IsInArray = true; diff --git a/src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs b/src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs index be8794941..b68c6cd7d 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs @@ -48,7 +48,7 @@ public override void WriteStartObject() var currentScope = StartScope(ScopeType.Object); - if (previousScope != null && previousScope.Type == ScopeType.Array) + if (previousScope is {Type: ScopeType.Array}) { currentScope.IsInArray = true; @@ -76,7 +76,7 @@ public override void WriteEndObject() if (previousScope.ObjectCount == 0) { // If we are in an object, write a white space preceding the braces. - if (currentScope != null && currentScope.Type == ScopeType.Object) + if (currentScope is {Type: ScopeType.Object}) { Writer.Write(" "); } @@ -94,7 +94,7 @@ public override void WriteStartArray() var currentScope = StartScope(ScopeType.Array); - if (previousScope != null && previousScope.Type == ScopeType.Array) + if (previousScope is {Type: ScopeType.Array}) { currentScope.IsInArray = true; @@ -122,7 +122,7 @@ public override void WriteEndArray() if (previousScope.ObjectCount == 0) { // If we are in an object, write a white space preceding the braces. - if (currentScope != null && currentScope.Type == ScopeType.Object) + if (currentScope is {Type: ScopeType.Object}) { Writer.Write(" "); } From 3408bfc8285b8e866056478aa081113084fb3aa2 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 11 Oct 2023 22:04:20 +1100 Subject: [PATCH 2/2] Update OpenApiRemoteReferenceCollector.cs --- .../Services/OpenApiRemoteReferenceCollector.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs b/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs index 12b5f8a99..cc2879bd5 100644 --- a/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs +++ b/src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs @@ -39,12 +39,10 @@ public override void Visit(IOpenApiReferenceable referenceable) /// private void AddReference(OpenApiReference reference) { - if (reference is {IsExternal: true}) + if (reference is {IsExternal: true} && + !_references.ContainsKey(reference.ExternalResource)) { - if (!_references.ContainsKey(reference.ExternalResource)) - { - _references.Add(reference.ExternalResource, reference); - } + _references.Add(reference.ExternalResource, reference); } } }