AutoNormalize, a new MIT-licensed NuGet package, normalizes request strings during System.Text.Json contract building and inside EF Core's SaveChanges instead of through a global string converter, sidestepping password trimming, immutable record and dictionary key pitfalls. The author explains the design and shares benchmarks.
Key points
- AutoNormalize 1.0.0 ships as AutoNormalize.AspNetCore and AutoNormalize.EntityFrameworkCore NuGet packages, MIT licensed.
- AddAutoNormalize() wires JSON options, minimal API endpoint filters and MVC model binders in one call.
- Its options cover trimming, converting empty strings to null and Unicode normalization to FormC.
- It hooks IJsonTypeInfoResolver.Modifiers so positional record parameters are normalized before the constructor runs.
- [PreserveWhitespace] and [DoNotNormalize] opt out, while password, token, secret, salt and signature names are shielded by default.
- Dictionary keys stay untouched; measured means are 4.83 ns for clean strings and 283.55 ns for a six-property DTO.
Why it matters
Most teams solve this with trim calls scattered through controllers plus a validation layer, and the real value here is placement: normalization happens where the JSON contract is built, so positional records and Native AOT builds stop being special cases.
The work left is evaluation. A v1.0.0 package sits in the path of every request, so measure it against your own DTO shapes rather than trusting one microbenchmark, and confirm how it interacts with the validators you already run — your rules will now see normalized values — plus your EF Core mappings and query filters. Roll it out in one bounded context first.
Real-world use
- A sign-up endpoint where an email with a trailing space would create a second account.
- Registration payloads that mix trimmable fields with passwords, API tokens or codes where whitespace is significant.
- APIs accepting Dictionary<string, string> bodies whose keys must not be rewritten.
- EF Core entities that must be canonicalized before SaveChanges() persists them.
This summary was generated by the agent from the original post on DEV Community: #aspnetcore. The full text and all rights belong to its author and publisher. Go to original