From 6c5f3cfd8f81312df864d95fa3c8ea650a7e0933 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 18 Jan 2024 08:23:31 -0500 Subject: [PATCH] - fixes a bug where scalar error mappings would be generated even though it's not supported by the http request adapter Signed-off-by: Vincent Biret --- CHANGELOG.md | 2 ++ src/Kiota.Builder/KiotaBuilder.cs | 11 +++++------ tests/Kiota.Builder.Tests/KiotaBuilderTests.cs | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96ad1f37a3..6981a71d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed a bug where scalar error mappings would be generated even though it's not supported by the http request adapter. [#4018](https://github.com/microsoft/kiota/issues/4018) + ## [1.10.1] - 2024-01-12 ### Added diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index 7f37bc05bd..bd618c9e94 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -1283,15 +1283,14 @@ private void AddErrorMappingToExecutorMethod(OpenApiUrlTreeNode currentNode, Ope : modelsNamespace; var errorType = CreateModelDeclarations(currentNode, errorSchema, operation, parentElement, $"{errorCode}Error", response: response); if (errorType is CodeType codeType && - codeType.TypeDefinition is CodeClass codeClass && - !codeClass.IsErrorDefinition) + codeType.TypeDefinition is CodeClass codeClass) { - codeClass.IsErrorDefinition = true; + if (!codeClass.IsErrorDefinition) + codeClass.IsErrorDefinition = true; + executorMethod.AddErrorMapping(errorCode, errorType); } - if (errorType is null) - logger.LogWarning("Could not create error type for {Error} in {Operation}", errorCode, operation.OperationId); else - executorMethod.AddErrorMapping(errorCode, errorType); + logger.LogWarning("Could not create error type for {Error} in {Operation}", errorCode, operation.OperationId); } } private (CodeTypeBase?, CodeTypeBase?) GetExecutorMethodReturnType(OpenApiUrlTreeNode currentNode, OpenApiSchema? schema, OpenApiOperation operation, CodeClass parentClass, OperationType operationType) diff --git a/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs b/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs index 615da912cb..8521c78141 100644 --- a/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs +++ b/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs @@ -2037,6 +2037,19 @@ public void AddsErrorMapping() } } }, + ["402"] = new OpenApiResponse + { + Content = + { + ["application/json"] = new OpenApiMediaType + { + Schema = new OpenApiSchema + { + Type = "string" + } + } + } + }, ["401"] = new OpenApiResponse { Content = @@ -2083,6 +2096,7 @@ public void AddsErrorMapping() var keys = executorMethod.ErrorMappings.Select(x => x.Key).ToHashSet(); Assert.Contains("4XX", keys); Assert.Contains("401", keys); + Assert.DoesNotContain("402", keys); Assert.Contains("5XX", keys); var errorType401 = codeModel.FindChildByName("tasks401Error"); Assert.NotNull(errorType401);