dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.44k stars 10.02k forks source link

.NET 9 - OpenAPI Not Generating Output #58805

Open sbwalker opened 5 hours ago

sbwalker commented 5 hours ago

Is there an existing issue for this?

Describe the bug

I am trying to integrate OpenAPI into an existing Blazor project which was recently upgraded to .NET 9.

I added the following project reference to the Server project:

    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rc.2.24474.3" />

I modified the Startup.cs (yes this project has never migrated to the "new" Program.cs approach - but that should not affect the result):

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddMvc();
            services.AddRazorPages();
            services.AddOpenApi();
            ...
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            ...
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapOpenApi();
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapRazorPages();
            });
            ...
        }

Note that this application is using traditional Controllers and Razor Pages - not minimal APIs.

When I run the application and browse to /openapi/v1.json I get a blank screen with no output:

Image

The Visual Studio console contains the following information:

Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executing endpoint 'HTTP: GET /openapi/{documentName}.json'
Microsoft.AspNetCore.Routing.EndpointMiddleware: Information: Executed endpoint 'HTTP: GET /openapi/{documentName}.json'
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished HTTP/1.1 GET http://localhost:44357/openapi/v1.json - 200 - - 981.2280ms

Expected Behavior

Browsing to /openapi/v1.json should display the Open API document

Steps To Reproduce

No response

Exceptions (if any)

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll

.NET Version

9.0.0-rc.2.24473.5

Anything else?

Note that this application already had Swagger integrated previously:

            services.AddSwaggerGen(options =>
            {
                options.CustomSchemaIds(type => type.ToString()); // Handle SchemaId already used for different type
            });
            services.TryAddSwagger(_useSwagger);

...

            if (_useSwagger)
            {
                app.UseSwagger();
                app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/" + Constants.Version + "/swagger.json", Constants.PackageId + " " + Constants.Version); });
            }

and the Swagger UI still works fine:

Image

BrennanConroy commented 4 hours ago

Please turn on more logging (debug/trace) so you can see the exception details, or set a breakpoint on InvalidOperationException in VS so you can see where the exception is coming from.

sbwalker commented 4 hours ago

the actual exception is:

Unsupported parameter source: ModelBinding
Microsoft.AspNetCore.OpenApi
   at Microsoft.AspNetCore.OpenApi.OpenApiDocumentService.<GetParametersAsync>d__28.MoveNext()
   at Microsoft.AspNetCore.OpenApi.OpenApiDocumentService.<GetOperationAsync>d__21.MoveNext()
   at Microsoft.AspNetCore.OpenApi.OpenApiDocumentService.<GetOperationsAsync>d__20.MoveNext()
   at Microsoft.AspNetCore.OpenApi.OpenApiDocumentService.<GetOpenApiPathsAsync>d__19.MoveNext()
   at Microsoft.AspNetCore.OpenApi.OpenApiDocumentService.<GetOpenApiDocumentAsync>d__12.MoveNext()
   at Microsoft.AspNetCore.Builder.OpenApiEndpointRouteBuilderExtensions.<>c__DisplayClass0_0.<<MapOpenApi>b__0>d.MoveNext()
   at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>F56B68D2B55B5B7B373BA2E4796D897848BC0F04A969B1AF6260183E8B9E0BAF2__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass2_0.<<MapGet0>g__RequestHandler|5>d.MoveNext()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<<Invoke>g__AwaitRequestTask|7_0>d.MoveNext()
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.<Invoke>d__5.MoveNext()
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.<Invoke>d__6.MoveNext()
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.<Invoke>d__11.MoveNext()
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
   at Oqtane.Infrastructure.JwtMiddleware.<Invoke>d__2.MoveNext() 
   in C:\Source\Projects\oqtane.framework\Oqtane.Server\Infrastructure\Middleware\JwtMiddleware.cs:line 88

Line 88 in Jwtmiddleware is simply:

if (_next != null) await _next(context);