Skip to content

Validation bug of DateOnly for minimalAPI in ASP.NET Core 10 Preview 4 #62065

@vancodocton

Description

@vancodocton

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

As my understanding, source generation of model validation for DateOnly is not correctly: it validates DateOnly.MaxValue and DateOnly.MinValue, which are static helper props, that cause infinite recursion.

Expected Behavior

Validation for DateOnly should be worked.

Steps To Reproduce

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <InvariantGlobalization>true</InvariantGlobalization>
    <PublishAot>true</PublishAot>
  </PropertyGroup>

  <PropertyGroup>
    <InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.AspNetCore.Http.Validation.Generated</InterceptorsNamespaces>
  </PropertyGroup>
  
</Project>
using Microsoft.AspNetCore.Http.HttpResults;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.AddProblemDetails();

builder.Services.AddValidation();

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

var app = builder.Build();

var todosApi = app.MapGroup("/todos");
todosApi.MapPost("/", Created<Todo> (Todo todo) =>
{
    return TypedResults.Created($"/todos/{todo.Id}", todo);
})
    .WithName("PostTodo");

await app.RunAsync("http://localhost:5054");

public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);

[JsonSerializable(typeof(Todo))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
curl --location 'http://localhost:5054/todos' \
--header 'Content-Type: application/json' \
--data '{
    "id": 1,
    "title": "Hello",
    "dueBy": "2025-05-22"
}'

Exceptions (if any)

{
    "type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
    "title": "System.InvalidOperationException",
    "status": 500,
    "detail": "Maximum validation depth of 32 exceeded at 'DueBy.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue.MaxValue' in 'DateOnly'. This is likely caused by a circular reference in the object graph. Consider increasing the MaxDepth in ValidationOptions if deeper validation is required."
}

.NET Version

10.0.100-preview.4.25258.110

Anything else?

No response

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