Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/string comparisons #2108

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Workbench/MainModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ internal async Task ParseDocumentAsync()
{
if (!string.IsNullOrWhiteSpace(_inputFile))
{
stream = _inputFile.StartsWith("http") ? await _httpClient.GetStreamAsync(_inputFile)
stream = _inputFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? await _httpClient.GetStreamAsync(_inputFile)
: new FileStream(_inputFile, FileMode.Open);
}
else
Expand All @@ -241,7 +241,7 @@ internal async Task ParseDocumentAsync()
};
if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile))
{
settings.BaseUrl = _inputFile.StartsWith("http") ? new(_inputFile)
settings.BaseUrl = _inputFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? new(_inputFile)
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
}

Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,39 @@ public static RuntimeExpression Build(string expression)
{
Utils.CheckArgumentNullOrEmpty(expression);

if (!expression.StartsWith(Prefix))
if (!expression.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase))
{
return new CompositeExpression(expression);
}

// $url
if (expression == UrlExpression.Url)
if (expression.Equals(UrlExpression.Url, StringComparison.Ordinal))
{
return new UrlExpression();
}

// $method
if (expression == MethodExpression.Method)
if (expression.Equals(MethodExpression.Method, StringComparison.Ordinal))
{
return new MethodExpression();
}

// $statusCode
if (expression == StatusCodeExpression.StatusCode)
if (expression.Equals(StatusCodeExpression.StatusCode, StringComparison.Ordinal))
{
return new StatusCodeExpression();
}

// $request.
if (expression.StartsWith(RequestExpression.Request))
if (expression.StartsWith(RequestExpression.Request, StringComparison.Ordinal))
{
var subString = expression.Substring(RequestExpression.Request.Length);
var source = SourceExpression.Build(subString);
return new RequestExpression(source);
}

// $response.
if (expression.StartsWith(ResponseExpression.Response))
if (expression.StartsWith(ResponseExpression.Response, StringComparison.Ordinal))
{
var subString = expression.Substring(ResponseExpression.Response.Length);
var source = SourceExpression.Build(subString);
Expand Down
9 changes: 5 additions & 4 deletions src/Microsoft.OpenApi/Expressions/SourceExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;

Expand Down Expand Up @@ -37,27 +38,27 @@ protected SourceExpression(string value)
var expressions = expression.Split('.');
if (expressions.Length == 2)
{
if (expression.StartsWith(HeaderExpression.Header))
if (expression.StartsWith(HeaderExpression.Header, StringComparison.Ordinal))
{
// header.
return new HeaderExpression(expressions[1]);
}

if (expression.StartsWith(QueryExpression.Query))
if (expression.StartsWith(QueryExpression.Query, StringComparison.Ordinal))
{
// query.
return new QueryExpression(expressions[1]);
}

if (expression.StartsWith(PathExpression.Path))
if (expression.StartsWith(PathExpression.Path, StringComparison.Ordinal))
{
// path.
return new PathExpression(expressions[1]);
}
}

// body
if (expression.StartsWith(BodyExpression.Body))
if (expression.StartsWith(BodyExpression.Body, StringComparison.Ordinal))
{
var subString = expression.Substring(BodyExpression.Body.Length);
if (string.IsNullOrEmpty(subString))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
Expand All @@ -26,7 +27,7 @@ public static void AddExtension<T>(this T element, string name, IOpenApiExtensio
Utils.CheckArgumentNull(element);
Utils.CheckArgumentNullOrEmpty(name);

if (!name.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix))
if (!name.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase))
{
throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private static void WriteHostInfoV2(IOpenApiWriter writer, IList<OpenApiServer>?
else
{
var relativeUrl = firstServerUrl.OriginalString;
if (relativeUrl.StartsWith("//"))
if (relativeUrl.StartsWith("//", StringComparison.OrdinalIgnoreCase))
{
var pathPosition = relativeUrl.IndexOf('/', 3);
writer.WriteProperty(OpenApiConstants.Host, relativeUrl.Substring(0, pathPosition));
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public string ReferenceV3
{
return Id;
}
if (Id.StartsWith("http"))
if (Id.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
return Id;
}
Expand Down Expand Up @@ -238,7 +238,7 @@ private string GetExternalReferenceV3()
return ExternalResource + "#" + Id;
}

if (Id.StartsWith("http"))
if (Id.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
return Id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiContact> _contactPatternFields = new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiContact LoadContact(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiDocument> _openApiPatternFields = new()
{
// We have no semantics to verify X- nodes, therefore treat them as just values.
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

private static void MakeServers(IList<OpenApiServer> servers, ParsingContext context, RootNode rootNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiExternalDocs> _externalDocsPatternFields =
new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiExternalDocs LoadExternalDocs(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiHeader> _headerPatternFields = new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

private static OpenApiSchema GetOrCreateSchema(OpenApiHeader p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiInfo> _infoPatternFields = new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiInfo LoadInfo(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiLicense> _licensePatternFields = new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiLicense LoadLicense(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Models.Interfaces;
using System;

namespace Microsoft.OpenApi.Reader.V2
{
Expand Down Expand Up @@ -80,16 +81,16 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiOperation> _operationPatternFields =
new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

private static readonly FixedFieldMap<OpenApiResponses> _responsesFixedFields = new();

private static readonly PatternFieldMap<OpenApiResponses> _responsesPatternFields =
new()
{
{s => !s.StartsWith("x-"), (o, p, n, t) => o.Add(p, LoadResponse(n, t))},
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => !s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, t) => o.Add(p, LoadResponse(n, t))},
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

internal static OpenApiOperation LoadOperation(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiParameter> _parameterPatternFields =
new()
{
{s => s.StartsWith("x-") && !s.Equals(OpenApiConstants.ExamplesExtension, StringComparison.OrdinalIgnoreCase),
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase) && !s.Equals(OpenApiConstants.ExamplesExtension, StringComparison.OrdinalIgnoreCase),
(o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiPathItem> _pathItemPatternFields =
new()
{
{s => s.StartsWith("x-", StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))},
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))},
};

public static OpenApiPathItem LoadPathItem(ParseNode node, OpenApiDocument hostDocument)
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiPathsDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
Expand All @@ -17,8 +18,8 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiPaths> _pathsPatternFields = new()
{
{s => s.StartsWith("/"), (o, k, n, t) => o.Add(k, LoadPathItem(n, t))},
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith("/", StringComparison.OrdinalIgnoreCase), (o, k, n, t) => o.Add(k, LoadPathItem(n, t))},
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiPaths LoadPaths(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiResponse> _responsePatternFields =
new()
{
{s => s.StartsWith("x-") && !s.Equals(OpenApiConstants.ExamplesExtension, StringComparison.OrdinalIgnoreCase),
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase) && !s.Equals(OpenApiConstants.ExamplesExtension, StringComparison.OrdinalIgnoreCase),
(o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Models.References;
using System;

namespace Microsoft.OpenApi.Reader.V2
{
Expand Down Expand Up @@ -153,7 +154,7 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiSchema> _openApiSchemaPatternFields = new PatternFieldMap<OpenApiSchema>
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@
private static readonly PatternFieldMap<OpenApiSecurityScheme> _securitySchemePatternFields =
new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node, OpenApiDocument hostDocument)
{
// Reset the local variables every time this method is called.
// TODO: Change _flow to a tempStorage variable to make the deserializer thread-safe.

Check warning on line 86 in src/Microsoft.OpenApi/Reader/V2/OpenApiSecuritySchemeDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
_flowValue = null;
_flow = new();

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Reader/V2/OpenApiTagDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
Expand Down Expand Up @@ -31,7 +32,7 @@ internal static partial class OpenApiV2Deserializer

private static readonly PatternFieldMap<OpenApiTag> _tagPatternFields = new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};

public static OpenApiTag LoadTag(ParseNode n, OpenApiDocument hostDocument)
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Reader/V2/OpenApiV2Deserializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
Expand Down Expand Up @@ -98,7 +99,7 @@ private static (string, string) GetReferenceIdAndExternalResource(string pointer
{
var refSegments = pointer.Split('/');
var refId = refSegments.Last();
var isExternalResource = !refSegments.First().StartsWith("#");
var isExternalResource = !refSegments.First().StartsWith("#", StringComparison.OrdinalIgnoreCase);

string externalResource = isExternalResource ? $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}" : null;

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiV2VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public OpenApiReference ConvertToOpenApiReference(string reference, ReferenceTyp
}
else if (segments.Length == 2)
{
if (reference.StartsWith("#"))
if (reference.StartsWith("#", StringComparison.OrdinalIgnoreCase))
{
// "$ref": "#/definitions/Pet"
try
Expand All @@ -178,7 +178,7 @@ public OpenApiReference ConvertToOpenApiReference(string reference, ReferenceTyp
// Where fragments point into a non-OpenAPI document, the id will be the complete fragment identifier
var id = segments[1];
// $ref: externalSource.yaml#/Pet
if (id.StartsWith("/definitions/"))
if (id.StartsWith("/definitions/", StringComparison.Ordinal))
{
var localSegments = id.Split('/');
var referencedType = GetReferenceTypeV2FromName(localSegments[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal static partial class OpenApiV2Deserializer
private static readonly PatternFieldMap<OpenApiXml> _xmlPatternFields =
new()
{
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
};

public static OpenApiXml LoadXml(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Expressions;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
Expand All @@ -21,8 +22,8 @@ internal static partial class OpenApiV3Deserializer
private static readonly PatternFieldMap<OpenApiCallback> _callbackPatternFields =
new()
{
{s => !s.StartsWith("x-"), (o, p, n, t) => o.AddPathItem(RuntimeExpression.Build(p), LoadPathItem(n, t))},
{s => s.StartsWith("x-"), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))},
{s => !s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, t) => o.AddPathItem(RuntimeExpression.Build(p), LoadPathItem(n, t))},
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))},
};

public static IOpenApiCallback LoadCallback(ParseNode node, OpenApiDocument hostDocument)
Expand Down
Loading
Loading