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

Closed
wants to merge 13 commits into from
Closed
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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)
- Python: Set a default value for non-nullable array fields. [#6350](https://github.com/microsoft/kiota/issues/6350)

## [1.27.0] - 2025-06-11
Expand Down Expand Up @@ -1661,4 +1662,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Initial GitHub release

9 changes: 5 additions & 4 deletions src/Kiota.Builder/Refiners/CSharpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Kiota.Builder.Extensions;

namespace Kiota.Builder.Refiners;

public class CSharpRefiner : CommonLanguageRefiner, ILanguageRefiner
{
public CSharpRefiner(GenerationConfiguration configuration) : base(configuration) { }
Expand Down Expand Up @@ -216,24 +217,24 @@ protected static void CorrectPropertyType(CodeProperty currentProperty)
currentProperty.DefaultValue = "new List<IRequestOption>()";
else if (currentProperty.IsOfKind(CodePropertyKind.Headers))
currentProperty.DefaultValue = $"new {currentProperty.Type.Name.ToFirstCharacterUpperCase()}()";
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, true, currentProperty.Type);
}
protected static void CorrectMethodType(CodeMethod currentMethod)
{
ArgumentNullException.ThrowIfNull(currentMethod);
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, true, currentMethod.Parameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.PathQueryAndHeaderParameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, true, currentMethod.PathQueryAndHeaderParameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
}
protected static void CorrectIndexerType(CodeIndexer currentIndexer)
{
ArgumentNullException.ThrowIfNull(currentIndexer);
CorrectCoreTypes(currentIndexer.Parent as CodeClass, DateTypesReplacements, currentIndexer.IndexParameter.Type);
CorrectCoreTypes(currentIndexer.Parent as CodeClass, DateTypesReplacements, true, currentIndexer.IndexParameter.Type);
}

private static readonly Dictionary<string, (string, CodeUsing?)> DateTypesReplacements = new(StringComparer.OrdinalIgnoreCase)
Expand Down
23 changes: 19 additions & 4 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Kiota.Builder.Extensions;

namespace Kiota.Builder.Refiners;

public abstract class CommonLanguageRefiner : ILanguageRefiner
{
protected static readonly char[] UnderscoreArray = new[] { '_' };
Expand Down Expand Up @@ -802,7 +803,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 @@ -881,18 +882,32 @@ protected static void AddParsableImplementsForModelClasses(CodeElement currentEl
}
CrawlTree(currentElement, c => AddParsableImplementsForModelClasses(c, className));
}
protected static void CorrectCoreTypes(CodeClass? parentClass, Dictionary<string, (string, CodeUsing?)> coreTypesReplacements, params CodeTypeBase[] types)
protected static void CorrectCoreTypes(CodeClass? currentClass, Dictionary<string, (string, CodeUsing?)> coreTypesReplacements, bool languageSupportsNestedClasses = false, params CodeTypeBase[] types)
{
ArgumentNullException.ThrowIfNull(coreTypesReplacements);
if (parentClass == null)
if (currentClass == null)
return;
foreach (var type in types.Where(x => x != null && !string.IsNullOrEmpty(x.Name) && coreTypesReplacements.ContainsKey(x.Name)))
{
var replacement = coreTypesReplacements[type.Name];
if (!string.IsNullOrEmpty(replacement.Item1))
type.Name = replacement.Item1;
if (replacement.Item2 != null)
parentClass.AddUsing((CodeUsing)replacement.Item2.Clone());
switch (currentClass.Parent)
{
case CodeClass parentClass when languageSupportsNestedClasses:
parentClass.AddUsing((CodeUsing)replacement.Item2.Clone()); //lots of languages do not support imports on nested classes
break;
case CodeInterface parentInterface when languageSupportsNestedClasses:
parentInterface.AddUsing((CodeUsing)replacement.Item2.Clone()); //lots of languages do not support imports on nested classes
break;
case CodeFile parentFile when languageSupportsNestedClasses:
parentFile.AddUsing((CodeUsing)replacement.Item2.Clone()); //lots of languages do not support imports on nested classes
break;
default:
currentClass.AddUsing((CodeUsing)replacement.Item2.Clone());
break;
}
}
}
protected static void InlineParentClasses(CodeElement currentElement, CodeElement parent)
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/DartRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


namespace Kiota.Builder.Refiners;

public class DartRefiner : CommonLanguageRefiner, ILanguageRefiner
{
private const string MultipartBodyClassName = "MultipartBody";
Expand Down Expand Up @@ -266,7 +267,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
.ToList()
.ForEach(x => x.Type.Name = x.Type.Name[1..]); // removing the "I"
}
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, types: currentMethod.Parameters
.Select(static x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
Expand Down Expand Up @@ -321,7 +322,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
currentProperty.Name = currentProperty.Name.ToFirstCharacterLowerCase();
}
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, types: currentProperty.Type);
}

private static void CorrectImplements(ProprietableBlockDeclaration block)
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Kiota.Builder.Writers.Go;

namespace Kiota.Builder.Refiners;

public class GoRefiner : CommonLanguageRefiner
{
public GoRefiner(GenerationConfiguration configuration) : base(configuration) { }
Expand Down Expand Up @@ -862,7 +863,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
if (currentMethod.Parameters.OfKind(CodeParameterKind.RawUrl) is CodeParameter codeParameter)
codeParameter.Type.IsNullable = false;
}
CorrectCoreTypes(parentClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(parentClass, DateTypesReplacements, types: currentMethod.Parameters
.Select(static x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
Expand Down Expand Up @@ -941,7 +942,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
currentProperty.Type.Name = "RequestOption";
currentProperty.Type.CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array;
}
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, types: currentProperty.Type);
}
}
/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/JavaRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Kiota.Abstractions;

namespace Kiota.Builder.Refiners;

public class JavaRefiner : CommonLanguageRefiner, ILanguageRefiner
{
public JavaRefiner(GenerationConfiguration configuration) : base(configuration) { }
Expand Down Expand Up @@ -334,7 +335,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
currentProperty.DefaultValue = "new HashMap<>()";
}
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, true, currentProperty.Type);
}
private static void CorrectImplements(ProprietableBlockDeclaration block)
{
Expand Down Expand Up @@ -370,7 +371,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
}
else if (currentMethod.IsOfKind(CodeMethodKind.Factory) && currentMethod.Parameters.OfKind(CodeParameterKind.ParseNode) is CodeParameter parseNodeParam)
parseNodeParam.Type.Name = parseNodeParam.Type.Name[1..];
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, true, currentMethod.Parameters
.Select(static x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/PhpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Kiota.Builder.Writers;

namespace Kiota.Builder.Refiners;

public class PhpRefiner : CommonLanguageRefiner
{
private static readonly CodeUsingDeclarationNameComparer usingComparer = new();
Expand Down Expand Up @@ -260,7 +261,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
currentProperty.Type.CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Complex;
currentProperty.Type.Name = "array";
}
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, types: currentProperty.Type);
}

private void CorrectMethodType(CodeMethod method)
Expand All @@ -269,7 +270,7 @@ private void CorrectMethodType(CodeMethod method)
{
method.ReturnType.Name = "array";
}
CorrectCoreTypes(method.Parent as CodeClass, DateTypesReplacements, method.Parameters
CorrectCoreTypes(method.Parent as CodeClass, DateTypesReplacements, types: method.Parameters
.Select(static x => x.Type)
.Union(new[] { method.ReturnType })
.ToArray());
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Refiners/PythonRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
{
currentProperty.Type.Name = currentProperty.Type.Name.ToFirstCharacterUpperCase();
}
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, true, currentProperty.Type);
}
private static void CorrectMethodType(CodeMethod currentMethod)
{
Expand All @@ -334,7 +334,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
urlTplParams.Documentation.DescriptionTemplate = "The raw url or the url-template parameters for the request.";
}
}
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, true, currentMethod.Parameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/RubyRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Kiota.Builder.PathSegmenters;

namespace Kiota.Builder.Refiners;

public partial class RubyRefiner : CommonLanguageRefiner, ILanguageRefiner
{
public RubyRefiner(GenerationConfiguration configuration) : base(configuration) { }
Expand Down Expand Up @@ -219,7 +220,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
{
if (currentMethod.IsOfKind(CodeMethodKind.Factory) && currentMethod.Parameters.OfKind(CodeParameterKind.ParseNode) is CodeParameter parseNodeParam)
parseNodeParam.Type.Name = parseNodeParam.Type.Name[1..];
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, types: currentMethod.Parameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
Expand Down Expand Up @@ -263,7 +264,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
currentProperty.DefaultValue = "Hash.new";
}

CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, types: currentProperty.Type);

}
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {
Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/SwiftRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Kiota.Builder.Extensions;

namespace Kiota.Builder.Refiners;

public class SwiftRefiner : CommonLanguageRefiner
{
public SwiftRefiner(GenerationConfiguration configuration) : base(configuration) { }
Expand Down Expand Up @@ -117,7 +118,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
if (currentMethod.IsOfKind(CodeMethodKind.Factory))
currentMethod.ReturnType = new CodeType { Name = "Parsable", IsNullable = false, IsExternal = true };
}
CorrectCoreTypes(parentClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(parentClass, DateTypesReplacements, types: currentMethod.Parameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
Expand Down Expand Up @@ -184,7 +185,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
}
else if (currentProperty.IsOfKind(CodePropertyKind.QueryParameter) && currentProperty.Parent is CodeClass parentClass)
currentProperty.Type.Name = $"{parentClass.Name}{currentProperty.Type.Name}";
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, types: currentProperty.Type);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using static Kiota.Builder.Writers.TypeScript.TypeScriptConventionService;

namespace Kiota.Builder.Refiners;

public class TypeScriptRefiner : CommonLanguageRefiner, ILanguageRefiner
{
public static readonly string BackingStoreEnabledKey = "backingStoreEnabled";
Expand Down Expand Up @@ -741,7 +742,7 @@ private static void CorrectPropertyType(CodeProperty currentProperty)
if (!string.IsNullOrEmpty(currentProperty.DefaultValue))
currentProperty.DefaultValue = string.Empty;
}
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, true, currentProperty.Type);
}
private static void CorrectMethodType(CodeMethod currentMethod)
{
Expand Down Expand Up @@ -784,7 +785,7 @@ private static void CorrectMethodType(CodeMethod currentMethod)
{
parameter.Type.IsNullable = false;
}
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, currentMethod.Parameters
CorrectCoreTypes(currentMethod.Parent as CodeClass, DateTypesReplacements, true, currentMethod.Parameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
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