Skip to content

Make length validators trim safe in generated binders and the example AOT ready - #432

Merged
shibayan merged 2 commits into
masterfrom
aot-length-validators
Oct 21, 2026
Merged

shibayan merged 2 commits into
masterfrom
aot-length-validators

Conversation

@shibayan

Copy link
Copy Markdown
Owner

Summary

I published the example app with Native AOT against the packed library to check AOT locally. That turned up IL2026 in the binder that [PromptBindable] generates, which the CI verification from #425 did not catch, because its model used no length attributes:

Sharprompt_Example_Models_MyFormModel.Binder.g.cs(37,31): warning IL2026: Using member
'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' ...

The example's Bind sample would also have thrown at the end under AOT, because it printed the model with reflection-based JsonSerializer.Serialize.

Which validation attribute constructors are [RequiresUnreferencedCode] (.NET 10)

Constructor Why it uses reflection This PR
MinLength(int), MaxLength(int), MaxLength(), Length(int, int) IsValid reads Count through reflection only for values that are neither string nor ICollection Suppressed in generated code when the validated type is string, an array, or implements ICollection
Compare(string) Reads another property of the model Warning kept
Range(Type, string, string) Uses the type's converter Warning kept

Changes

  • PromptBindableGenerator: a length attribute qualifies when the validated values (the property type, or its element type for a List prompt) are string, arrays, or implement non-generic ICollection. The binder then creates the attribute in a CreateLengthValidatorN() factory method that carries UnconditionalSuppressMessage("Trimming", "IL2026") with that justification. Everything else is emitted as before. HashSet<T> is not ICollection, so it is deliberately not covered.
  • Native AOT verification: SignupModel now uses [MinLength], [MaxLength], and [Length] (including on a List<string> property). A new step checks that [MinLength] rejects input. With the previous generator, the AOT publish fails on the generated binder.
  • Example app:
    • It serializes with a source-generated ExampleJsonContext.
    • It sets PublishAot, so dotnet publish samples/Sharprompt.Example -c Release -r <RID> produces a Native AOT build with no warnings.
  • Docs (en/ja): a new "Validation Attributes" section in the Native AOT part of advanced.md describes how each attribute behaves under trimming.

Note for reviewers

The trim analyzer also reports IL2026 on the line where [MinLength], [MaxLength], or [Length] is applied. That happens whether or not Sharprompt is used, so Sharprompt cannot remove it. The docs show suppressing it at that site for string and collection properties. The example model and the AOT verification model do the same, with comments, so the verification still fails on any warning from Sharprompt's generated code.

Testing

  • PromptBindableGeneratorTests:
    • Length attributes on string, string?, a password, List<string>, and string[][] generate code with no trim analyzer diagnostics.
    • [MinLength] on a type that only exposes Count, [Compare], and [Range(Type, ...)] still report IL2026 in generated code.
    • The first test fails on master.
  • tests/Sharprompt.NativeAot/verify.ps1 on win-x64: 65/65 for net8.0 and net10.0, with no warnings. Run against the previous generator, the publish fails.
  • The example builds with 0 warnings, and dotnet publish -r win-x64 produces a 4.3 MB native executable with no warnings.
  • dotnet build, dotnet test (1352 passed), and dotnet format --verify-no-changes.
  • VitePress docs build.

🤖 Generated with Claude Code

… AOT ready

Publishing the example app with Native AOT showed IL2026 in the binder
that [PromptBindable] generates: MinLengthAttribute, MaxLengthAttribute
and LengthAttribute constructors are [RequiresUnreferencedCode], because
IsValid reads Count through reflection for values that are neither
strings nor ICollection. When the validated values (the property type, or
its element type for a List prompt) are strings, arrays or ICollection,
that path cannot run, so the generator now creates those attributes in
factory methods that suppress the warning with that justification.
Compare and Range(Type, string, string) really use reflection and keep
their warning.

- The Native AOT verification model now uses [MinLength], [MaxLength]
  and [Length], so CI catches this; the previous generator fails it.
- The example serializes with a source-generated JsonSerializerContext
  instead of reflection-based JsonSerializer, and sets PublishAot, so
  dotnet publish -r <RID> produces a warning-free Native AOT build.
- Docs (en/ja) explain how validation attributes behave under trimming,
  including the IL2026 that .NET reports where [MinLength] is applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 21, 2026 09:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The safe-type check excludes non-generic ICollection itself, leaving avoidable IL2026 warnings in generated binders.

1 open issue
What changed in this PR

Makes generated length validators trim-safe for supported types and prepares the example app for Native AOT, extending the packaged-library verification from #425.

Changes:

  • Adds targeted trimming suppressions to generated length-validator factories.
  • Expands generator and Native AOT validation coverage.
  • Enables AOT publishing, source-generated JSON serialization, and bilingual guidance.
File Description
tests/​Sharprompt.SourceGenerator.Tests/​PromptBindableGeneratorTests.cs Tests suppressed and preserved trimming warnings.
tests/​Sharprompt.NativeAot/​Sharprompt.NativeAot.App/​Program.cs Checks minimum-length rejection.
tests/​Sharprompt.NativeAot/​Sharprompt.NativeAot.App/​Models.cs Adds length-validation scenarios.
src/​Sharprompt.SourceGenerator/​PromptBindableGenerator.cs Generates factories with targeted trimming suppressions.
samples/​Sharprompt.Example/​Sharprompt.Example.csproj Enables Native AOT publishing.
samples/​Sharprompt.Example/​Program.cs Uses generated JSON metadata.
samples/​Sharprompt.Example/​Models/​MyFormModel.cs Suppresses the attribute-site trimming warning.
samples/​Sharprompt.Example/​Models/​ExampleJsonContext.cs Adds source-generated JSON serialization metadata.
docs/​ja/​advanced.md Documents validation under trimming in Japanese.
docs/​en/​advanced.md Documents validation under trimming in English.

Review effort: Balanced

AllInterfaces does not include the type itself, so a validated type of
System.Collections.ICollection (for example the elements of a
List<ICollection> property) was not recognized, and its length validator
kept IL2026 although the value always takes the ICollection path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 21, 2026 09:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Static review found no unresolved issues in the targeted suppression, AOT example, or regression coverage; builds and tests were not independently run.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shibayan
shibayan merged commit 5e17ace into master Oct 21, 2026
10 checks passed
@shibayan
shibayan deleted the aot-length-validators branch October 21, 2026 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants