Skip to content

Commit

Permalink
Merge pull request #5221 from thom0707/netstandard20-dateonly
Browse files Browse the repository at this point in the history
fix #5201: C# refiner now fixes data types for indexers.
  • Loading branch information
andrueastman authored Aug 23, 2024
2 parents 75f03bf + 7f35197 commit ec3b3c9
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Reverts modification of responses in output openApi file when generating plugins [#4945](https://github.com/microsoft/kiota/issues/4945)
- Expand properties types with null type for Typescript. [#4993](https://github.com/microsoft/kiota-typescript/issues/1188)
- Added Collection, HashMap, Map, Objects, InputStream, BigDecimal to the list of reserved names for Java generation. [#5135](https://github.com/microsoft/kiota/issues/5135)
- C# refiner now fixes data types for indexers. [#5201](https://github.com/microsoft/kiota/issues/5201)

## [1.17.0] - 2024-08-09

Expand Down
7 changes: 6 additions & 1 deletion src/Kiota.Builder/Refiners/CSharpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
static s => s.ToPascalCase(UnderscoreArray));
DisambiguatePropertiesWithClassNames(generatedCode);
// Correct the core types after reserved names for types/properties are done to avoid collision of types e.g. renaming custom model called `DateOnly` to `Date`
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType);
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType, correctIndexer: CorrectIndexerType);
cancellationToken.ThrowIfCancellationRequested();
AddSerializationModulesImport(generatedCode);
AddParentClassToErrorClasses(
Expand Down Expand Up @@ -226,6 +226,11 @@ protected static void CorrectMethodType(CodeMethod currentMethod)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
}
protected static void CorrectIndexerType(CodeIndexer currentIndexer)
{
ArgumentNullException.ThrowIfNull(currentIndexer);
CorrectCoreTypes(currentIndexer.Parent as CodeClass, DateTypesReplacements, currentIndexer.IndexParameter.Type);
}

private static readonly Dictionary<string, (string, CodeUsing?)> DateTypesReplacements = new(StringComparer.OrdinalIgnoreCase)
{
Expand Down
7 changes: 5 additions & 2 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ protected static void CrawlTree(CodeElement currentElement, Action<CodeElement>
foreach (var childElement in currentElement.GetChildElements(innerOnly))
function.Invoke(childElement);
}
protected static void CorrectCoreType(CodeElement currentElement, Action<CodeMethod>? correctMethodType, Action<CodeProperty>? correctPropertyType, Action<ProprietableBlockDeclaration>? correctImplements = default)
protected static void CorrectCoreType(CodeElement currentElement, Action<CodeMethod>? correctMethodType, Action<CodeProperty>? correctPropertyType, Action<ProprietableBlockDeclaration>? correctImplements = default, Action<CodeIndexer>? correctIndexer = default)
{
switch (currentElement)
{
Expand All @@ -800,8 +800,11 @@ protected static void CorrectCoreType(CodeElement currentElement, Action<CodeMet
case ProprietableBlockDeclaration block:
correctImplements?.Invoke(block);
break;
case CodeIndexer indexer:
correctIndexer?.Invoke(indexer);
break;
}
CrawlTree(currentElement, x => CorrectCoreType(x, correctMethodType, correctPropertyType, correctImplements), false);
CrawlTree(currentElement, x => CorrectCoreType(x, correctMethodType, correctPropertyType, correctImplements, correctIndexer), false);
}
protected static void MakeModelPropertiesNullable(CodeElement currentElement)
{
Expand Down
147 changes: 147 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/CSharpLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,153 @@ public async Task ReplacesLocallyDefinedTimeOnlyByNativeType()
Assert.NotEmpty(model.StartBlock.Usings);
Assert.Equal("TimeOnlyObject", method.ReturnType.Name);
}

[Fact]
public async Task ReplacesIndexerDateOnlyTypeWithAbstractedDateType()
{
// Arrange
var requestBuilder = root.AddClass(new CodeClass
{
Name = "requestBuilder"
}).First();

requestBuilder.AddIndexer(new CodeIndexer
{
Name = "indexer",
IndexParameter = new CodeParameter
{
Type = new CodeType
{
Name = "DateOnly",
IsExternal = true
}
},
ReturnType = new CodeType
{
Name = "SomeType"
}
});

// Act
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);

// Assert
Assert.Equal("Date", requestBuilder.Indexer.IndexParameter.Type.Name, StringComparer.OrdinalIgnoreCase);
}

[Fact]
public async Task ReplacesIndexerTimeOnlyTypeWithAbstractedTimeType()
{
// Arrange
var requestBuilder = root.AddClass(new CodeClass
{
Name = "requestBuilder"
}).First();

requestBuilder.AddIndexer(new CodeIndexer
{
Name = "indexer",
IndexParameter = new CodeParameter
{
Type = new CodeType
{
Name = "TimeOnly",
IsExternal = true
}
},
ReturnType = new CodeType
{
Name = "SomeType"
}
});

// Act
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);

// Assert
Assert.Equal("Time", requestBuilder.Indexer.IndexParameter.Type.Name, StringComparer.OrdinalIgnoreCase);
}

[Fact]
public async Task ReplacesIndexerLocallyDefinedDateOnlyTypeWithAbstractedDateType()
{
// Arrange
var requestBuilder = root.AddClass(new CodeClass
{
Name = "requestBuilder"
}).First();

var dateOnlyModel = root.AddClass(new CodeClass
{
Name = "DateOnly",
Kind = CodeClassKind.Model
}).First();

requestBuilder.AddIndexer(new CodeIndexer
{
Name = "indexer",
IndexParameter = new CodeParameter
{
Type = new CodeType
{
Name = "DateOnly",
IsExternal = false,
TypeDefinition = dateOnlyModel
}
},
ReturnType = new CodeType
{
Name = "SomeType"
}
});

// Act
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);

// Assert
Assert.Equal("DateOnlyObject", requestBuilder.Indexer.IndexParameter.Type.Name, StringComparer.OrdinalIgnoreCase);
}

[Fact]
public async Task ReplacesIndexerLocallyDefinedTimeOnlyTypeWithAbstractedTimeType()
{
// Arrange
var requestBuilder = root.AddClass(new CodeClass
{
Name = "requestBuilder"
}).First();

var timeOnlyModel = root.AddClass(new CodeClass
{
Name = "TimeOnly",
Kind = CodeClassKind.Model
}).First();

requestBuilder.AddIndexer(new CodeIndexer
{
Name = "indexer",
IndexParameter = new CodeParameter
{
Type = new CodeType
{
Name = "TimeOnly",
IsExternal = false,
TypeDefinition = timeOnlyModel
}
},
ReturnType = new CodeType
{
Name = "SomeType"
}
});

// Act
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);

// Assert
Assert.Equal("TimeOnlyObject", requestBuilder.Indexer.IndexParameter.Type.Name, StringComparer.OrdinalIgnoreCase);
}

[Fact]
public async Task AddsUsingForUntypedNode()
{
Expand Down

0 comments on commit ec3b3c9

Please sign in to comment.