emit valid C# initializers for date/time type defaults in constructor#7671
Open
jeffreybulanadi wants to merge 2 commits into
Open
emit valid C# initializers for date/time type defaults in constructor#7671jeffreybulanadi wants to merge 2 commits into
jeffreybulanadi wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to fix C# code generation for constructor property initializers when schema default values represent date/time types, so generated code uses type-correct initializer expressions instead of invalid string assignments.
Changes:
- Adds a new
WriteConstructorBodybranch to emit initializer expressions forTime,Date,DateTimeOffset, andTimeSpandefaults. - Adds/updates C# writer tests covering these date/time default initializers (including skipping an invalid
Timevalue). - Documents the fix in
CHANGELOG.md.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs |
Adds date/time default detection and emits specialized initializer expressions in constructors. |
tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs |
Adds a test validating emitted constructor initializers for date/time defaults and skipping invalid Time. |
CHANGELOG.md |
Records the C# date/time default initializer fix and behavior for invalid defaults. |
Comment on lines
283
to
+295
| else if (propWithDefault.Type is CodeType propertyType && GetDefaultValue(defaultValue.SanitizeQuotedStringLiteral(), propertyType) is string convertedDefaultValue) | ||
| { | ||
| defaultValue = convertedDefaultValue; | ||
| } | ||
| else if (propWithDefault.Type is CodeType dateTimePropType && | ||
| DateTimeTypeNames.Contains(dateTimePropType.Name) && | ||
| defaultValue.StartsWith('"') && defaultValue.EndsWith('"')) | ||
| { | ||
| var expression = GetDateTimeDefaultValueExpression(dateTimePropType.Name, defaultValue.TrimQuotes()); | ||
| if (string.IsNullOrEmpty(expression)) | ||
| continue; | ||
| defaultValue = expression; | ||
| } |
Comment on lines
+336
to
+343
| var escapedRawValue = rawValue.Replace("\"", "\\\"", StringComparison.Ordinal); | ||
| if (typeName.Equals("DateTimeOffset", StringComparison.OrdinalIgnoreCase) && | ||
| DateTimeOffset.TryParse(rawValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind | DateTimeStyles.AssumeUniversal, out _)) | ||
| return $"DateTimeOffset.Parse(\"{escapedRawValue}\", null, global::System.Globalization.DateTimeStyles.RoundtripKind)"; | ||
|
|
||
| if (typeName.Equals("TimeSpan", StringComparison.OrdinalIgnoreCase) && | ||
| !string.IsNullOrEmpty(rawValue)) | ||
| return $"global::System.Xml.XmlConvert.ToTimeSpan(\"{escapedRawValue}\")"; |
Comment on lines
+336
to
+339
| var escapedRawValue = rawValue.Replace("\"", "\\\"", StringComparison.Ordinal); | ||
| if (typeName.Equals("DateTimeOffset", StringComparison.OrdinalIgnoreCase) && | ||
| DateTimeOffset.TryParse(rawValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind | DateTimeStyles.AssumeUniversal, out _)) | ||
| return $"DateTimeOffset.Parse(\"{escapedRawValue}\", null, global::System.Globalization.DateTimeStyles.RoundtripKind)"; |
Comment on lines
+341
to
+344
| if (typeName.Equals("TimeSpan", StringComparison.OrdinalIgnoreCase) && | ||
| !string.IsNullOrEmpty(rawValue)) | ||
| return $"global::System.Xml.XmlConvert.ToTimeSpan(\"{escapedRawValue}\")"; | ||
|
|
Comment on lines
+2100
to
+2101
| Assert.Contains("StartTime = new Time(13, 0, 0)", result); | ||
| Assert.Contains("StartDate = new Date(2023, 4, 19)", result); |
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.
Resolves #6251
When a schema property has a default value with a date/time format, the C# constructor writer was falling through to the generic string-quoting branch and emitting code like:
The fix adds a dedicated branch in
WriteConstructorBodythat detects the post-refiner C# type names (Time,Date,DateTimeOffset,TimeSpan) and emits the correct initializer expression:Time13:00:00new Time(13, 0, 0)Date2023-04-19new Date(2023, 4, 19)DateTimeOffset2023-04-19T13:00:00ZDateTimeOffset.Parse("...", null, ...RoundtripKind)TimeSpanPT1Hglobal::System.Xml.XmlConvert.ToTimeSpan("PT1H")If the raw value cannot be parsed at codegen time (e.g.
24:00:00forTime), the property initializer is silently skipped rather than generating uncompilable code.