fix(generator): non-nullable [AsParameters] properties with defaults not required#40
Merged
Merged
Conversation
…not required
Two bugs caused non-nullable options-type properties with C# initializers
(e.g. `public LogLevel Severity { get; set; } = LogLevel.Information;`) to
be treated as required CLI flags:
1. `FromAsParametersInitProperty` computed `required` from the property type's
nullability alone, never checking for a property initializer. Fixed by
calling `TryGetOptionsPropertyDefaultLiteral` and applying `&& defaultValueLiteral is null`,
mirroring the existing global-options path in `FromOptionsProperty`.
2. `EmitParseAndAssign` called `EmitParseFromString` unconditionally when
`DefaultValueLiteral is null`, passing a null raw expression into parsers
like `Enum.TryParse` when the flag was absent. Fixed by wrapping the parse
in `if (rawExpr is not null)` for the optional-no-default case.
Tests added for all three parameter shapes: global options properties,
[AsParameters] DTO properties, and method parameters.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <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
FromAsParametersInitPropertywas computingrequiredfrom the property type's nullability alone — it never checked for a C# property initializer (= value). A non-nullable property likepublic FixtureSeverity Severity { get; set; } = FixtureSeverity.Information;was treated as a required CLI flag. Fixed by callingTryGetOptionsPropertyDefaultLiteraland applying&& defaultValueLiteral is null, mirroring the existingFromOptionsPropertypath.EmitParseAndAssigncalledEmitParseFromStringunconditionally whenDefaultValueLiteral is null, passing a null raw expression into type-specific parsers (e.g.Enum.TryParse) when an optional flag was absent. This caused a spuriousError: invalid value for --foo: ''.and causedTryParseArghto returnfalseearly — meaning subsequent flags (like--skip-private-repositories) were never applied. Fixed by wrapping the parse inif (rawExpr is not null)for the optional-no-default case.Test plan
ParseErrors/ParseErrorTests.csverify exit 0 when omitting non-nullable-with-default flags across all three shapes: global options property,[AsParameters]DTO property, method parameterBinding/AsParametersAndBindingTests.csverify the correct default values are bound (Informationenum,20int)🤖 Generated with Claude Code