Skip to content

Commit

Permalink
fix: use the right UriTemplate for requests metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
chelkyl committed Oct 21, 2024
1 parent faec08f commit 514c603
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Kiota.Builder/CodeDOM/CodeConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public CodeDocumentation Documentation
Name = $"{codeClass.Name.ToFirstCharacterLowerCase()}{RequestsMetadataSuffix}",
Kind = CodeConstantKind.RequestsMetadata,
OriginalCodeElement = codeClass,
UriTemplate = $"{codeClass.Name.ToFirstCharacterLowerCase()}{UriTemplateSuffix}",
};
result.Documentation.DescriptionTemplate = "Metadata for all the requests in the request builder.";
if (usingsToAdd is { Length: > 0 } usingsToAddList)
Expand Down
6 changes: 3 additions & 3 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ private static void MergeConflictingBuilderCodeFiles(CodeNamespace modelsNamespa
}
private static void MergeConflictingBuilderCodeFilesForElement(CodeElement currentElement)
{
if (currentElement is CodeNamespace currentNamespace && currentNamespace.Files.Count() > 1)
if (currentElement is CodeNamespace currentNamespace && currentNamespace.Files.ToArray() is { Length: > 1 } codeFiles)
{
var targetFile = currentNamespace.Files.First();
foreach (var fileToMerge in currentNamespace.Files.Skip(1))
var targetFile = codeFiles.First();
foreach (var fileToMerge in codeFiles.Skip(1))
{
if (fileToMerge.Classes.Any())
targetFile.AddElements(fileToMerge.Classes.ToArray());
Expand Down
16 changes: 14 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,20 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri
.OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase)
.ToArray() is not { Length: > 0 } executorMethods)
return;
var uriTemplateConstant = codeElement.Parent is CodeFile parentFile && parentFile.Constants.FirstOrDefault(static x => x.Kind is CodeConstantKind.UriTemplate) is CodeConstant tplct ?
tplct : throw new InvalidOperationException("Couldn't find the associated uri template constant for the requests metadata constant");
CodeConstant? uriTemplateConstant = null;
if (codeElement.Parent is CodeFile parentFile)
{
var uriTemplates = parentFile.Constants.Where(static x => x.Kind is CodeConstantKind.UriTemplate).ToArray();
var uriTemplate = uriTemplates.Length == 1
? uriTemplates.First()
: uriTemplates.FirstOrDefault(x => x.Name == codeElement.UriTemplate);
if (uriTemplate is CodeConstant tplct)
{
uriTemplateConstant = tplct;
}
}
if (uriTemplateConstant == null)
throw new InvalidOperationException("Couldn't find the associated uri template constant for the requests metadata constant");
writer.StartBlock($"export const {codeElement.Name.ToFirstCharacterUpperCase()}: RequestsMetadata = {{");
foreach (var executorMethod in executorMethods)
{
Expand Down

0 comments on commit 514c603

Please sign in to comment.