Skip to content

Commit

Permalink
Use existing method for adding imports
Browse files Browse the repository at this point in the history
  • Loading branch information
joanne-ter-maat committed Sep 26, 2024
1 parent 76a51bd commit 4eb7e54
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 27 deletions.
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/DartRefinerFromScratch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
AddConstructorsForDefaultValues(generatedCode, true);
cancellationToken.ThrowIfCancellationRequested();
AddAsyncSuffix(generatedCode);

AddDiscriminatorMappingsUsingsToParentClasses(generatedCode, "ParseNode", addUsings: true, includeParentNamespace: true);

ReplaceDefaultSerializationModules(
generatedCode,
Expand Down
26 changes: 0 additions & 26 deletions src/Kiota.Builder/Writers/Dart/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
if (codeElement.Parent is not CodeClass parentClass)
throw new InvalidOperationException($"The provided code element {codeElement.Name} doesn't have a parent of type {nameof(CodeClass)}");

addImportsForDiscriminatorTypes(codeElement);

var currentNamespace = codeElement.GetImmediateParentOfType<CodeNamespace>();

var relativeImportManager = new RelativeImportManager(
Expand Down Expand Up @@ -60,28 +58,4 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
conventions.WriteDeprecationAttribute(parentClass, writer);
writer.StartBlock($"class {codeElement.Name.ToFirstCharacterUpperCase()}{derivation}{implements} {{");
}
/// <summary>
/// Dart needs import statements for classes that are in the same folder.
/// </summary
void addImportsForDiscriminatorTypes(ClassDeclaration classDeclaration)
{

var parent = classDeclaration.Parent as CodeClass;
var methods = parent!.GetMethodsOffKind(CodeMethodKind.Factory);
var method = methods?.FirstOrDefault();
if (method != null && method.Parent is CodeElement codeElement && method.Parent is IDiscriminatorInformationHolder)
{
var discriminatorInformation = (method.Parent as IDiscriminatorInformationHolder)!.DiscriminatorInformation;
var discriminatorMappings = discriminatorInformation.DiscriminatorMappings;
foreach (var discriminatorMapping in discriminatorMappings)
{
var className = discriminatorMapping.Value.Name;
classDeclaration.AddUsings(new CodeUsing
{
Name = className,
Declaration = discriminatorMapping.Value
});
}
}
}
}

0 comments on commit 4eb7e54

Please sign in to comment.