Skip to content

Commit

Permalink
write all the path parameter variables
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Nov 18, 2024
1 parent f033a45 commit f2f1c8e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 15 deletions.
49 changes: 49 additions & 0 deletions src/Kiota.Builder/Refiners/HttpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,58 @@ private static void RemoveUnusedCodeElements(CodeElement element)
var parentNameSpace = element.GetImmediateParentOfType<CodeNamespace>();
parentNameSpace?.RemoveChildElement(element);
}
else
{
// Add path variables
AddPathParameters(element);
}
CrawlTree(element, RemoveUnusedCodeElements);
}

private static void AddPathParameters(CodeElement element)
{
// Target RequestBuilder Classes only
if (element is not CodeClass codeClass) return;

var parent = element.GetImmediateParentOfType<CodeNamespace>().Parent;
while (parent is not null)
{
var codeIndexer = parent.GetChildElements(false)
.OfType<CodeClass>()
.FirstOrDefault()?
.GetChildElements(false)
.OfType<CodeMethod>()
.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.IndexerBackwardCompatibility));

if (codeIndexer is not null)
{
// Retrieve all the parameters of kind CodeParameterKind.Custom
var customParameters = codeIndexer.Parameters
.Where(param => param.IsOfKind(CodeParameterKind.Custom))
.ToList();

// For each parameter:
foreach (var param in customParameters)
{
// Create a new property of kind CodePropertyKind.PathParameters using the parameter and add it to the codeClass
var pathParameterProperty = new CodeProperty
{
Name = param.Name,
Kind = CodePropertyKind.PathParameters,
Type = param.Type,
Access = AccessModifier.Public,
DefaultValue = param.DefaultValue,
SerializationName = param.SerializationName,
Documentation = param.Documentation
};
codeClass.AddProperty(pathParameterProperty);
}
}

parent = parent.Parent?.GetImmediateParentOfType<CodeNamespace>();
}
}

private static bool IsRequestBuilderClass(CodeElement element)
{
return element is CodeClass code && code.IsOfKind(CodeClassKind.RequestBuilder);
Expand Down
43 changes: 28 additions & 15 deletions src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ protected override void WriteTypeDeclaration(ClassDeclaration codeElement, Langu
// Extract and write the URL template
WriteUrlTemplate(codeElement, writer);

// Write path parameters
WritePathParameters(codeElement, writer);

// Write all query parameter variables
WriteQueryParameters(codeElement, writer);

Expand Down Expand Up @@ -52,6 +55,20 @@ private static void WriteUrlTemplate(CodeElement codeElement, LanguageWriter wri
writer.WriteLine();
}

private static void WritePathParameters(CodeElement codeElement, LanguageWriter writer)
{
var pathParameters = codeElement.Parent?
.GetChildElements(true)
.OfType<CodeProperty>()
.Where(property => property.IsOfKind(CodePropertyKind.PathParameters))
.ToList();

pathParameters?.ForEach(prop =>
{
WriteHttpParameterProperty(prop, writer);
});
}

private static void WriteQueryParameters(CodeElement codeElement, LanguageWriter writer)
{
var queryParameterClasses = codeElement.Parent?
Expand All @@ -69,14 +86,21 @@ private static void WriteQueryParameters(CodeElement codeElement, LanguageWriter
queryParams.ForEach(prop =>
{
writer.WriteLine($"# {prop.Documentation.DescriptionTemplate}");
var decodedParameterName = DecodeUrlComponent(prop.WireName);
writer.WriteLine($"@{decodedParameterName} = ");
writer.WriteLine();
WriteHttpParameterProperty(prop, writer);
});
});
}

private static void WriteHttpParameterProperty(CodeProperty property, LanguageWriter writer)
{
if (!string.IsNullOrEmpty(property.Name))
{
writer.WriteLine($"# {property.Documentation.DescriptionTemplate}");
writer.WriteLine($"@{property.Name} = ");
writer.WriteLine();
}
}

private static void WriteHttpMethods(CodeElement codeElement, LanguageWriter writer)
{
var httpMethods = codeElement.Parent?
Expand Down Expand Up @@ -180,15 +204,4 @@ private static string GetDefaultValueForProperty(CodeProperty prop)
_ => "null"
};
}

/// <summary>
/// Decodes a URL string component, replacing percent-encoded characters with their decoded equivalents.
/// </summary>
/// <param name="urlComponent">The URL string component to decode.</param>
/// <returns>The decoded URL string component.</returns>
private static string DecodeUrlComponent(string urlComponent)
{
return HttpUtility.UrlDecode(urlComponent);
}

}

0 comments on commit f2f1c8e

Please sign in to comment.