[wip] add naming normalization for dates - #11664
[wip] add naming normalization for dates#11664Jorge Rangel (jorgerangel-msft) wants to merge 4 commits into
Conversation
commit: |
|
No changes needing a change description found. |
There was a problem hiding this comment.
Pull request overview
This PR introduces date/time naming normalization in the C# generator so *Time/*Date/*At/*Timestamp/*DateTime-suffixed date-like shapes are surfaced with an On suffix (and some noun adjustments like Creation* -> CreatedOn), while preserving wire names.
Changes:
- Added
NormalizeDateTimeSuffix(name, inputType)and applied it during property and method-parameter naming (when notIsExactName). - Updated generated sample output to rename
CreatedAt→CreatedOnwhile keeping the XML element namecreatedAt. - Added/updated unit tests and golden test data to validate the new naming behavior.
Reviewed changes
Copilot reviewed 9 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecModelFactory.cs | Updates factory method parameter naming to createdOn to match new normalization. |
| packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/XmlAdvancedModel.Serialization.cs | Switches serialization/deserialization to use CreatedOn while keeping wire element name createdAt. |
| packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/XmlAdvancedModel.cs | Renames model property/ctor parameter from CreatedAt to CreatedOn. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs | Adds coverage for property name normalization for date/time suffix patterns. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ParameterProviderTests.cs | Adds coverage for method parameter name normalization for date/time suffix patterns. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Shared/CSharpNameExtensions.cs | Implements the new NormalizeDateTimeSuffix and date/time type detection. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs | Applies date/time suffix normalization before acronym normalization (when allowed). |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ParameterProvider.cs | Applies date/time suffix normalization to non-exact method parameters. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/XmlSerializationTests.cs | Updates assertions to reflect normalized property naming in generated XML serialization. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/XmlDeserializationTests.cs | Updates assertions to reflect normalized property naming in generated XML deserialization. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/XmlSerializationTests/XmlSerializationHandlesNullableDateTimeOffsetProperty.cs | Updates expected generated output for nullable DateTimeOffset serialization to use On. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/ModelReaderWriterValidation/TestProjects/Sample_TypeSpec/XmlAdvancedModelXmlTests.cs | Updates validation to use CreatedOn after renaming. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| var methodBody = xmlSerializationMethod!.BodyStatements!.ToDisplayString(); | ||
|
|
||
| Assert.IsTrue(methodBody.Contains("WriteStringValue") && methodBody.Contains("Timestamp"), | ||
| Assert.IsTrue(methodBody.Contains("writer.WriteStringValue(On.Value, \"O\")"), | ||
| $"DateTimeOffset property should be serialized with WriteStringValue. Actual:\n{methodBody}"); |
| var methodBody = xmlDeserializationMethod!.BodyStatements!.ToDisplayString(); | ||
|
|
||
| Assert.IsTrue(methodBody.Contains("timestamp = child.GetDateTimeOffset(\"O\")"), | ||
| Assert.IsTrue(methodBody.Contains("@on = child.GetDateTimeOffset(\"O\")"), | ||
| $"DateTimeOffset property should use child.GetDateTimeOffset(\"O\") with RFC3339 format. Actual:\n{methodBody}"); |
| Name = inputParameter is InputMethodParameter && !inputParameter.IsExactName | ||
| ? inputParameter.Name.NormalizeDateTimeSuffix(inputParameter.Type) | ||
| : inputParameter.Name; |
| private static readonly HashSet<string> _dateTimePrefixExclusions = new(StringComparer.OrdinalIgnoreCase) | ||
| { | ||
| "From", | ||
| "To" |
| public static string NormalizeDateTimeSuffix(this string name, InputType inputType) | ||
| { | ||
| if (!IsDateTimeInputType(inputType) || | ||
| HasExcludedDateTimePrefix(name) || | ||
| name.EndsWith("PointInTime", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| var suffixLength = GetDateTimeSuffixLength(name); | ||
| if (suffixLength == 0) | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| var prefix = name[..^suffixLength]; | ||
| var onSuffix = prefix.Length == 0 && char.IsLower(name[0]) ? "on" : "On"; | ||
| return prefix + onSuffix; |
There was a problem hiding this comment.
Copilot would a stringbuilder approach be more effective here or is the perf bump not worth the complexity ?
There was a problem hiding this comment.
The existing concatenation emits a single string.Concat allocation on the rename path; a StringBuilder would add complexity and typically an extra allocation for this two-part result, so it is not beneficial here.
| @@ -20,6 +22,12 @@ private static readonly (string Source, string Replacement)[] _acronymRenamingRu | |||
| ("Os", "OS") | |||
There was a problem hiding this comment.
Copilot per the issue's guidelines, shouldn't we add "date" here to be replaced by On ?
There was a problem hiding this comment.
date was already handled by GetDateTimeSuffixLength; 8bde832 adds direct unit coverage confirming it normalizes to on.
| @@ -2,8 +2,10 @@ | |||
| // Licensed under the MIT License. | |||
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
fixes: Azure/azure-sdk-for-net#61943