Skip to content

Commit

Permalink
Fix circular reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Evans Aboge (from Dev Box) committed Jan 14, 2025
1 parent 2daae83 commit a99c4a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
13 changes: 8 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,17 @@
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": [
"generate",
"--openapi", "C:\\Users\\evansaboge\\Documents\\RestClient\\openapi.yaml",
"--language", "http",
"--output", "C:\\Users\\evansaboge\\Documents\\RestClient\\HTTP1"
"generate",
"--openapi",
"https://raw.githubusercontent.com/microsoftgraph/msgraph-sdk-powershell/dev/openApiDocs/v1.0/Mail.yml",
"--language",
"http",
"--output",
"${workspaceFolder}/samples/msgraph-mail/http",
],
"cwd": "${workspaceFolder}/src/kiota",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
}
28 changes: 23 additions & 5 deletions src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,34 @@ private static void WriteRequestBody(CodeMethod method, LanguageWriter writer)
/// </summary>
/// <param name="requestBodyClass">The request body class containing the properties.</param>
/// <param name="writer">The language writer to write the properties to.</param>
private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter writer)
private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter writer, HashSet<CodeClass>? processedClasses = null, int depth = 0)
{

if (processedClasses == null)
{
processedClasses = new HashSet<CodeClass>();
}

// Add the current class to the set of processed classes
if (!processedClasses.Add(requestBodyClass))
{
// If the class is already processed, write its properties again up to a certain depth
if (depth >= 3)
{
return;
}
}

var properties = requestBodyClass.Properties
.Where(static prop => prop.IsOfKind(CodePropertyKind.Custom))
.ToArray();

var propertyCount = properties.Length;
var currentIndex = 0;

foreach (var prop in properties)
{
// Add a trailing comma if there are more properties to be written
var separator = currentIndex < propertyCount - 1 ? "," : string.Empty;
var separator = ",";
var propName = $"\"{prop.Name}\"";
writer.Write($"{propName}: ");

Expand All @@ -308,7 +323,7 @@ private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter w
// If the property is an object, write a JSON representation recursively
writer.WriteLine("{", includeIndent: false);
writer.IncreaseIndent();
WriteProperties(propClass, writer);
WriteProperties(propClass, writer, processedClasses, depth + 1);
writer.CloseBlock($"}}{separator}");
}
else
Expand All @@ -317,10 +332,13 @@ private static void WriteProperties(CodeClass requestBodyClass, LanguageWriter w
}
}

// Remove the current class from the set of processed classes after processing
processedClasses.Remove(requestBodyClass);

// If the class extends another class, write properties of the base class
if (requestBodyClass.StartBlock.Inherits?.TypeDefinition is CodeClass baseClass)
{
WriteProperties(baseClass, writer);
WriteProperties(baseClass, writer, processedClasses, depth + 1);
}
}

Expand Down

0 comments on commit a99c4a8

Please sign in to comment.