11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Collections . Concurrent ;
45using System . IO . Pipelines ;
56using System . Text . Json . Nodes ;
67using Microsoft . AspNetCore . Http ;
@@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.OpenApi;
1415/// </summary>
1516internal sealed class OpenApiSchemaStore
1617{
17- private readonly Dictionary < OpenApiSchemaKey , JsonNode > _schemas = new ( )
18+ private readonly ConcurrentDictionary < OpenApiSchemaKey , JsonNode > _schemas = new ( )
1819 {
1920 // Pre-populate OpenAPI schemas for well-defined types in ASP.NET Core.
2021 [ new OpenApiSchemaKey ( typeof ( IFormFile ) , null ) ] = new JsonObject
@@ -48,8 +49,8 @@ internal sealed class OpenApiSchemaStore
4849 } ,
4950 } ;
5051
51- public readonly Dictionary < OpenApiSchema , string ? > SchemasByReference = new ( OpenApiSchemaComparer . Instance ) ;
52- private readonly Dictionary < string , int > _referenceIdCounter = new ( ) ;
52+ public readonly ConcurrentDictionary < OpenApiSchema , string ? > SchemasByReference = new ( OpenApiSchemaComparer . Instance ) ;
53+ private readonly ConcurrentDictionary < string , int > _referenceIdCounter = new ( ) ;
5354
5455 /// <summary>
5556 /// Resolves the JSON schema for the given type and parameter description.
@@ -59,13 +60,7 @@ internal sealed class OpenApiSchemaStore
5960 /// <returns>A <see cref="JsonObject" /> representing the JSON schema associated with the key.</returns>
6061 public JsonNode GetOrAdd ( OpenApiSchemaKey key , Func < OpenApiSchemaKey , JsonNode > valueFactory )
6162 {
62- if ( _schemas . TryGetValue ( key , out var schema ) )
63- {
64- return schema ;
65- }
66- var targetSchema = valueFactory ( key ) ;
67- _schemas . Add ( key , targetSchema ) ;
68- return targetSchema ;
63+ return _schemas . GetOrAdd ( key , valueFactory ( key ) ) ;
6964 }
7065
7166 /// <summary>
0 commit comments