diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs index 86be33978e..5b6d9acad2 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs @@ -80,6 +80,10 @@ private static void WriteNavigationMetadataEntry(CodeNamespace parentNamespace, if (pathParameters is { Length: > 0 }) writer.WriteLine($"pathParametersMappings: [{string.Join(", ", pathParameters)}],"); } + private static string GetErrorMappingKey(string original) => + original.Equals("4XX", StringComparison.OrdinalIgnoreCase) || original.Equals("5XX", StringComparison.OrdinalIgnoreCase) ? + $"_{original.ToUpperInvariant()}" : // to avoid emitting strings that can't be minified + original.ToUpperInvariant(); private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWriter writer) { @@ -105,9 +109,9 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri writer.StartBlock("errorMappings: {"); foreach (var errorMapping in executorMethod.ErrorMappings) { - writer.WriteLine($"\"{errorMapping.Key.ToUpperInvariant()}\": {GetFactoryMethodName(errorMapping.Value, codeElement, writer)},"); + writer.WriteLine($"{GetErrorMappingKey(errorMapping.Key)}: {GetFactoryMethodName(errorMapping.Value, codeElement, writer)} as ParsableFactory,"); } - writer.CloseBlock("} as Record>,"); + writer.CloseBlock("},"); } writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, returnTypeWithoutCollectionSymbol)}\","); if (!isVoid) diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs index d8be90451b..918f4ca128 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeConstantWriterTests.cs @@ -322,10 +322,9 @@ public void WritesRequestExecutorBody() writer.Write(constant); var result = tw.ToString(); Assert.Contains("errorMappings: {", result); - Assert.Contains("4XX\": createError4XXFromDiscriminatorValue", result); - Assert.Contains("5XX\": createError5XXFromDiscriminatorValue", result); - Assert.Contains("403\": createError403FromDiscriminatorValue", result); - Assert.Contains(" as Record>", result); + Assert.Contains("_4XX: createError4XXFromDiscriminatorValue as ParsableFactory", result); + Assert.Contains("_5XX: createError5XXFromDiscriminatorValue as ParsableFactory", result); + Assert.Contains("403: createError403FromDiscriminatorValue as ParsableFactory", result); } [Fact] public void WritesIndexer()