Skip to content

.NET 9 - OpenAPI Not Generating Output #58805

Closed
@sbwalker

Description

@sbwalker

Is there an existing issue for this?

  • I have searched the existing issues

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions