Skip to content

Commit f194ac3

Browse files
committed
Address feedback
1 parent f4ec475 commit f194ac3

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

src/OpenApi/src/Services/OpenApiDocumentProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.AspNetCore.OpenApi;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
7-
87
using System.Linq;
98

109
namespace Microsoft.Extensions.ApiDescriptions;

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
45
using System.ComponentModel;
56
using System.ComponentModel.DataAnnotations;
67
using System.Diagnostics;
@@ -29,6 +30,7 @@ internal sealed class OpenApiSchemaService(
2930
IOptions<JsonOptions> jsonOptions,
3031
IOptionsMonitor<OpenApiOptions> optionsMonitor)
3132
{
33+
private readonly ConcurrentDictionary<Type, string?> _schemaIdCache = new();
3234
private readonly OpenApiJsonSchemaContext _jsonSchemaContext = new(new(jsonOptions.Value.SerializerOptions));
3335
private readonly JsonSerializerOptions _jsonSerializerOptions = new(jsonOptions.Value.SerializerOptions)
3436
{
@@ -137,7 +139,15 @@ internal async Task<OpenApiSchema> GetOrCreateUnresolvedSchemaAsync(OpenApiDocum
137139
internal async Task<IOpenApiSchema> GetOrCreateSchemaAsync(OpenApiDocument document, Type type, IServiceProvider scopedServiceProvider, IOpenApiSchemaTransformer[] schemaTransformers, ApiParameterDescription? parameterDescription = null, CancellationToken cancellationToken = default)
138140
{
139141
var schema = await GetOrCreateUnresolvedSchemaAsync(document, type, scopedServiceProvider, schemaTransformers, parameterDescription, cancellationToken);
140-
var baseSchemaId = optionsMonitor.Get(documentName).CreateSchemaReferenceId(_jsonSerializerOptions.GetTypeInfo(type));
142+
143+
// Cache the root schema IDs since we expect to be called
144+
// on the same type multiple times within an API
145+
var baseSchemaId = _schemaIdCache.GetOrAdd(type, t =>
146+
{
147+
var jsonTypeInfo = _jsonSerializerOptions.GetTypeInfo(t);
148+
return optionsMonitor.Get(documentName).CreateSchemaReferenceId(jsonTypeInfo);
149+
});
150+
141151
return ResolveReferenceForSchema(document, schema, baseSchemaId);
142152
}
143153

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/SnapshotTestHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ public static Task Verify(string source, IIncrementalGenerator generator, Dictio
9797
.ScrubLinesWithReplace(line => InterceptsLocationRegex().Replace(line, "[InterceptsLocation]"))
9898
.UseDirectory(SkipOnHelixAttribute.OnHelix()
9999
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "snapshots")
100-
: "snapshots")
101-
.AutoVerify();
100+
: "snapshots");
102101
}
103102

104103
public static async Task VerifyOpenApi(Compilation compilation, Action<OpenApiDocument> verifyFunc)

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/OpenApiDocumentIntegrationTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public async Task VerifyOpenApiDocument(string documentName, OpenApiSpecVersion
3636
var outputDirectory = Path.Combine(baseSnapshotsDirectory, version.ToString());
3737
await Verifier.Verify(json)
3838
.UseDirectory(outputDirectory)
39-
.UseParameters(documentName)
40-
.AutoVerify();
39+
.UseParameters(documentName);
4140
}
4241
}

0 commit comments

Comments
 (0)