Skip to content

Commit 553133b

Browse files
committed
Reset OpenApiSchemaTransformerContext before each invocation
1 parent bef4dae commit 553133b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ internal async Task ApplySchemaTransformersAsync(OpenApiSchema schema, Type type
155155
};
156156
for (var i = 0; i < _openApiOptions.SchemaTransformers.Count; i++)
157157
{
158+
// Reset context object to base state before running each transformer.
159+
context.UpdateJsonTypeInfo(jsonTypeInfo, null);
158160
var transformer = _openApiOptions.SchemaTransformers[i];
159161
// If the transformer is a type-based transformer, we need to initialize and finalize it
160162
// once in the context of the top-level assembly and not the child properties we are invoking

src/OpenApi/test/Transformers/SchemaTransformerTests.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@ public async Task SchemaTransformer_CanAccessTypeAndParameterDescriptionForParam
1919
builder.MapPost("/todo", (Todo todo) => { });
2020

2121
var options = new OpenApiOptions();
22+
var firstInvocationOnSecondTransformer = true;
2223
options.AddSchemaTransformer((schema, context, cancellationToken) =>
24+
{
25+
ValidateContext(context);
26+
return Task.CompletedTask;
27+
})
28+
.AddSchemaTransformer((schema, context, cancellationToken) =>
29+
{
30+
// Coverage for https://github.com/dotnet/aspnetcore/issues/56899
31+
if (firstInvocationOnSecondTransformer)
32+
{
33+
Assert.Equal(typeof(Todo), context.JsonTypeInfo.Type);
34+
firstInvocationOnSecondTransformer = false;
35+
}
36+
// Rest of the state is still consistent
37+
ValidateContext(context);
38+
return Task.CompletedTask;
39+
});
40+
41+
await VerifyOpenApiDocument(builder, options, document => { });
42+
43+
static void ValidateContext(OpenApiSchemaTransformerContext context)
2344
{
2445
if (context.JsonPropertyInfo == null)
2546
{
@@ -42,10 +63,7 @@ public async Task SchemaTransformer_CanAccessTypeAndParameterDescriptionForParam
4263
{
4364
Assert.Equal(typeof(DateTime), context.JsonTypeInfo.Type);
4465
}
45-
return Task.CompletedTask;
46-
});
47-
48-
await VerifyOpenApiDocument(builder, options, document => { });
66+
}
4967
}
5068

5169
[Fact]

0 commit comments

Comments
 (0)