Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwaggerUI broken by primitive return values + endpoint filters #52128

Closed
1 task done
erikhejl opened this issue Nov 16, 2023 · 7 comments
Closed
1 task done

SwaggerUI broken by primitive return values + endpoint filters #52128

erikhejl opened this issue Nov 16, 2023 · 7 comments
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug.

Comments

@erikhejl
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A combination of returning primitives from a minimal API delegate and adding an endpoint filter will cause the SwaggerUI page to crash. Primitive return values and endpoint filters do not cause this problem individually. This is in an application targeting .NET Core 7.

Expected Behavior

SwaggerUI page renders.

Steps To Reproduce

app.MapGet("/", () =>
{
return 1;
}).AddEndpointFilter(async (context, nextFilter) => await nextFilter(context));

Exceptions (if any)

ArgumentException: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'System.Threading.Tasks.ValueTask`1[System.Object] WrapObjectAsValueTask(System.Object)' (Parameter 'arg0')

System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, string methodParamName, string argumentParamName, int index)
System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0)
Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateFilterPipeline(MethodInfo methodInfo, Expression targetExpression, RequestDelegateFactoryContext factoryContext, Expression<Func<HttpContext, object>> targetFactory)
Microsoft.AspNetCore.Http.RequestDelegateFactory.CreateTargetableRequestDelegate(MethodInfo methodInfo, Expression targetExpression, RequestDelegateFactoryContext factoryContext, Expression<Func<HttpContext, object>> targetFactory)
Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(Delegate handler, RequestDelegateFactoryOptions options, RequestDelegateMetadataResult metadataResult)
Microsoft.AspNetCore.Routing.RouteEndpointDataSource.CreateRouteEndpointBuilder(RouteEntry entry, RoutePattern groupPrefix, IReadOnlyList<Action<EndpointBuilder>> groupConventions, IReadOnlyList<Action<EndpointBuilder>> groupFinallyConventions)
Microsoft.AspNetCore.Routing.RouteEndpointDataSource.get_Endpoints()
Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.CreateEndpointsUnsynchronized()
Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.EnsureEndpointsInitialized()
Microsoft.AspNetCore.Routing.DataSourceDependentCache<T>.Initialize()
System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func<MatcherBuilder> matcherBuilderFactory)
Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|8_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task<Matcher> matcherTask)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

.NET Version

8.0.100

Anything else?

.NET SDK:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.8d38d0cc

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed:
Workload version: 8.0.100-manifests.8d38d0cc
[wasm-tools-net6]
Installation Source: VS 17.8.34309.116
Manifest Version: 8.0.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.net6\8.0.0\WorkloadManifest.json
Install Type: Msi

Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31a71

.NET SDKs installed:
8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 16, 2023
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@ryanpeters-MSFT
Copy link

Has a fix been found for this? This sample code will fail, but changing the X and Y properties to string will work fine:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapPost("/addsmallnumbers", (AddRequest request) =>
{
    return request.X + request.Y;

}).AddEndpointFilter(async (ctx, next) => 
{
    return await next(ctx);
});

app.Run();

public class AddRequest
{
    public int X { get; set; } = default;
    public int Y { get; set; } = default;
}

@erikhejl
Copy link
Author

erikhejl commented Apr 18, 2024

It's still broken as of version 8.0.4. You can work around it by casting as (object), but this will defeat reflection based swagger generation done.

@shanerogers
Copy link

I just downloaded the lastest .net 8 sdk... I still get this issue..

@captainsafia captainsafia self-assigned this Aug 20, 2024
@captainsafia captainsafia added bug This issue describes a behavior which is not expected - a bug. area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Aug 20, 2024
@captainsafia
Copy link
Member

I'll try to take a look at this bug and see if we can slot it in for servicing.

In the meantime, you can try seeing if compiling your application using the source generator for minimal APIs will work for you. It doesn't run into the same conversion bug we are seeing here with the Linq expressions we generated.

<PropertyGroup>
    <EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
</PropertyGroup>

@captainsafia
Copy link
Member

This is fixed now in .NET 10 Preview 1 (we've already branched for .NET 9). .NET 10 preview builds aren't available yet. I'd like to wait until they are, have folks try them out to validate the fix, then backport to 8.0/9.0 once we've confirmed that this fix resolves the issue.

@UdiAzulay
Copy link

The issue only occur if the method that return int result has also an endpoint filter, without the filter, it works.
Please patch a fix for .Net 8 ASAP (as it's the only available stable release now)

@captainsafia
Copy link
Member

Thanks for your patience here, everyone! The fix for this has been merged into all release branches and will ship in .NET 8.0.11, 9.0, and 10.

@github-project-automation github-project-automation bot moved this from 8.0.x to Done in Servicing Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug.
Projects
Status: Done
Development

No branches or pull requests

6 participants