RDG009: Invalid nested AsParameters
Value | |
---|---|
Rule ID | RDG009 |
Fix is breaking or non-breaking | Non-breaking |
Cause
This diagnostic is emitted by the Request Delegate Generator when an endpoint contains invalid nested [AsParameters]
.
Rule description
Types that are used for surrogate binding via the [AsParameters]
attribute must not contain nested types that are also annotated with the [AsParameters]
attribute:
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder();
var todos = new[]
{
new Todo(1, "Write tests", DateTime.UtcNow.AddDays(2)),
new Todo(2, "Fix tests",DateTime.UtcNow.AddDays(1))
};
builder.Services.AddSingleton(todos);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
var app = builder.Build();
app.MapGet("/v1/todos/{id}", ([AsParameters] TodoItemRequest request) =>
{
return request.Todos.ToList().Find(todoItem => todoItem.Id == request.Id) is Todo todo
? Results.Ok(todo)
: Results.NotFound();
});
app.Run();
struct TodoItemRequest
{
public int Id { get; set; }
[AsParameters]
public Todo[] Todos { get; set; }
}
internal record Todo(int Id, string Task, DateTime DueDate);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
How to fix violations
Remove the nested nested AsParameters
attribute:
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder();
var todos = new[]
{
new Todo(1, "Write tests", DateTime.UtcNow.AddDays(2)),
new Todo(2, "Fix tests",DateTime.UtcNow.AddDays(1))
};
builder.Services.AddSingleton(todos);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
var app = builder.Build();
app.MapGet("/v1/todos/{id}", ([AsParameters] TodoItemRequest request) =>
{
return request.Todos.ToList().Find(todoItem => todoItem.Id == request.Id) is Todo todo
? Results.Ok(todo)
: Results.NotFound();
});
app.Run();
struct TodoItemRequest
{
public int Id { get; set; }
//[AsParameters]
public Todo[] Todos { get; set; }
}
internal record Todo(int Id, string Task, DateTime DueDate);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
When to suppress warnings
This warning can not be suppressed.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
ASP.NET Core
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for