From 7195b61d6a4c901d9d7ea9a6831fa42f69e6ccad Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 18:08:36 +0000 Subject: [PATCH 1/2] [Java] Remove the usage of reflection to get enum instances test in ci clean ci jobs cleanup and changelog update with the latest encoding minor cleanup Update src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs Co-authored-by: Vincent Biret - adds changelog entry for java enum --- CHANGELOG.md | 1 + it/java/basic/pom.xml | 2 +- it/java/pom.xml | 2 +- .../Writers/Java/CodeMethodWriter.cs | 22 ++++++++++++++++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ed5db875d..04f53d878b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed a bug where serialize method will not write additional data when backingStore is enabled for java. [#3643](https://github.com/microsoft/kiota/pull/3643) - Fixed a bug where paths with parameters in the same position but different names would make generation fail. [#3756](https://github.com/microsoft/kiota/issues/3756) - Fixed a bug where a property named constructor would make generation fail. [#3756](https://github.com/microsoft/kiota/issues/3756) +- Removed reflection for Java enum deserialization. [microsoft/kiota-java#843](https://github.com/microsoft/kiota-java/pull/843) ## [1.8.2] - 2023-11-08 diff --git a/it/java/basic/pom.xml b/it/java/basic/pom.xml index 82f41bd4ba..f217e83c5f 100644 --- a/it/java/basic/pom.xml +++ b/it/java/basic/pom.xml @@ -15,7 +15,7 @@ UTF-8 UTF-8 - 0.9.0 + 0.10.0 diff --git a/it/java/pom.xml b/it/java/pom.xml index 95b65a018b..3edaaa1d7b 100644 --- a/it/java/pom.xml +++ b/it/java/pom.xml @@ -15,7 +15,7 @@ UTF-8 UTF-8 - 0.9.0 + 0.10.0 diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index 80699e2c72..21ab5a5868 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -500,9 +500,21 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ writer.WriteLine($"{errorMappingVarName}.put(\"{errorMapping.Key.ToUpperInvariant()}\", {errorMapping.Value.Name}::{FactoryMethodName});"); } } - var factoryParameter = codeElement.ReturnType is CodeType returnCodeType && returnCodeType.TypeDefinition is CodeClass ? $"{returnType}::{FactoryMethodName}" : $"{returnType}.class"; + string factoryParameter; + if (codeElement.ReturnType is CodeType returnCodeType) + { + if (returnCodeType.TypeDefinition is CodeClass) + factoryParameter = $"{returnType}::{FactoryMethodName}"; + else if (returnCodeType.TypeDefinition is CodeEnum) + factoryParameter = $"{returnType}::forValue"; + else + factoryParameter = $"{returnType}.class"; + } + else + factoryParameter = $"{returnType}.class"; + var returnPrefix = codeElement.ReturnType.Name.Equals("void", StringComparison.OrdinalIgnoreCase) ? string.Empty : "return "; - writer.WriteLine($"{returnPrefix}this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {factoryParameter}, {errorMappingVarName});"); + writer.WriteLine($"{returnPrefix}this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {errorMappingVarName}, {factoryParameter});"); } private string GetSendRequestMethodName(bool isCollection, string returnType, bool isEnum) { @@ -733,7 +745,11 @@ private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod me else return $"getCollectionOfObjectValues({propertyType.ToFirstCharacterUpperCase()}::{FactoryMethodName})"; if (currentType.TypeDefinition is CodeEnum currentEnum) - return $"getEnum{(currentEnum.Flags ? "Set" : string.Empty)}Value({propertyType.ToFirstCharacterUpperCase()}.class)"; + { + var returnType = propertyType.ToFirstCharacterUpperCase(); + return $"getEnum{(currentEnum.Flags ? "Set" : string.Empty)}Value({returnType}::forValue)"; + } + } return propertyType switch { From 13159c1b51fe09c18fb09eddcd8278053581d0f4 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 22 Nov 2023 16:51:32 +0000 Subject: [PATCH 2/2] fix last missing bits --- .../Writers/Java/CodeMethodWriter.cs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index 21ab5a5868..b1d10598d9 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -500,19 +500,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ writer.WriteLine($"{errorMappingVarName}.put(\"{errorMapping.Key.ToUpperInvariant()}\", {errorMapping.Value.Name}::{FactoryMethodName});"); } } - string factoryParameter; - if (codeElement.ReturnType is CodeType returnCodeType) - { - if (returnCodeType.TypeDefinition is CodeClass) - factoryParameter = $"{returnType}::{FactoryMethodName}"; - else if (returnCodeType.TypeDefinition is CodeEnum) - factoryParameter = $"{returnType}::forValue"; - else - factoryParameter = $"{returnType}.class"; - } - else - factoryParameter = $"{returnType}.class"; - + var factoryParameter = GetSendRequestFactoryParam(returnType, codeElement.ReturnType.AllTypes.First().TypeDefinition is CodeEnum); var returnPrefix = codeElement.ReturnType.Name.Equals("void", StringComparison.OrdinalIgnoreCase) ? string.Empty : "return "; writer.WriteLine($"{returnPrefix}this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {errorMappingVarName}, {factoryParameter});"); } @@ -531,6 +519,16 @@ private string GetSendRequestMethodName(bool isCollection, string returnType, bo else if (isCollection) return "sendCollection"; return "send"; } + private string GetSendRequestFactoryParam(string returnType, bool isEnum) + { + if (conventions.PrimitiveTypes.Contains(returnType)) + return $"{returnType}.class"; + else if (isEnum) + return $"{returnType}::forValue"; + else + return $"{returnType}::{FactoryMethodName}"; + } + private const string RequestInfoVarName = "requestInfo"; private static void WriteGeneratorOrExecutorMethodCall(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer, string prefix, CodeMethodKind codeMethodKind) { @@ -741,7 +739,7 @@ private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod me if (currentType.TypeDefinition == null) return $"getCollectionOfPrimitiveValues({propertyType.ToFirstCharacterUpperCase()}.class)"; else if (currentType.TypeDefinition is CodeEnum enumType) - return $"getCollectionOfEnumValues({enumType.Name}.class)"; + return $"getCollectionOfEnumValues({enumType.Name}::forValue)"; else return $"getCollectionOfObjectValues({propertyType.ToFirstCharacterUpperCase()}::{FactoryMethodName})"; if (currentType.TypeDefinition is CodeEnum currentEnum)