diff --git a/src/Kiota.Builder/CodeDOM/CodeConstant.cs b/src/Kiota.Builder/CodeDOM/CodeConstant.cs index 0acc18bb32..c7321c290d 100644 --- a/src/Kiota.Builder/CodeDOM/CodeConstant.cs +++ b/src/Kiota.Builder/CodeDOM/CodeConstant.cs @@ -56,7 +56,7 @@ public string? UriTemplate ArgumentNullException.ThrowIfNull(codeClass); if (codeClass.Kind != CodeClassKind.RequestBuilder) return default; if (!(codeClass.Properties.Any(static x => x.Kind is CodePropertyKind.RequestBuilder) || - codeClass.Methods.Any(x => x.Kind is CodeMethodKind.IndexerBackwardCompatibility or CodeMethodKind.RequestBuilderWithParameters))) + codeClass.Methods.Any(static x => x.Kind is CodeMethodKind.IndexerBackwardCompatibility or CodeMethodKind.RequestBuilderWithParameters))) return default; return new CodeConstant { @@ -69,7 +69,7 @@ public string? UriTemplate { ArgumentNullException.ThrowIfNull(codeClass); if (codeClass.Kind != CodeClassKind.RequestBuilder) return default; - if (!codeClass.Methods.Any(x => x.Kind is CodeMethodKind.RequestExecutor or CodeMethodKind.RequestGenerator)) + if (!codeClass.Methods.Any(static x => x.Kind is CodeMethodKind.RequestExecutor or CodeMethodKind.RequestGenerator)) return default; return new CodeConstant { diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs index 49d29081b6..e15baa8ceb 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs @@ -292,6 +292,7 @@ public void WritesIndexer() { AddRequestProperties(); method.Kind = CodeMethodKind.IndexerBackwardCompatibility; + parentClass.Kind = CodeClassKind.RequestBuilder; method.OriginalIndexer = new() { Name = "indx", @@ -310,37 +311,53 @@ public void WritesIndexer() }, } }; - var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass); - parentClass.GetImmediateParentOfType().AddConstant(constant); - writer.Write(constant); - var result = tw.ToString(); - Assert.Contains("this.requestAdapter", result); - Assert.Contains("this.pathParameters", result); - Assert.Contains("id", result); - Assert.Contains("return new", result); - } - [Fact] - public void WritesPathParameterRequestBuilder() - { - AddRequestProperties(); - method.Kind = CodeMethodKind.RequestBuilderWithParameters; + var parentInterface = new CodeInterface + { + Name = "parentClass", + Kind = CodeInterfaceKind.RequestBuilder + }; method.AddParameter(new CodeParameter { - Name = "pathParam", - Kind = CodeParameterKind.Path, + Name = "id", Type = new CodeType { - Name = "string" - } + Name = "string", + IsNullable = true, + }, + SerializationName = "foo-id", + Kind = CodeParameterKind.Path }); - var constant = CodeConstant.FromRequestBuilderToRequestsMetadata(parentClass); - parentClass.GetImmediateParentOfType().AddConstant(constant); + var parentNS = parentClass.GetImmediateParentOfType(); + Assert.NotNull(parentNS); + var childNS = parentNS.AddNamespace($"{parentNS.Name}.childNS"); + childNS.TryAddCodeFile("foo", + new CodeConstant + { + Name = "SomecustomtypeUriTemplate", + Kind = CodeConstantKind.UriTemplate, + }, + new CodeConstant + { + Name = "SomecustomtypeNavigationMetadata", + Kind = CodeConstantKind.NavigationMetadata, + }, + new CodeConstant + { + Name = "SomecustomtypeRequestsMetadata", + Kind = CodeConstantKind.RequestsMetadata, + }); + parentInterface.AddMethod(method); + var constant = CodeConstant.FromRequestBuilderToNavigationMetadata(parentClass); + Assert.NotNull(constant); + parentNS.TryAddCodeFile("foo", constant, parentInterface); writer.Write(constant); var result = tw.ToString(); - Assert.Contains("this.requestAdapter", result); - Assert.Contains("this.pathParameters", result); - Assert.Contains("pathParam", result); - Assert.Contains("return new", result); + Assert.Contains("export const ParentClassNavigationMetadata: Record, NavigationMetadata> = {", result); + Assert.Contains("methodName", result); + Assert.Contains("uriTemplate: SomecustomtypeUriTemplate", result); + Assert.Contains("requestsMetadata: SomecustomtypeRequestsMetadata", result); + Assert.Contains("navigationMetadata: SomecustomtypeNavigationMetadata", result); + Assert.Contains("pathParametersMappings: [\"foo-id\"]", result); } private void AddRequestProperties() {