Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug with reserved property names in dotnet #3825

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where a property named constructor would make generation fail. [#3756](https://github.com/microsoft/kiota/issues/3756)
- Removed reflection for Java enum deserialization. [microsoft/kiota-java#843](https://github.com/microsoft/kiota-java/pull/843)
- Added support for AnyOf arrays. [#3786](https://github.com/microsoft/kiota/pull/3792)
- Fixed a bug where property names that matched reserved type would be replaced in dotnet.

## [1.8.2] - 2023-11-08

Expand Down
3 changes: 1 addition & 2 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@
codeElementExceptions: new HashSet<Type> { typeof(CodeNamespace) },
shouldReplaceCallback: codeElement => codeElement is CodeClass
|| codeElement is CodeMethod
|| codeElement is CodeEnum codeEnum && provider.ReservedNames.Contains(codeEnum.Name) // only replace enum type names not enum member names
|| (codeElement is CodeProperty currentProperty && currentProperty.Type is CodeType propertyType && !propertyType.IsExternal && provider.ReservedNames.Contains(propertyType.Name)));// only replace property type names not property names
|| codeElement is CodeEnum codeEnum && provider.ReservedNames.Contains(codeEnum.Name)); // only replace enum type names not enum member names

protected static void ReplaceReservedNamespaceTypeNames(CodeElement current, IReservedNamesProvider provider, Func<string, string> replacement) =>
ReplaceReservedNames(current, provider, replacement, shouldReplaceCallback: codeElement => codeElement is CodeNamespace || codeElement is CodeClass);
Expand Down Expand Up @@ -609,9 +608,9 @@
if (currentElement is CodeIndexer currentIndexer &&
currentElement.Parent is CodeClass indexerParentClass)
{
if (indexerParentClass.ContainsMember(currentElement.Name)) // TODO remove condition for v2 necessary because of the second case of Go block

Check warning on line 611 in src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
indexerParentClass.RemoveChildElement(currentElement);
//TODO remove whole block except for last else if body for v2

Check warning on line 613 in src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
if (language == GenerationLanguage.Go)
{
if (currentIndexer.IsLegacyIndexer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,32 @@ public async Task DoesNotEscapesReservedKeywordsForClassOrPropertyKind()
Name = "break", // this a keyword
Kind = CodeClassKind.Model,
}).First();
var property = model.AddProperty(new CodeProperty
var propertyWithCsharpReservedName = model.AddProperty(new CodeProperty
{
Name = "alias",// this a keyword
Type = new CodeType
{
Name = "string"
}
}).First();
var propertyWithReservedTypeName = model.AddProperty(new CodeProperty
{
Name = "task",// this a type name reserved in C#
Type = new CodeType
{
Name = "string"
}
}).First();
// Act
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);
// Assert
Assert.Equal("break", model.Name);
Assert.DoesNotContain("@", model.Name); // classname will be capitalized
Assert.Equal("Alias", property.Name);
Assert.DoesNotContain("@", property.Name); // classname will be capitalized
Assert.Equal("Alias", propertyWithCsharpReservedName.Name);
Assert.DoesNotContain("@", propertyWithCsharpReservedName.Name); // classname will be capitalized
Assert.Equal("Task", propertyWithReservedTypeName.Name);
Assert.DoesNotContain("@", propertyWithReservedTypeName.Name); // classname will be capitalized
Assert.DoesNotContain("Escaped", propertyWithReservedTypeName.Name); // classname will be capitalized
}

[Fact]
Expand Down
Loading