Skip to content

Incorrect swagger.json definition for GET endpoints in Minimal API when using Keyed Services #50704

@marcominerva

Description

@marcominerva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying Keyed Services with Minimal APIs using .NET 8.0.100-rc.1.23455.8. I have the following code:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddKeyedScoped<TestService>("test1");
builder.Services.AddKeyedScoped<TestService>("test2");

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
app.UseHttpsRedirection();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapGet("/api/sample", ([FromKeyedServices("test1")] TestService testService) =>
{
    return TypedResults.Ok();
}).WithOpenApi();

app.Run();

public class TestService
{
    public void Foo() { }
}

When I run the application, I see that the Swagger UI incorrectly requires a body for the request:

image

In fact, the swagger.json file contains the following wrong definition:

{
    "openapi": "3.0.1",
    "info": {
        "title": "KeyedServicesIssue",
        "version": "1.0"
    },
    "paths": {
        "/api/sample": {
            "get": {
                "tags": [
                    "KeyedServicesIssue"
                ],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/TestService"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "TestService": {
                "type": "object",
                "additionalProperties": false
            }
        }
    }
}

Expected Behavior

swagger.json should not require a body request.

Steps To Reproduce

Minimal repo here: https://github.com/marcominerva/KeyedServicesIssue

Exceptions (if any)

No response

.NET Version

8.0.100-rc.1.23455.8

Anything else?

No response

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions