Skip to content

Commit

Permalink
Do not override user's settings from config
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Oct 2, 2023
1 parent ab5e40e commit ea902d2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
36 changes: 23 additions & 13 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()}";
Expand Down
44 changes: 27 additions & 17 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -971,6 +973,8 @@ public void WritesRequestGeneratorBodyForScalar()
Assert.Contains("return requestInfo;", result);
Assert.Contains("async Task<double?>", result);//verify we only have one nullable marker
Assert.DoesNotContain("async Task<double??>", 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]
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit ea902d2

Please sign in to comment.