Skip to content

Min API validation NRE when ValidationContext is not set on ValidateContext #61738

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
sbomer opened this issue Apr 29, 2025 · 1 comment · Fixed by #61854
Closed

Min API validation NRE when ValidationContext is not set on ValidateContext #61738

sbomer opened this issue Apr 29, 2025 · 1 comment · Fixed by #61854
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
Milestone

Comments

@sbomer
Copy link
Member

sbomer commented Apr 29, 2025

Repro:

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

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddValidation();
var app = builder.Build();

var model = new Model { Name = "Name" };

var validationOptions = app.Services.GetService<IOptions<ValidationOptions>>()!.Value;
if (!validationOptions.TryGetValidatableTypeInfo(typeof(Model), out var typeInfo))
{
    throw new InvalidOperationException("Type not found in validation options.");
}
var validateContext = new ValidateContext()
{
    ValidationOptions = validationOptions,
    // ValidationContext = new ValidationContext(model) // Uncomment this to fix it
};
await typeInfo.ValidateAsync(model, validateContext, CancellationToken.None);

[ValidatableType]
class Model
{
    [Required]
    public required string Name { get; init; }
}

This throws an exception:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.Http.Validation.ValidatablePropertyInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.Validation.ValidatableTypeInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /home/svbomer/src/test-apps/validationGen/Program.cs:line 21
@ghost ghost added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Apr 29, 2025
@BrennanConroy
Copy link
Member

Looks like we never followed up on this. Comments about it at #46349 (comment)

@gfoidl gfoidl added 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 and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Apr 30, 2025
@BrennanConroy BrennanConroy added this to the 10.0-preview5 milestone May 15, 2025
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

Successfully merging a pull request may close this issue.

3 participants