Skip to content

Commit

Permalink
Merge pull request #1416 from SimonCropp/use-some-pattern-matching
Browse files Browse the repository at this point in the history
use some pattern matching
  • Loading branch information
baywet authored Oct 24, 2023
2 parents f5309ce + 3408bfc commit a4ffde3
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static OpenApiSpecVersion TryParseOpenApiSpecVersion(string value)

if (int.TryParse(res, out var result))
{
if (result >= 2 && result < 3)
if (result is >= 2 and < 3)
{
return OpenApiSpecVersion.OpenApi2_0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ public override void Visit(IOpenApiReferenceable referenceable)
/// </summary>
private void AddReference(OpenApiReference reference)
{
if (reference != null)
if (reference is {IsExternal: true} &&
!_references.ContainsKey(reference.ExternalResource))
{
if (reference.IsExternal)
{
if (!_references.ContainsKey(reference.ExternalResource))
{
_references.Add(reference.ExternalResource, reference);
}
}
_references.Add(reference.ExternalResource, reference);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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;
}
Expand Down
27 changes: 9 additions & 18 deletions src/Microsoft.OpenApi/Models/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,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);
Expand All @@ -144,8 +143,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);
Expand All @@ -162,8 +160,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);
Expand All @@ -180,8 +177,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);
Expand All @@ -198,8 +194,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);
Expand All @@ -216,8 +211,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);
Expand All @@ -234,8 +228,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);
Expand All @@ -252,8 +245,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);
Expand All @@ -270,8 +262,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);
Expand Down
12 changes: 4 additions & 8 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,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);
Expand Down Expand Up @@ -215,8 +214,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);
Expand All @@ -233,8 +231,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);
Expand All @@ -251,8 +248,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);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,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();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private void ResolveTags(IList<OpenApiTag> tags)

private bool IsUnresolvedReference(IOpenApiReferenceable possibleReference)
{
return (possibleReference != null && possibleReference.UnresolvedReference);
return possibleReference is {UnresolvedReference: true};
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Validations/OpenApiValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Writers/OpenApiJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(" ");
}
Expand All @@ -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;

Expand Down Expand Up @@ -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(" ");
}
Expand Down

0 comments on commit a4ffde3

Please sign in to comment.