Skip to content

Commit 3710020

Browse files
committed
openapi net10 fixes
1 parent bba4417 commit 3710020

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/IntelliTect.Coalesce/Api/OpenApi/CoalesceApiDescriptionProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ public void OnProvidersExecuting(ApiDescriptionProviderContext context)
3434
var method = new ReflectionMethodViewModel(methodInfo, cvm, cvm);
3535

3636
ProcessStandardParameters(operation, method);
37+
#if !NET10_0_OR_GREATER
3738
FixEnumSerializationType(operation, method);
39+
#endif
3840
}
3941
}
4042

43+
#if !NET10_0_OR_GREATER
4144
/// <summary>
4245
/// Workaround https://github.com/dotnet/aspnetcore/issues/61327 by correcting enum types from String
4346
/// to their underlying integral type. Coalesce also just generally only handles enums as numbers on the wire anyway,
@@ -57,6 +60,7 @@ private void FixEnumSerializationType(ApiDescription operation, ReflectionMethod
5760
}
5861
}
5962
}
63+
#endif
6064

6165
private void ProcessStandardParameters(ApiDescription operation, MethodViewModel method)
6266
{

src/IntelliTect.Coalesce/Api/OpenApi/CoalesceApiOperationFilter.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,30 @@ ReflectionRepository reflectionRepository
3939
.ToLookup(d => (d.HttpMethod, d.RelativePath));
4040
}
4141

42-
public Task TransformAsync(
42+
public async Task TransformAsync(
4343
OpenApiOperation operation,
4444
OpenApiOperationTransformerContext context,
4545
CancellationToken cancellationToken)
4646
{
4747
if (context.Description.ActionDescriptor is not ControllerActionDescriptor cad ||
4848
!cad.ControllerTypeInfo.IsAssignableTo(typeof(BaseApiController)))
4949
{
50-
return Task.CompletedTask;
50+
return;
5151
}
5252

5353
var methodInfo = cad.MethodInfo;
5454
var cvm = reflectionRepository.GetClassViewModel(methodInfo.DeclaringType!)!;
5555
var method = new ReflectionMethodViewModel(methodInfo, cvm, cvm);
5656

57-
AddOtherBodyTypes(operation, context);
57+
await AddOtherBodyTypes(operation, context, cancellationToken);
5858
ProcessDataSources(operation, context, method);
5959
ProcessStandardParameters(operation, method);
60-
61-
return Task.CompletedTask;
6260
}
6361

6462
/// <summary>
6563
/// Workaround https://github.com/dotnet/aspnetcore/issues/58329
6664
/// </summary>
67-
private async void AddOtherBodyTypes(OpenApiOperation operation, OpenApiOperationTransformerContext context)
65+
private async Task AddOtherBodyTypes(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken ct)
6866
{
6967
Type? docServiceType = Type.GetType("Microsoft.AspNetCore.OpenApi.OpenApiDocumentService,Microsoft.AspNetCore.OpenApi");
7068
if (docServiceType is null) return;
@@ -77,6 +75,24 @@ private async void AddOtherBodyTypes(OpenApiOperation operation, OpenApiOperatio
7775
foreach (var otherDescription in otherDescriptions)
7876
{
7977
object docService = context.ApplicationServices.GetRequiredKeyedService(docServiceType, context.DocumentName);
78+
#if NET10_0_OR_GREATER
79+
// In .NET 10, GetRequestBodyAsync requires OpenApiDocument as the first parameter
80+
var resultTask = docServiceType
81+
.GetMethod("GetRequestBodyAsync", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)?
82+
.Invoke(docService, [
83+
// OpenApiDocument document,
84+
context.Document,
85+
// ApiDescription description,
86+
otherDescription,
87+
// IServiceProvider scopedServiceProvider,
88+
context.ApplicationServices,
89+
// IOpenApiSchemaTransformer[] schemaTransformers,
90+
// TODO: Too hard to acquire schema transformers here.
91+
Array.Empty<IOpenApiSchemaTransformer>(),
92+
// CancellationToken cancellationToken
93+
ct
94+
]) as Task<OpenApiRequestBody>;
95+
#else
8096
var resultTask = docServiceType
8197
.GetMethod("GetRequestBodyAsync", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)?
8298
.Invoke(docService, [
@@ -88,8 +104,9 @@ private async void AddOtherBodyTypes(OpenApiOperation operation, OpenApiOperatio
88104
// TODO: Too hard to acquire schema transformers here.
89105
Array.Empty<IOpenApiSchemaTransformer>(),
90106
// CancellationToken cancellationToken
91-
CancellationToken.None
107+
ct
92108
]) as Task<OpenApiRequestBody>;
109+
#endif
93110

94111
if (resultTask is null) continue;
95112

0 commit comments

Comments
 (0)