Support C# required keyword in source generation and reflection#139
Merged
Conversation
Contributor
Author
|
Sorry for the messy commit history -- I’ll clean it up soon. |
daefed0 to
f326801
Compare
The source generator now detects the C# 'required' modifier on properties and fields (via IPropertySymbol.IsRequired / IFieldSymbol.IsRequired) in addition to [YamlRequired] and [JsonRequired] attributes. Source generator changes: - MemberModel gains IsRequiredKeyword and NeedsObjectInitializer properties - CreateMemberModel() detects C# required modifier via Roslyn symbol APIs - Types with required-keyword members are routed through the constructor/ object-initializer code path (avoiding bare 'new T()' which causes CS9035) - Required-keyword members use 'default!' as fallback in the object initializer (dead code since required-member validation throws first) - __defaults construction includes required members in its initializer when needed for pure init-only member defaults Reflection changes: - YamlObjectConverter.IsRequired() now checks for RequiredMemberAttribute (emitted by the C# compiler for required members) on net8.0+ Tests: 35 new tests covering required set/init properties, mixed required modes, records, value types, inheritance, naming policies, and custom options in both reflection and source-generated modes.
f326801 to
e38d28a
Compare
Contributor
Author
|
Commit history rewritten. Just running the final tests before taking it out of draft. |
Contributor
Author
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
Types using the C#
requiredkeyword (C# 11+) fail to compile when registered with the source generator because the generated deserialization code emitsnew T()without setting required members, producing CS9035 errors. The reflection path also doesn't detectrequiredmembers for validation.This PR fixes both the source generator and the reflection converter to fully support C#
requiredmembers.Problem
Solution
Source Generator (
YamlSerializerContextGenerator.cs)Detect
requiredmodifier —CreateMemberModel()now checksIPropertySymbol.IsRequiredandIFieldSymbol.IsRequiredin addition to[YamlRequired]and[JsonRequired]attributes.Route through initializer path — Types with
requiredkeyword members are routed through the constructor/object-initializer code path (same asinit-only properties), avoiding the barenew T()that causes CS9035.Use
default!as dead-code fallback — Required-keyword members in the object initializer usedefault!instead of__defaults.Member, since the required-member validation will throw before that fallback is ever observed.__defaultsconstruction — When pureinit-only members coexist withrequiredkeyword members, the defaults instance includes required members in its own initializer withdefault!.Generated code (after fix):
Reflection (
YamlObjectConverter.cs)IsRequired()now checks forSystem.Runtime.CompilerServices.RequiredMemberAttributeon .NET 8+ targets, detecting the C#requiredkeyword at runtime.Test Coverage
35 new tests covering:
requiredproperties withsetandinitaccessorsrequiredkeyword +[YamlRequired]+[JsonRequired]CreateOptions()All 626 tests pass (591 existing + 35 new).