Skip to content

Minimal API validation does not use ProblemDetails #61533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
marcominerva opened this issue Apr 17, 2025 · 1 comment
Closed
1 task done

Minimal API validation does not use ProblemDetails #61533

marcominerva opened this issue Apr 17, 2025 · 1 comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs

Comments

@marcominerva
Copy link
Contributor

marcominerva commented Apr 17, 2025

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

@ghost ghost added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Apr 17, 2025
@captainsafia captainsafia added feature-validation Issues related to model validation in minimal and controller-based APIs and removed feature-openapi labels Apr 28, 2025
@captainsafia
Copy link
Member

Closing as dupe of #61219.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs
Projects
None yet
Development

No branches or pull requests

3 participants