Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request executer method call java #3161

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Flattens the models namespaces in Ruby to avoid circular dependencies.
- Adds ObjectId as a reserved keyword in Ruby to have memory management issues.
- Replace Javax annotations in favor of Jakarta annotations for Java code generation. [#2810](https://github.com/microsoft/kiota/issues/2810)
- Remove URISyntaxException from Java generated RequestExecutors and RequestGenerators. [#3149](https://github.com/microsoft/kiota/issues/3149)
- RequestExecuters call overload methods reducing code generation size for Java. [#3150](https://github.com/microsoft/kiota/issues/3150)
- Remove URISyntaxException from Java generated RequestExecutors and RequestGenerators. [#3149](https://github.com/microsoft/kiota/issues/3149)

## [1.5.1] - 2023-08-08

Expand Down
21 changes: 12 additions & 9 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
WriteIndexerBody(codeElement, parentClass, writer, returnType);
break;
case CodeMethodKind.RequestGenerator when codeElement.IsOverload:
WriteGeneratorMethodCall(codeElement, requestParams, parentClass, writer, "return ");
WriteGeneratorOrExecutorMethodCall(codeElement, requestParams, parentClass, writer, "return ", CodeMethodKind.RequestGenerator);
break;
case CodeMethodKind.RequestGenerator when !codeElement.IsOverload:
WriteRequestGeneratorBody(codeElement, requestParams, parentClass, writer);
break;
case CodeMethodKind.RequestExecutor:
case CodeMethodKind.RequestExecutor when codeElement.IsOverload:
WriteGeneratorOrExecutorMethodCall(codeElement, requestParams, parentClass, writer, "return ", CodeMethodKind.RequestExecutor);
break;
case CodeMethodKind.RequestExecutor when !codeElement.IsOverload:
WriteRequestExecutorBody(codeElement, requestParams, parentClass, writer);
break;
case CodeMethodKind.Getter:
Expand Down Expand Up @@ -461,7 +464,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ
{
if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null");
var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement, false);
WriteGeneratorMethodCall(codeElement, requestParams, parentClass, writer, $"final RequestInformation {RequestInfoVarName} = ");
WriteGeneratorOrExecutorMethodCall(codeElement, requestParams, parentClass, writer, $"final RequestInformation {RequestInfoVarName} = ", CodeMethodKind.RequestGenerator);
var sendMethodName = GetSendRequestMethodName(codeElement.ReturnType.IsCollection, returnType, codeElement.ReturnType.AllTypes.First().TypeDefinition is CodeEnum);
var errorMappingVarName = "null";
if (codeElement.ErrorMappings.Any())
Expand Down Expand Up @@ -493,11 +496,11 @@ private string GetSendRequestMethodName(bool isCollection, string returnType, bo
}
private const string RequestInfoVarName = "requestInfo";
private const string RequestConfigVarName = "requestConfig";
private static void WriteGeneratorMethodCall(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer, string prefix)
private static void WriteGeneratorOrExecutorMethodCall(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer, string prefix, CodeMethodKind codeMethodKind)
{
var generatorMethodName = parentClass
var methodName = parentClass
.Methods
.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.RequestGenerator) && x.HttpMethod == codeElement.HttpMethod)
.FirstOrDefault(x => x.IsOfKind(codeMethodKind) && x.HttpMethod == codeElement.HttpMethod)
?.Name
?.ToFirstCharacterLowerCase();
var paramsList = new[] { requestParams.requestBody, requestParams.requestConfiguration };
Expand All @@ -507,7 +510,7 @@ private static void WriteGeneratorMethodCall(CodeMethod codeElement, RequestPara
var skipIndex = requestParams.requestBody == null ? 1 : 0;
requestInfoParameters.AddRange(paramsList.Where(x => x == null).Skip(skipIndex).Select(x => "null"));
var paramsCall = requestInfoParameters.Any() ? requestInfoParameters.Aggregate((x, y) => $"{x}, {y}") : string.Empty;
writer.WriteLine($"{prefix}{generatorMethodName}({paramsCall});");
writer.WriteLine($"{prefix}{methodName}({paramsCall});");
}
private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass currentClass, LanguageWriter writer)
{
Expand Down Expand Up @@ -671,7 +674,7 @@ private void WriteMethodPrototype(CodeMethod code, LanguageWriter writer, string
private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, string returnType)
{
var returnVoid = returnType.Equals("void", StringComparison.OrdinalIgnoreCase);
// Void returns, this includes constructors, should not have a return statement in the JavaDocs.
// Void returns, this includes constructors, should not have a return statement in the JavaDocs.
var returnRemark = returnVoid ? string.Empty : (code.IsAsync switch
{
true => $"@return a CompletableFuture of {code.ReturnType.Name}",
Expand All @@ -684,7 +687,7 @@ private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, st
.OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => $"@param {x.Name} {JavaConventionService.RemoveInvalidDescriptionCharacters(x.Documentation.Description)}")
.Union(new[] { returnRemark }));
if (!returnVoid) //Nullable/Nonnull annotations for returns are a part of Method Documentation
if (!returnVoid) //Nullable/Nonnull annotations for returns are a part of Method Documentation
writer.WriteLine(code.ReturnType.IsNullable && !code.IsAsync ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull");
}
private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod method)
Expand Down
20 changes: 20 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,26 @@ public void WritesRequestExecutorBody()
AssertExtensions.CurlyBracesAreClosed(result);
}
[Fact]
public void WritesRequestExecutorOverloadBody()
{
setup();
method.Kind = CodeMethodKind.RequestExecutor;
method.HttpMethod = HttpMethod.Get;
method.OriginalMethod = method;
AddRequestBodyParameters();
writer.Write(method);
var result = tw.ToString();
Assert.DoesNotContain("final RequestInformation requestInfo", result);
Assert.DoesNotContain("final HashMap<String, ParsableFactory<? extends Parsable>> errorMapping = new HashMap<String, ParsableFactory<? extends Parsable>>", result);
Assert.DoesNotContain("put(\"4XX\", Error4XX::createFromDiscriminatorValue);", result);
Assert.DoesNotContain("put(\"5XX\", Error5XX::createFromDiscriminatorValue);", result);
Assert.DoesNotContain("put(\"401\", Error401::createFromDiscriminatorValue);", result);
Assert.DoesNotContain("sendAsync", result);
Assert.DoesNotContain($"java.util.concurrent.CompletableFuture<Somecustomtype> {ExecuterExceptionVar} = new java.util.concurrent.CompletableFuture<Somecustomtype>();", result);
Assert.DoesNotContain($"{ExecuterExceptionVar}.completeExceptionally(ex);", result);
AssertExtensions.CurlyBracesAreClosed(result);
}
[Fact]
public void DoesntCreateDictionaryOnEmptyErrorMapping()
{
setup();
Expand Down
Loading