From a5c4097ef37c9ea10ac18387e3b96b31b67678b5 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Mon, 21 Sep 2026 15:55:46 +0530 Subject: [PATCH] Fix TypeScript enum response imports for indexed endpoints --- CHANGELOG.md | 2 ++ .../Refiners/TypeScriptRefiner.cs | 2 +- ...iotaBuilderTests.TypeScriptEnumResponse.cs | 36 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/Kiota.Builder.Tests/KiotaBuilderTests.TypeScriptEnumResponse.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index be337b40be..c7ed91a7f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- TypeScript: import response enum objects for request builders reached through path parameters. [#6884](https://github.com/microsoft/kiota/issues/6884) + - Go: nullable UUID path parameters now dereference the pointer before calling `String()`, so generated request builders compile. - Ruby: a composed type without a discriminator generated `parse_node.get_child_node("")`, which raises, and composed type wrappers referenced their member classes unqualified so the constants did not resolve. Completes the composed type support added in [#8065](https://github.com/microsoft/kiota/pull/8065). [kiota-ruby#73](https://github.com/microsoft/kiota-ruby/issues/73) diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index d321b1f9da..fea284ed67 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -1419,7 +1419,7 @@ protected static void AddEnumObject(CodeElement currentElement) } protected static void AddEnumObjectUsings(CodeElement currentElement) { - if (currentElement is CodeProperty codeProperty && codeProperty.Kind is CodePropertyKind.RequestBuilder && codeProperty.Type is CodeType codeType && codeType.TypeDefinition is CodeClass codeClass) + if (currentElement is CodeClass codeClass && codeClass.Kind is CodeClassKind.RequestBuilder) { foreach (var propertyMethod in codeClass.Methods) { diff --git a/tests/Kiota.Builder.Tests/KiotaBuilderTests.TypeScriptEnumResponse.cs b/tests/Kiota.Builder.Tests/KiotaBuilderTests.TypeScriptEnumResponse.cs new file mode 100644 index 0000000000..230aa089b8 --- /dev/null +++ b/tests/Kiota.Builder.Tests/KiotaBuilderTests.TypeScriptEnumResponse.cs @@ -0,0 +1,36 @@ +using System.Threading.Tasks; +using Kiota.Builder.CodeDOM; +using Kiota.Builder.Configuration; +using Kiota.Builder.Refiners; +using Microsoft.Extensions.Logging.Abstractions; +using Xunit; + +namespace Kiota.Builder.Tests; + +public sealed partial class KiotaBuilderTests +{ + [Theory] + [InlineData("/groups", "GroupsRequestBuilder")] + [InlineData("/groups/{group}", "WithGroupItemRequestBuilder")] + public async Task ImportsEnumResponseObjectAsync(string path, string builderName) + { + var description = """ + {"openapi":"3.0.3","info":{"title":"Enum response","version":"1.0"}, + "paths":{"PATH":{"get":{"responses":{"200":{"description":"Success","content":{ + "application/json":{"schema":{"$ref":"#/components/schemas/Status"}}}}}}}}, + "components":{"schemas":{"Status":{"type":"string","enum":["active","inactive"]}}}} + """.Replace("PATH", path); + await using var stream = await GetDocumentStreamAsync(description); + var configuration = new GenerationConfiguration { Language = GenerationLanguage.TypeScript }; + var builder = new KiotaBuilder(NullLogger.Instance, configuration, _httpClient); + var document = await builder.CreateOpenApiDocumentAsync(stream, cancellationToken: TestContext.Current.CancellationToken); + var model = builder.CreateSourceModel(builder.CreateUriSpace(document)); + await ILanguageRefiner.RefineAsync(configuration, model, TestContext.Current.CancellationToken); + var requestBuilder = model.FindChildByName(builderName); + Assert.NotNull(requestBuilder); + var responseEnum = model.FindChildByName("Status"); + Assert.NotNull(responseEnum); + Assert.NotNull(responseEnum.CodeEnumObject); + Assert.Contains(requestBuilder.Usings, x => x.Declaration?.TypeDefinition == responseEnum.CodeEnumObject && !x.IsErasable); + } +}