Skip to content

Commit

Permalink
Merge pull request #1382 from SimonCropp/remove-redundant-casts
Browse files Browse the repository at this point in the history
remove redundant casts
  • Loading branch information
baywet authored Oct 2, 2023
2 parents 4bf34d3 + 0252298 commit 9c3668c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancellationToken = default)
{
MemoryStream bufferedStream;
if (input is MemoryStream)
if (input is MemoryStream stream)
{
bufferedStream = (MemoryStream)input;
bufferedStream = stream;
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
return newObject;
}

if (!(openApiAny is OpenApiString))
if (openApiAny is not OpenApiString apiString)
{
return openApiAny;
}

var value = ((OpenApiString)openApiAny).Value;
var value = apiString.Value;
var type = schema?.Type;
var format = schema?.Format;

if (((OpenApiString)openApiAny).IsExplicit())
if (apiString.IsExplicit())
{
// More narrow type detection for explicit strings, only check types that are passed as strings
if (schema == null)
Expand Down Expand Up @@ -113,7 +113,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
}
}

return openApiAny;
return apiString;
}

if (value is null or "null")
Expand Down Expand Up @@ -247,7 +247,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS

if (type == "string")
{
return openApiAny;
return apiString;
}

if (type == "boolean")
Expand All @@ -262,7 +262,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
// If data conflicts with the given type, return a string.
// This converter is used in the parser, so it does not perform any validations,
// but the validator can be used to validate whether the data and given type conflicts.
return openApiAny;
return apiString;
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Extensions/OpenAPIWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal static class OpenAPIWriterExtensions
/// <returns></returns>
internal static OpenApiWriterSettings GetSettings(this IOpenApiWriter openApiWriter)
{
if (openApiWriter is OpenApiWriterBase)
if (openApiWriter is OpenApiWriterBase @base)
{
return ((OpenApiWriterBase)openApiWriter).Settings;
return @base.Settings;
}
return new OpenApiWriterSettings();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,10 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
{
Style = In switch
{
ParameterLocation.Query => (ParameterStyle?)ParameterStyle.Form,
ParameterLocation.Header => (ParameterStyle?)ParameterStyle.Simple,
ParameterLocation.Path => (ParameterStyle?)ParameterStyle.Simple,
ParameterLocation.Cookie => (ParameterStyle?)ParameterStyle.Form,
ParameterLocation.Query => ParameterStyle.Form,
ParameterLocation.Header => ParameterStyle.Simple,
ParameterLocation.Path => ParameterStyle.Simple,
ParameterLocation.Cookie => ParameterStyle.Form,
_ => (ParameterStyle?)ParameterStyle.Simple,
};

Expand Down
8 changes: 2 additions & 6 deletions src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ public static void ValidateDataTypeMismatch(
}

// If value is not a string and also not an object, there is a data mismatch.
if (!(value is OpenApiObject))
if (value is not OpenApiObject anyObject)
{
context.CreateWarning(
ruleName,
DataTypeMismatchedErrorMessage);
return;
}

var anyObject = (OpenApiObject)value;

foreach (var key in anyObject.Keys)
{
context.Enter(key);
Expand Down Expand Up @@ -115,16 +113,14 @@ public static void ValidateDataTypeMismatch(
}

// If value is not a string and also not an array, there is a data mismatch.
if (!(value is OpenApiArray))
if (value is not OpenApiArray anyArray)
{
context.CreateWarning(
ruleName,
DataTypeMismatchedErrorMessage);
return;
}

var anyArray = (OpenApiArray)value;

for (int i = 0; i < anyArray.Count; i++)
{
context.Enter(i.ToString());
Expand Down

0 comments on commit 9c3668c

Please sign in to comment.