Repository navigation
Make length validators trim safe in generated binders and the example AOT ready - #432
Merged
Merged
Conversation
… 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>
Contributor
There was a problem hiding this comment.
🟡 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
I published the example app with Native AOT against the packed library to check AOT locally. That turned up
IL2026in the binder that[PromptBindable]generates, which the CI verification from #425 did not catch, because its model used no length attributes:The example's
Bindsample would also have thrown at the end under AOT, because it printed the model with reflection-basedJsonSerializer.Serialize.Which validation attribute constructors are
[RequiresUnreferencedCode](.NET 10)MinLength(int),MaxLength(int),MaxLength(),Length(int, int)IsValidreadsCountthrough reflection only for values that are neitherstringnorICollectionstring, an array, or implementsICollectionCompare(string)Range(Type, string, string)Changes
PromptBindableGenerator: a length attribute qualifies when the validated values (the property type, or its element type for aListprompt) arestring, arrays, or implement non-genericICollection. The binder then creates the attribute in aCreateLengthValidatorN()factory method that carriesUnconditionalSuppressMessage("Trimming", "IL2026")with that justification. Everything else is emitted as before.HashSet<T>is notICollection, so it is deliberately not covered.SignupModelnow uses[MinLength],[MaxLength], and[Length](including on aList<string>property). A new step checks that[MinLength]rejects input. With the previous generator, the AOT publish fails on the generated binder.ExampleJsonContext.PublishAot, sodotnet publish samples/Sharprompt.Example -c Release -r <RID>produces a Native AOT build with no warnings.advanced.mddescribes how each attribute behaves under trimming.Note for reviewers
The trim analyzer also reports
IL2026on 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:string,string?, a password,List<string>, andstring[][]generate code with no trim analyzer diagnostics.[MinLength]on a type that only exposesCount,[Compare], and[Range(Type, ...)]still reportIL2026in generated code.tests/Sharprompt.NativeAot/verify.ps1on win-x64: 65/65 for net8.0 and net10.0, with no warnings. Run against the previous generator, the publish fails.dotnet publish -r win-x64produces a 4.3 MB native executable with no warnings.dotnet build,dotnet test(1352 passed), anddotnet format --verify-no-changes.🤖 Generated with Claude Code