Skip to content

[Minimal API] Route parameters are not set as required when using WithOpenApi and Nullable Reference Types are disabled #46746

@marcominerva

Description

@marcominerva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If I define this endpoint:

#nullable enable
app.MapGet("/api/{group}/people_nullable_enabled", (string group) =>
{
    return TypedResults.Ok(new { GroupName = group });
})
.WithOpenApi();

swagger.json file correctly sets the group route parameter as required:

"parameters": [
  {
    "name": "group",
    "in": "path",
    "required": true,
    "style": "simple",
    "schema": {
      "type": "string"
    }
  }
]

However, If I disable Nullable Reference Types, than this parameter is no longer marked as required:

#nullable disable
app.MapGet("/api/{group}/people_nullable_disabled", (string group) =>
{
    return TypedResults.Ok(new { GroupName = group });
})
.WithOpenApi();

"parameters": [
  {
    "name": "group",
    "in": "path",
    "style": "simple",
    "schema": {
      "type": "string"
    }
  }
]

If I try to invoke the endpoint using Swagger UI, a comma is passed as route argument:

image

The problem is related to the WithOpenApi extension method. In fact, if I remove it, than the group route parameter is again set as required:

#nullable disable
app.MapGet("/api/{group}/people_nullable_disabled", (string group) =>
{
    return TypedResults.Ok(new { GroupName = group });
});

"parameters": [
  {
    "name": "group",
    "in": "path",
    "required": true,
    "style": "simple",
    "schema": {
      "type": "string"
    }
  }
]

Expected Behavior

The group route parameter should be required in any cases, regardless the presence of Nullable Reference Types and the usage of WithOpenApi.

Steps To Reproduce

Minimal repro here: https://github.com/marcominerva/PathIssue

Exceptions (if any)

No response

.NET Version

7.0.103

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