Skip to content

fix: fix generation of usings of date types for when present in nested classes #6311

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixes generation of usings/imports in where Date Types are placed in a nested class. [#6286](https://github.com/microsoft/kiota/issues/6286)

## [1.24.1] - 2025-03-13

### Changed
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ currentClass.StartBlock is ClassDeclaration currentClassDeclaration &&


if (usingsToAdd.Length != 0)
(currentClass.Parent is CodeClass parentClass ? parentClass : currentClass).AddUsing(usingsToAdd); //lots of languages do not support imports on nested classes
(currentClass.Parent as CodeClass ?? currentClass).AddUsing(usingsToAdd); //lots of languages do not support imports on nested classes
}
CrawlTree(current, x => AddPropertiesAndMethodTypesImports(x, includeParentNamespaces, includeCurrentNamespace, compareOnDeclaration, codeTypeFilter));
}
Expand Down Expand Up @@ -892,7 +892,7 @@ protected static void CorrectCoreTypes(CodeClass? parentClass, Dictionary<string
if (!string.IsNullOrEmpty(replacement.Item1))
type.Name = replacement.Item1;
if (replacement.Item2 != null)
parentClass.AddUsing((CodeUsing)replacement.Item2.Clone());
(parentClass.Parent as CodeClass ?? parentClass).AddUsing((CodeUsing)replacement.Item2.Clone()); //lots of languages do not support imports on nested classes
}
}
protected static void InlineParentClasses(CodeElement currentElement, CodeElement parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,33 @@ public async Task ReplacesDateOnlyByNativeTypeAsync()
Assert.Equal("Date", method.ReturnType.Name);
}
[Fact]
public async Task ReplacesDateOnlyByNativeTypeInNestedClassAsync()
{
var model = root.AddClass(new CodeClass
{
Name = "model",
Kind = CodeClassKind.Model
}).First();
var nestedModel = model.AddInnerClass(new CodeClass
{
Name = "nestedModel",
Kind = CodeClassKind.Model
}).First();
var propertyInNestedModel = nestedModel.AddProperty(new CodeProperty
{
Name = "nestedModelProperty",
Type = new CodeType
{
Name = "DateOnly",
IsExternal = true// this is external from the Kiota abstractions
},
}).First();
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.CSharp }, root);
Assert.NotEmpty(model.StartBlock.Usings); // using is added to outer class.
Assert.Empty(nestedModel.StartBlock.Usings); // using is not added to nested model
Assert.Equal("Date", propertyInNestedModel.Type.Name);
}
[Fact]
public async Task ReplacesTimeOnlyByNativeTypeAsync()
{
var model = root.AddClass(new CodeClass
Expand Down
Loading