diff --git a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs index dd95265e3c..dfc55aed52 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs @@ -418,19 +418,6 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req writer.WriteLines($"UrlTemplate = {GetPropertyCall(urlTemplateProperty, "string.Empty")},", $"PathParameters = {GetPropertyCall(urlTemplateParamsProperty, "string.Empty")},"); writer.CloseBlock("};"); - if (codeElement.AcceptedResponseTypes.Any()) - writer.WriteLine($"{RequestInfoVarName}.Headers.Add(\"Accept\", \"{string.Join(", ", codeElement.AcceptedResponseTypes)}\");"); - if (requestParams.requestBody != null) - { - var suffix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty; - if (requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase)) - writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({requestParams.requestBody.Name});"); - else if (currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty) - if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase))) - writer.WriteLine($"{RequestInfoVarName}.SetContentFromParsable({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});"); - else - writer.WriteLine($"{RequestInfoVarName}.SetContentFromScalar{suffix}({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});"); - } if (requestParams.requestConfiguration != null) { @@ -445,6 +432,29 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req $"{RequestInfoVarName}.AddHeaders({RequestConfigVarName}.Headers);"); writer.CloseBlock(); } + + if (codeElement.AcceptedResponseTypes.Any()) + { + writer.WriteLine($"if (!{RequestInfoVarName}.Headers.ContainsKey(\"Accept\")) {{"); + writer.IncreaseIndent(); + writer.WriteLine($"{RequestInfoVarName}.Headers.Add(\"Accept\", \"{string.Join(", ", codeElement.AcceptedResponseTypes)}\");"); + writer.CloseBlock(); + } + if (requestParams.requestBody != null) + { + writer.WriteLine($"if (!{RequestInfoVarName}.Headers.ContainsKey(\"Content-Type\")) {{"); + writer.IncreaseIndent(); + var suffix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty; + if (requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase)) + writer.WriteLine($"{RequestInfoVarName}.SetStreamContent({requestParams.requestBody.Name});"); + else if (currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty) + if (requestParams.requestBody.Type is CodeType bodyType && (bodyType.TypeDefinition is CodeClass || bodyType.Name.Equals("MultipartBody", StringComparison.OrdinalIgnoreCase))) + writer.WriteLine($"{RequestInfoVarName}.SetContentFromParsable({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});"); + else + writer.WriteLine($"{RequestInfoVarName}.SetContentFromScalar{suffix}({requestAdapterProperty.Name.ToFirstCharacterUpperCase()}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name});"); + writer.CloseBlock(); + } + writer.WriteLine($"return {RequestInfoVarName};"); } private static string GetPropertyCall(CodeProperty property, string defaultValue) => property == null ? defaultValue : $"{property.Name.ToFirstCharacterUpperCase()}"; diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index b26c9513ff..3efeb6dc93 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -542,16 +542,43 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req writer.WriteLine($"final RequestInformation {RequestInfoVarName} = new RequestInformation();"); writer.WriteLine($"{RequestInfoVarName}.httpMethod = HttpMethod.{codeElement.HttpMethod.ToString()?.ToUpperInvariant()};"); + + if (requestParams.requestConfiguration != null) + { + writer.WriteLine($"if ({requestParams.requestConfiguration.Name} != null) {{"); + writer.IncreaseIndent(); + var requestConfigTypeName = requestParams.requestConfiguration.Type.Name; + writer.WriteLines($"final {requestConfigTypeName} {RequestConfigVarName} = new {requestConfigTypeName}();", + $"{requestParams.requestConfiguration.Name}.accept({RequestConfigVarName});"); + var queryString = requestParams.QueryParameters; + if (queryString != null) + { + var queryStringName = $"{RequestConfigVarName}.{queryString.Name}"; + writer.WriteLine($"{RequestInfoVarName}.addQueryParameters({queryStringName});"); + } + writer.WriteLines($"{RequestInfoVarName}.headers.putAll({RequestConfigVarName}.headers);", + $"{RequestInfoVarName}.addRequestOptions({RequestConfigVarName}.options);"); + + writer.CloseBlock(); + } + if (currentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty urlTemplateParamsProperty && currentClass.GetPropertyOfKind(CodePropertyKind.UrlTemplate) is CodeProperty urlTemplateProperty) writer.WriteLines($"{RequestInfoVarName}.urlTemplate = {GetPropertyCall(urlTemplateProperty, "\"\"")};", $"{RequestInfoVarName}.pathParameters = {GetPropertyCall(urlTemplateParamsProperty, "null")};"); if (codeElement.AcceptedResponseTypes.Any()) + { + writer.WriteLine($"if (!{RequestInfoVarName}.headers.containsKey(\"Accept\")) {{"); + writer.IncreaseIndent(); writer.WriteLine($"{RequestInfoVarName}.headers.add(\"Accept\", \"{string.Join(", ", codeElement.AcceptedResponseTypes)}\");"); + writer.CloseBlock(); + } if (requestParams.requestBody != null && currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is CodeProperty requestAdapterProperty) { + writer.WriteLine($"if (!{RequestInfoVarName}.headers.containsKey(\"Content-Type\")) {{"); + writer.IncreaseIndent(); var toArrayPostfix = requestParams.requestBody.Type.IsCollection ? $".toArray(new {requestParams.requestBody.Type.Name}[0])" : string.Empty; var collectionPostfix = requestParams.requestBody.Type.IsCollection ? "Collection" : string.Empty; if (requestParams.requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase)) @@ -560,23 +587,6 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req writer.WriteLine($"{RequestInfoVarName}.setContentFromParsable({requestAdapterProperty.Name}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name}{toArrayPostfix});"); else writer.WriteLine($"{RequestInfoVarName}.setContentFromScalar{collectionPostfix}({requestAdapterProperty.Name}, \"{codeElement.RequestBodyContentType}\", {requestParams.requestBody.Name}{toArrayPostfix});"); - } - if (requestParams.requestConfiguration != null) - { - writer.WriteLine($"if ({requestParams.requestConfiguration.Name} != null) {{"); - writer.IncreaseIndent(); - var requestConfigTypeName = requestParams.requestConfiguration.Type.Name; - writer.WriteLines($"final {requestConfigTypeName} {RequestConfigVarName} = new {requestConfigTypeName}();", - $"{requestParams.requestConfiguration.Name}.accept({RequestConfigVarName});"); - var queryString = requestParams.QueryParameters; - if (queryString != null) - { - var queryStringName = $"{RequestConfigVarName}.{queryString.Name}"; - writer.WriteLine($"{RequestInfoVarName}.addQueryParameters({queryStringName});"); - } - writer.WriteLines($"{RequestInfoVarName}.headers.putAll({RequestConfigVarName}.headers);", - $"{RequestInfoVarName}.addRequestOptions({RequestConfigVarName}.options);"); - writer.CloseBlock(); } diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs index ec7a15c3bd..f6ffc1b17b 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs @@ -942,6 +942,8 @@ public void WritesRequestGeneratorBodyForNullableScalar() Assert.Contains("requestInfo.AddRequestOptions(requestConfig.Options)", result); Assert.Contains("SetContentFromScalar", result); Assert.Contains("return requestInfo;", result); + Assert.Contains("if (!requestInfo.Headers.ContainsKey(\"Accept\"))", result); + Assert.Contains("if (!requestInfo.Headers.ContainsKey(\"Content-Type\"))", result); AssertExtensions.CurlyBracesAreClosed(result, 1); } [Fact] @@ -971,6 +973,8 @@ public void WritesRequestGeneratorBodyForScalar() Assert.Contains("return requestInfo;", result); Assert.Contains("async Task", result);//verify we only have one nullable marker Assert.DoesNotContain("async Task", result);//verify we only have one nullable marker + Assert.Contains("if (!requestInfo.Headers.ContainsKey(\"Accept\"))", result); + Assert.Contains("if (!requestInfo.Headers.ContainsKey(\"Content-Type\"))", result); AssertExtensions.CurlyBracesAreClosed(result, 1); } [Fact] @@ -997,6 +1001,8 @@ public void WritesRequestGeneratorBodyForParsable() Assert.Contains("requestInfo.AddRequestOptions(requestConfig.Options)", result); Assert.Contains("SetContentFromParsable", result); Assert.Contains("return requestInfo;", result); + Assert.Contains("if (!requestInfo.Headers.ContainsKey(\"Accept\"))", result); + Assert.Contains("if (!requestInfo.Headers.ContainsKey(\"Content-Type\"))", result); AssertExtensions.CurlyBracesAreClosed(result, 1); } [Fact] diff --git a/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs index 01078f7390..3318ef892b 100644 --- a/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs @@ -1201,6 +1201,8 @@ public void WritesRequestGeneratorBodyForScalar() Assert.Contains("addRequestOptions", result); Assert.Contains("setContentFromScalar", result); Assert.Contains("return requestInfo;", result); + Assert.Contains("if (!requestInfo.headers.containsKey(\"Accept\"))", result); + Assert.Contains("if (!requestInfo.headers.containsKey(\"Content-Type\"))", result); AssertExtensions.CurlyBracesAreClosed(result); } [Fact] @@ -1230,6 +1232,8 @@ public void WritesRequestGeneratorBodyForScalarCollection() Assert.Contains("setContentFromScalarCollection", result); Assert.Contains("toArray", result); Assert.Contains("return requestInfo;", result); + Assert.Contains("if (!requestInfo.headers.containsKey(\"Accept\"))", result); + Assert.Contains("if (!requestInfo.headers.containsKey(\"Content-Type\"))", result); AssertExtensions.CurlyBracesAreClosed(result); } [Fact] @@ -1256,6 +1260,8 @@ public void WritesRequestGeneratorBodyForParsable() Assert.Contains("addRequestOptions", result); Assert.Contains("setContentFromParsable", result); Assert.Contains("return requestInfo;", result); + Assert.Contains("if (!requestInfo.headers.containsKey(\"Accept\"))", result); + Assert.Contains("if (!requestInfo.headers.containsKey(\"Content-Type\"))", result); AssertExtensions.CurlyBracesAreClosed(result); } [Fact]