Skip to content

emit valid C# initializers for date/time type defaults in constructor#7671

Open
jeffreybulanadi wants to merge 2 commits into
microsoft:mainfrom
jeffreybulanadi:csharp-datetime-default-values-6251
Open

emit valid C# initializers for date/time type defaults in constructor#7671
jeffreybulanadi wants to merge 2 commits into
microsoft:mainfrom
jeffreybulanadi:csharp-datetime-default-values-6251

Conversation

@jeffreybulanadi
Copy link
Copy Markdown
Contributor

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:

StartTime = "13:00:00";  // compile error: cannot assign string to Time

The fix adds a dedicated branch in WriteConstructorBody that detects the post-refiner C# type names (Time, Date, DateTimeOffset, TimeSpan) and emits the correct initializer expression:

Type Default value Generated code
Time 13:00:00 new Time(13, 0, 0)
Date 2023-04-19 new Date(2023, 4, 19)
DateTimeOffset 2023-04-19T13:00:00Z DateTimeOffset.Parse("...", null, ...RoundtripKind)
TimeSpan PT1H global::System.Xml.XmlConvert.ToTimeSpan("PT1H")

If the raw value cannot be parsed at codegen time (e.g. 24:00:00 for Time), the property initializer is silently skipped rather than generating uncompilable code.

@jeffreybulanadi jeffreybulanadi requested a review from a team as a code owner May 1, 2026 22:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 WriteConstructorBody branch to emit initializer expressions for Time, Date, DateTimeOffset, and TimeSpan defaults.
  • Adds/updates C# writer tests covering these date/time default initializers (including skipping an invalid Time value).
  • 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);
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.

Default date/time values generate invalid code

3 participants