Skip to content

Commit

Permalink
Define StringComparer for all Dictionaries of string key type
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Feb 18, 2024
1 parent aba085f commit 2819ce5
Show file tree
Hide file tree
Showing 24 changed files with 85 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Thumbs.db
test/WebSites/CliExample/wwwroot/api-docs/v1/*.json
test/WebSites/CliExampleWithFactory/wwwroot/api-docs/v1/*.json
test/WebSites/NswagClientExample/NSwagClient/
*ncrunch*
*ncrunch*
BenchmarkDotNet.Artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace DotSwashbuckle.AspNetCore.Benchmark.Logic
public class DotSwashbuckleGenerator
{

public static string CreateSwaggerDoc(IServiceProvider serviceProvider, bool isYaml, bool isV2)
public static async Task<string> CreateSwaggerDoc(IServiceProvider serviceProvider, bool isYaml, bool isV2)
{
// 3) Retrieve Swagger via configured provider
var swaggerProvider = serviceProvider.GetRequiredService<ISwaggerProvider>();
var swagger = swaggerProvider.GetSwagger(
var swagger = await swaggerProvider.GetSwaggerAsync(
"v1",
null,
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public void GlobalSetup()
}

[Benchmark]
public string DotSwashbuckleOpenApiV2()
public async Task<string> DotSwashbuckleOpenApiV2()
{
return DotSwashbuckleGenerator.CreateSwaggerDoc(
return await DotSwashbuckleGenerator.CreateSwaggerDoc(
assemblyProviders["Basic.dll"],
false,
true
Expand Down
4 changes: 2 additions & 2 deletions benches/DotSwashbuckle.AspNetCore.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace DotSwashbuckle.AspNetCore.Console;

public class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
var serviceProvider = AssemblyServiceProvider.GetServiceProvider(
AssemblyLoadContext.Default.LoadFromAssemblyPath(
Expand All @@ -15,7 +15,7 @@ public static void Main(string[] args)

for (var i = 0; i < 1000; i++)
{
DotSwashbuckleGenerator.CreateSwaggerDoc(
await DotSwashbuckleGenerator.CreateSwaggerDoc(
serviceProvider,
false,
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Models;

Expand All @@ -7,7 +8,7 @@ public class ApiTestRunnerOptions
{
public ApiTestRunnerOptions()
{
OpenApiDocs = new Dictionary<string, OpenApiDocument>();
OpenApiDocs = new Dictionary<string, OpenApiDocument>(StringComparer.Ordinal);
ContentValidators = new List<IContentValidator> { new JsonContentValidator() };
GenerateOpenApiFiles = false;
FileOutputRoot = null;
Expand Down
6 changes: 3 additions & 3 deletions src/DotSwashbuckle.AspNetCore.Cli/CommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public CommandRunner(string commandName, string commandDescription, TextWriter o
{
CommandName = commandName;
CommandDescription = commandDescription;
_argumentDescriptors = new Dictionary<string, string>();
_optionDescriptors = new Dictionary<string, OptionDescriptor>();
_argumentDescriptors = new Dictionary<string, string>(StringComparer.Ordinal);
_optionDescriptors = new Dictionary<string, OptionDescriptor>(StringComparer.Ordinal);
_runFunc = (namedArgs) => { return 1; }; // noop
_subRunners = new List<CommandRunner>();
_output = output;
Expand Down Expand Up @@ -70,7 +70,7 @@ public int Run(IEnumerable<string> args)

private bool TryParseArgs(IEnumerable<string> args, out IDictionary<string, string> namedArgs)
{
namedArgs = new Dictionary<string, string>();
namedArgs = new Dictionary<string, string>(StringComparer.Ordinal);
var argsQueue = new Queue<string>(args);

// Process options first
Expand Down
5 changes: 3 additions & 2 deletions src/DotSwashbuckle.AspNetCore.ReDoc/ReDocMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -108,7 +109,7 @@ private async Task RespondWithIndexHtml(HttpResponse response)

private IDictionary<string, string> GetIndexArguments()
{
return new Dictionary<string, string>()
return new Dictionary<string, string>(StringComparer.Ordinal)
{
{ "%(DocumentTitle)", _options.DocumentTitle },
{ "%(HeadContent)", _options.HeadContent },
Expand Down
2 changes: 1 addition & 1 deletion src/DotSwashbuckle.AspNetCore.ReDoc/ReDocOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ public class ConfigObject
public bool SortPropsAlphabetically { get; set; } = false;

[JsonExtensionData]
public Dictionary<string, object> AdditionalItems { get; set; } = new Dictionary<string, object>();
public Dictionary<string, object> AdditionalItems { get; set; } = new Dictionary<string, object>(StringComparer.Ordinal);
}
}
6 changes: 6 additions & 0 deletions src/DotSwashbuckle.AspNetCore.Swagger/ISwaggerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.OpenApi.Models;

namespace DotSwashbuckle.AspNetCore.Swagger
Expand All @@ -11,6 +12,11 @@ OpenApiDocument GetSwagger(
string documentName,
string host = null,
string basePath = null);

Task<OpenApiDocument> GetSwaggerAsync(
string documentName,
string host = null,
string basePath = null);
}

public class UnknownSwaggerDocument : InvalidOperationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
var schema = new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>(),
Properties = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal),
Required = new SortedSet<string>(),
AdditionalPropertiesAllowed = false
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SchemaRepository(string documentName = null)

public string DocumentName { get; }

public Dictionary<string, OpenApiSchema> Schemas { get; private set; } = new Dictionary<string, OpenApiSchema>();
public Dictionary<string, OpenApiSchema> Schemas { get; private set; } = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal);

public void RegisterType(Type type, string schemaId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ private OpenApiSchema GenerateSchemaFromFormParameters(
IEnumerable<ApiParameterDescription> formParameters,
SchemaRepository schemaRepository)
{
var properties = new Dictionary<string, OpenApiSchema>();
var properties = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal);
var requiredPropertyNames = new List<string>();

foreach (var formParameter in formParameters)
Expand Down Expand Up @@ -601,7 +601,7 @@ private OpenApiMediaType CreateResponseMediaType(ModelMetadata modelMetadata, Sc
};
}

private static readonly Dictionary<string, OperationType> OperationTypeMap = new Dictionary<string, OperationType>
private static readonly Dictionary<string, OperationType> OperationTypeMap = new Dictionary<string, OperationType>(StringComparer.Ordinal)
{
{ "GET", OperationType.Get },
{ "PUT", OperationType.Put },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class SwaggerGeneratorOptions
{
public SwaggerGeneratorOptions()
{
SwaggerDocs = new Dictionary<string, OpenApiInfo>();
SwaggerDocs = new Dictionary<string, OpenApiInfo>(StringComparer.Ordinal);
DocInclusionPredicate = DefaultDocInclusionPredicate;
OperationIdSelector = DefaultOperationIdSelector;
TagsSelector = DefaultTagsSelector;
SortKeySelector = DefaultSortKeySelector;
SecuritySchemesSelector = null;
SchemaComparer = StringComparer.Ordinal;
Servers = new List<OpenApiServer>();
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>();
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>(StringComparer.Ordinal);
SecurityRequirements = new List<OpenApiSecurityRequirement>();
ParameterFilters = new List<IParameterFilter>();
RequestBodyFilters = new List<IRequestBodyFilter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.StaticFiles;
using System.Linq;
using System;

namespace DotSwashbuckle.AspNetCore.SwaggerUI
{
Expand Down Expand Up @@ -110,7 +111,7 @@ private async Task RespondWithIndexHtml(HttpResponse response)

private IDictionary<string, string> GetIndexArguments()
{
return new Dictionary<string, string>()
return new Dictionary<string, string>(StringComparer.Ordinal)
{
{ "%(DocumentTitle)", _options.DocumentTitle },
{ "%(HeadContent)", _options.HeadContent },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public class ConfigObject
public string ValidatorUrl { get; set; } = null;

[JsonExtensionData]
public Dictionary<string, object> AdditionalItems { get; set; } = new Dictionary<string, object>();
public Dictionary<string, object> AdditionalItems { get; set; } = new Dictionary<string, object>(StringComparer.Ordinal);
}

public class UrlDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Linq;
using System.Linq;
using System;

namespace DotSwashbuckle.AspNetCore.ApiTesting.Test
{
Expand Down Expand Up @@ -433,7 +434,7 @@ public void Validate_ReturnsError_IfKnownPropertyDoesNotMatchPropertySchema(
var openApiSchema = new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
Properties = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal)
{
[ "id" ] = new OpenApiSchema { Type = propertySchemaType }
}
Expand Down Expand Up @@ -606,7 +607,7 @@ public void Validate_SupportsReferencedSchemas_IfDefinedInProvidedOpenApiDocumen
{
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, OpenApiSchema>
Schemas = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal)
{
["ref"] = new OpenApiSchema { Type = "number" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void Validate_ThrowsException_IfRequiredContentIsNotPresent(
RequestBody = new OpenApiRequestBody
{
Required = true,
Content = new Dictionary<string, OpenApiMediaType>
Content = new Dictionary<string, OpenApiMediaType>(StringComparer.Ordinal)
{
[ "text/plain" ] = new OpenApiMediaType()
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public void Validate_ThrowsException_IfContentMediaTypeIsNotSpecified(
{
RequestBody = new OpenApiRequestBody
{
Content = new Dictionary<string, OpenApiMediaType>
Content = new Dictionary<string, OpenApiMediaType>(StringComparer.Ordinal)
{
[ "application/json" ] = new OpenApiMediaType()
}
Expand Down Expand Up @@ -326,7 +326,7 @@ public void Validate_DelegatesContentValidationToInjectedContentValidators(
{
RequestBody = new OpenApiRequestBody
{
Content = new Dictionary<string, OpenApiMediaType>
Content = new Dictionary<string, OpenApiMediaType>(StringComparer.Ordinal)
{
[ "application/json" ] = new OpenApiMediaType
{
Expand Down Expand Up @@ -370,7 +370,7 @@ private OpenApiDocument DocumentWithOperation(string pathTemplate, OperationType
},
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, OpenApiSchema>()
Schemas = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal)
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void Validate_ThrowsException_IfRequiredHeaderIsNotPresent(
{
[ "201" ] = new OpenApiResponse
{
Headers = new Dictionary<string, OpenApiHeader>
Headers = new Dictionary<string, OpenApiHeader>(StringComparer.Ordinal)
{
[ "test-header" ] = new OpenApiHeader
{
Expand Down Expand Up @@ -94,7 +95,7 @@ public void Validate_ThrowsException_IfHeaderIsNotOfSpecifiedType(
{
[ "201" ] = new OpenApiResponse
{
Headers = new Dictionary<string, OpenApiHeader>
Headers = new Dictionary<string, OpenApiHeader>(StringComparer.Ordinal)
{
[ "test-header" ] = new OpenApiHeader
{
Expand Down Expand Up @@ -135,7 +136,7 @@ public void Validate_ThrowsException_IfExpectedContentIsNotPresent(
{
[ "200" ] = new OpenApiResponse
{
Content = new Dictionary<string, OpenApiMediaType>
Content = new Dictionary<string, OpenApiMediaType>(StringComparer.Ordinal)
{
["text/plain"] = new OpenApiMediaType()
}
Expand Down Expand Up @@ -168,7 +169,7 @@ public void Validate_ThrowsException_IfContentMediaTypeIsNotSpecified(
{
[ "200" ] = new OpenApiResponse
{
Content = new Dictionary<string, OpenApiMediaType>
Content = new Dictionary<string, OpenApiMediaType>(StringComparer.Ordinal)
{
["application/json"] = new OpenApiMediaType()
}
Expand Down Expand Up @@ -201,7 +202,7 @@ public void Validate_DelegatesContentValidationToInjectedContentValidators(
{
[ "200" ] = new OpenApiResponse
{
Content = new Dictionary<string, OpenApiMediaType>
Content = new Dictionary<string, OpenApiMediaType>(StringComparer.Ordinal)
{
["application/json"] = new OpenApiMediaType
{
Expand Down Expand Up @@ -245,7 +246,7 @@ private OpenApiDocument DocumentWithOperation(string pathTemplate, OperationType
},
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, OpenApiSchema>()
Schemas = new Dictionary<string, OpenApiSchema>(StringComparer.Ordinal)
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public void GenerateSchema_HonorsSerializerSetting_TypeNameHandling(
Assert.NotNull(schema.Discriminator);
Assert.Equal("$type", schema.Discriminator.PropertyName);
Assert.Equal(
expected: new Dictionary<string, string>
expected: new Dictionary<string, string>(StringComparer.Ordinal)
{
[string.Format(expectedDiscriminatorMappingKeyFormat, "BaseType")] = "#/components/schemas/BaseType",
[string.Format(expectedDiscriminatorMappingKeyFormat, "SubType1")] = "#/components/schemas/SubType1",
Expand Down
Loading

0 comments on commit 2819ce5

Please sign in to comment.