From 5f0b5374b8a2821a425475a74211a2b73ebb24c9 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:12:54 -0700 Subject: [PATCH 1/8] Remove uriSyntaxException from executers --- .../Writers/Java/CodeMethodWriter.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index 0a771da7ed..fecfd122c5 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -458,13 +458,10 @@ private void WriteDeserializerBodyForInheritedModel(CodeMethod method, CodeClass writer.WriteLine($"return {DeserializerVarName};"); } private const string FactoryMethodName = "createFromDiscriminatorValue"; - private const string ExecuterExceptionVar = "executionException"; private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer, string signatureReturnType) { if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null"); var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement, false); - writer.WriteLine("try {"); - writer.IncreaseIndent(); WriteGeneratorMethodCall(codeElement, requestParams, parentClass, writer, $"final RequestInformation {RequestInfoVarName} = "); var sendMethodName = GetSendRequestMethodName(codeElement.ReturnType.IsCollection, returnType, codeElement.ReturnType.AllTypes.First().TypeDefinition is CodeEnum); var errorMappingVarName = "null"; @@ -479,12 +476,6 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ } var factoryParameter = codeElement.ReturnType is CodeType returnCodeType && returnCodeType.TypeDefinition is CodeClass ? $"{returnType}::{FactoryMethodName}" : $"{returnType}.class"; writer.WriteLine($"return this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {factoryParameter}, {errorMappingVarName});"); - writer.DecreaseIndent(); - writer.StartBlock("} catch (URISyntaxException ex) {"); - writer.WriteLine($"final java.util.concurrent.CompletableFuture<{signatureReturnType}> {ExecuterExceptionVar} = new java.util.concurrent.CompletableFuture<{signatureReturnType}>();"); - writer.WriteLine($"{ExecuterExceptionVar}.completeExceptionally(ex);"); - writer.WriteLine($"return {ExecuterExceptionVar};"); - writer.CloseBlock(); } private string GetSendRequestMethodName(bool isCollection, string returnType, bool isEnum) { @@ -670,18 +661,13 @@ private string WriteMethodPrototype(CodeMethod code, LanguageWriter writer, stri _ => code.Name.ToFirstCharacterLowerCase() }; var parameters = string.Join(", ", code.Parameters.OrderBy(static x => x, parameterOrderComparer).Select(p => conventions.GetParameterSignature(p, code))); - var throwableDeclarations = code.Kind switch - { - CodeMethodKind.RequestGenerator => "throws URISyntaxException ", - _ => string.Empty - }; var collectionCorrectedReturnType = code.ReturnType.IsArray && code.IsOfKind(CodeMethodKind.RequestExecutor) ? $"Iterable<{returnType.StripArraySuffix()}>" : returnType; var finalReturnType = isConstructor ? string.Empty : $" {returnTypeAsyncPrefix}{collectionCorrectedReturnType}{returnTypeAsyncSuffix}"; var staticModifier = code.IsStatic ? " static" : string.Empty; conventions.WriteDeprecatedAnnotation(code, writer); - writer.WriteLine($"{accessModifier}{staticModifier}{finalReturnType} {methodName}({parameters}) {throwableDeclarations}{{"); + writer.WriteLine($"{accessModifier}{staticModifier}{finalReturnType} {methodName}({parameters}) {{"); return collectionCorrectedReturnType; } private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer) From 7a7c9af81aa87dd0bd092a998bf4a970383f3276 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 12:14:38 -0700 Subject: [PATCH 2/8] Remove uri syntax exception import --- src/Kiota.Builder/Refiners/JavaRefiner.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Kiota.Builder/Refiners/JavaRefiner.cs b/src/Kiota.Builder/Refiners/JavaRefiner.cs index 3b5bfb91b8..f31a883995 100644 --- a/src/Kiota.Builder/Refiners/JavaRefiner.cs +++ b/src/Kiota.Builder/Refiners/JavaRefiner.cs @@ -209,8 +209,6 @@ private static void AddEnumSetImport(CodeElement currentElement) "java.util", "HashMap"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator), AbstractionsNamespaceName, "RequestInformation", "RequestOption", "HttpMethod"), - new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator), - "java.net", "URISyntaxException"), new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator), "java.util", "Collection", "Map"), new (static x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model), From e0df94d391dfb891bfb3354b796d54c3fdbbfcd6 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 13:13:29 -0700 Subject: [PATCH 3/8] Remove annotations from constructors --- src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index fecfd122c5..fc00f9af1a 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -22,7 +22,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri codeElement.IsOfKind(CodeMethodKind.RequestExecutor) && returnType.Equals("void", StringComparison.OrdinalIgnoreCase)) returnType = "Void"; //generic type for the future - writer.WriteLine(codeElement.ReturnType.IsNullable && !codeElement.IsAsync ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull"); + if (!codeElement.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor, CodeMethodKind.RawUrlConstructor)) //Constructors don't need an annotation + writer.WriteLine(codeElement.ReturnType.IsNullable && !codeElement.IsAsync ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull"); var signatureReturnType = WriteMethodPrototype(codeElement, writer, returnType); writer.IncreaseIndent(); var inherits = parentClass.StartBlock.Inherits != null && !parentClass.IsErrorDefinition; From bb03232eb2a2d92c8ee9ef6f577aea52974f1d25 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 13:37:02 -0700 Subject: [PATCH 4/8] void returns should not appear in documentation --- src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index fc00f9af1a..a31c4dd298 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -17,7 +17,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri if (codeElement.Parent is not CodeClass parentClass) throw new InvalidOperationException("the parent of a method should be a class"); var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement); - WriteMethodDocumentation(codeElement, writer); + WriteMethodDocumentation(codeElement, writer, returnType); if (codeElement.IsAsync && codeElement.IsOfKind(CodeMethodKind.RequestExecutor) && returnType.Equals("void", StringComparison.OrdinalIgnoreCase)) @@ -671,13 +671,18 @@ private string WriteMethodPrototype(CodeMethod code, LanguageWriter writer, stri writer.WriteLine($"{accessModifier}{staticModifier}{finalReturnType} {methodName}({parameters}) {{"); return collectionCorrectedReturnType; } - private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer) + private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, string returnType) { - var returnRemark = code.IsAsync switch + // var returnRemark = code.IsAsync switch + // { + // true => $"@return a CompletableFuture of {code.ReturnType.Name}", + // false => $"@return a {code.ReturnType.Name}", + // }; + var returnRemark = returnType.Equals("void", StringComparison.OrdinalIgnoreCase) ? string.Empty : (code.IsAsync switch { true => $"@return a CompletableFuture of {code.ReturnType.Name}", false => $"@return a {code.ReturnType.Name}", - }; + }); conventions.WriteLongDescription(code, writer, code.Parameters From 642160db0f69711ae2c41edbed02262b01bc84a2 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 14:23:01 -0700 Subject: [PATCH 5/8] Refactor Javadocs and annotations --- .../Writers/Java/CodeMethodWriter.cs | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index a31c4dd298..3805c79605 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -17,14 +17,12 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri if (codeElement.Parent is not CodeClass parentClass) throw new InvalidOperationException("the parent of a method should be a class"); var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement); - WriteMethodDocumentation(codeElement, writer, returnType); if (codeElement.IsAsync && codeElement.IsOfKind(CodeMethodKind.RequestExecutor) && returnType.Equals("void", StringComparison.OrdinalIgnoreCase)) returnType = "Void"; //generic type for the future - if (!codeElement.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor, CodeMethodKind.RawUrlConstructor)) //Constructors don't need an annotation - writer.WriteLine(codeElement.ReturnType.IsNullable && !codeElement.IsAsync ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull"); - var signatureReturnType = WriteMethodPrototype(codeElement, writer, returnType); + WriteMethodDocumentation(codeElement, writer, returnType); + WriteMethodPrototype(codeElement, writer, returnType); writer.IncreaseIndent(); var inherits = parentClass.StartBlock.Inherits != null && !parentClass.IsErrorDefinition; var requestBodyParam = codeElement.Parameters.OfKind(CodeParameterKind.RequestBody); @@ -49,7 +47,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri WriteRequestGeneratorBody(codeElement, requestParams, parentClass, writer); break; case CodeMethodKind.RequestExecutor: - WriteRequestExecutorBody(codeElement, requestParams, parentClass, writer, signatureReturnType); + WriteRequestExecutorBody(codeElement, requestParams, parentClass, writer); break; case CodeMethodKind.Getter: WriteGetterBody(codeElement, writer, parentClass); @@ -459,7 +457,7 @@ private void WriteDeserializerBodyForInheritedModel(CodeMethod method, CodeClass writer.WriteLine($"return {DeserializerVarName};"); } private const string FactoryMethodName = "createFromDiscriminatorValue"; - private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer, string signatureReturnType) + private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer) { if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null"); var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement, false); @@ -673,12 +671,9 @@ private string WriteMethodPrototype(CodeMethod code, LanguageWriter writer, stri } private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, string returnType) { - // var returnRemark = code.IsAsync switch - // { - // true => $"@return a CompletableFuture of {code.ReturnType.Name}", - // false => $"@return a {code.ReturnType.Name}", - // }; - var returnRemark = returnType.Equals("void", StringComparison.OrdinalIgnoreCase) ? string.Empty : (code.IsAsync switch + var returnVoid = returnType.Equals("void", StringComparison.OrdinalIgnoreCase); + // 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}", false => $"@return a {code.ReturnType.Name}", @@ -690,7 +685,8 @@ 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 + writer.WriteLine(code.ReturnType.IsNullable && !code.IsAsync ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull"); } private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod method) { From f41d7c8fd5da531230c7df02dc9942494fc09ce9 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:14:17 -0700 Subject: [PATCH 6/8] edit tests --- .../Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs index 2c3ce459cc..27b639e45b 100644 --- a/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs @@ -653,8 +653,6 @@ public void WritesRequestExecutorBody() Assert.Contains("put(\"5XX\", Error5XX::createFromDiscriminatorValue);", result); Assert.Contains("put(\"401\", Error401::createFromDiscriminatorValue);", result); Assert.Contains("sendAsync", result); - Assert.Contains($"java.util.concurrent.CompletableFuture {ExecuterExceptionVar} = new java.util.concurrent.CompletableFuture();", result); - Assert.Contains($"{ExecuterExceptionVar}.completeExceptionally(ex);", result); AssertExtensions.CurlyBracesAreClosed(result); } [Fact] @@ -1157,7 +1155,6 @@ public void WritesRequestExecutorBodyForCollections() writer.Write(method); var result = tw.ToString(); Assert.Contains("sendCollectionAsync", result); - Assert.Contains("final java.util.concurrent.CompletableFuture> executionException = new java.util.concurrent.CompletableFuture>()", result); AssertExtensions.CurlyBracesAreClosed(result); } [Fact] From e74f9343ddef3ed704f837a6addd32ce094ae4e6 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:48:00 -0700 Subject: [PATCH 7/8] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30012401f0..3acd0f2d1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,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) +- 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) ## [1.5.1] - 2023-08-08 From 70f927d61f6c4e94bb26950dd4978e27859c233a Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 17 Aug 2023 08:11:49 -0400 Subject: [PATCH 8/8] - code linting --- src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index 3805c79605..5af2bbf8ad 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -648,7 +648,7 @@ private void WriteSerializationMethodCall(CodeProperty otherProp, CodeMethod met writer.WriteLine($"writer.{GetSerializationMethodName(otherProp.Type, method)}({serializationKey}, {dataToSerialize});"); } private static readonly BaseCodeParameterOrderComparer parameterOrderComparer = new(); - private string WriteMethodPrototype(CodeMethod code, LanguageWriter writer, string returnType) + private void WriteMethodPrototype(CodeMethod code, LanguageWriter writer, string returnType) { var accessModifier = conventions.GetAccessModifier(code.Access); var returnTypeAsyncPrefix = code.IsAsync ? "java.util.concurrent.CompletableFuture<" : string.Empty; @@ -667,7 +667,6 @@ private string WriteMethodPrototype(CodeMethod code, LanguageWriter writer, stri var staticModifier = code.IsStatic ? " static" : string.Empty; conventions.WriteDeprecatedAnnotation(code, writer); writer.WriteLine($"{accessModifier}{staticModifier}{finalReturnType} {methodName}({parameters}) {{"); - return collectionCorrectedReturnType; } private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, string returnType) {