Skip to content

Minimal API validation does not use ProblemDetails #61533

@marcominerva

Description

@marcominerva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying the new validation support that comes with .NET 10 Preview 3. Validation errors aren't returned as ProblemDetails response.

using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http.Validation;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddValidation();
builder.Services.AddProblemDetails();

builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseHttpsRedirection();

app.UseExceptionHandler();
app.UseStatusCodePages();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();

    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/openapi/v1.json", app.Environment.ApplicationName);
    });
}

app.MapPost("/api/todo", (Todo todo) =>
{
    return TypedResults.Ok();
});

app.Run();

[ValidatableType]
public class Todo
{
    [Required]
    [Range(1, 10)]
    public int Id { get; set; }

    [StringLength(10)]
    public required string Title { get; set; }
}

If I try to make a POST request with Id = 0, I get this error:

{
  "title": "One or more validation errors occurred.",
  "errors": {
    "Id": [
      "The field Id must be between 1 and 10."
    ]
  }
}

And the Content-Type of the response is application/json, while it should be application/problem+json.

Expected Behavior

The validation error should use ProblemDetails service and return the application/problem+json Content Type.

Steps To Reproduce

Minimal repro at https://github.com/marcominerva/OpenApi10

.NET Version

10.0.100-preview.3.25201.16

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions