From cf7d24795ebe0c31ea241689de1aafb62c0cd546 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 11:49:02 +0000 Subject: [PATCH 01/10] Remove enum on reflection --- .../com/microsoft/kiota/RequestAdapter.java | 9 ++--- .../kiota/serialization/ParseNode.java | 14 ++++---- .../kiota/http/OkHttpRequestAdapter.java | 17 +++++----- .../kiota/serialization/FormParseNode.java | 34 ++++++------------- .../kiota/serialization/JsonParseNode.java | 33 ++++++------------ .../kiota/serialization/mocks/TestEntity.java | 2 +- .../kiota/serialization/TextParseNode.java | 24 ++++--------- 7 files changed, 50 insertions(+), 83 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java index 1bcd417b4..8809619c9 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java @@ -8,6 +8,7 @@ import jakarta.annotation.Nullable; import java.util.HashMap; import java.util.List; +import java.util.function.Function; /** Service responsible for translating abstract Request Info into concrete native HTTP requests. */ public interface RequestAdapter { @@ -80,27 +81,27 @@ public interface RequestAdapter { /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum value. * @param requestInfo the request info to execute. - * @param targetClass the class of the response model to deserialize the response into. + * @param forValue a function that retrieves an enum from the corresponding string. * @param errorMappings the error factories mapping to use in case of a failed request. * @param the type of the response model to deserialize the response into. * @return the deserialized primitive response model. */ @Nullable > ModelType sendEnum( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, + @Nonnull final Function forValue, @Nullable final HashMap> errorMappings); /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum collection value. * @param requestInfo the request info to execute. - * @param targetClass the class of the response model to deserialize the response into. + * @param forValue a function that retrieves an enum from the corresponding string. * @param errorMappings the error factories mapping to use in case of a failed request. * @param the type of the response model to deserialize the response into. * @return the deserialized primitive response model. */ @Nullable > List sendEnumCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, + @Nonnull final Function forValue, @Nullable final HashMap> errorMappings); /** diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java index 9100e9c44..877af8b72 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.UUID; import java.util.function.Consumer; +import java.util.function.Function; /** * Interface for a deserialization node in a parse tree. This interface provides an abstraction layer over serialization formats, libraries and implementations. @@ -110,18 +111,18 @@ public interface ParseNode { /** * Gets the Enum value of the node. * @return the Enum value of the node. - * @param targetEnum the class of the enum. + * @param forValue a function that given a string returns the corresponding enum * @param the type of the enum. */ - @Nullable > T getEnumValue(@Nonnull final Class targetEnum); + @Nullable > T getEnumValue(@Nonnull final Function forValue); /** * Gets the EnumSet value of the node. * @return the EnumSet value of the node. - * @param targetEnum the class of the enum. + * @param forValue a function that given a string returns the corresponding enum * @param the type of the enum. */ - @Nullable > EnumSet getEnumSetValue(@Nonnull final Class targetEnum); + @Nullable > EnumSet getEnumSetValue(@Nonnull final Function forValue); /** * Gets the collection of primitive values of the node. @@ -144,9 +145,10 @@ public interface ParseNode { * Gets the collection of Enum values of the node. * @return the collection of Enum values of the node. * @param the type of the enum. - * @param targetEnum the class of the enum + * @param forValue a function that given a string returns the corresponding enum */ - @Nullable > List getCollectionOfEnumValues(@Nonnull final Class targetEnum); + @Nullable > List getCollectionOfEnumValues( + @Nonnull final Function forValue); /** * Gets the model object value of the node. diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index bd77974d5..2bd8e9c81 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -38,6 +38,7 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import okhttp3.*; @@ -172,7 +173,7 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF } private static final String nullRequestInfoParameter = "parameter requestInfo cannot be null"; - private static final String nullTargetClassParameter = "parameter targetClass cannot be null"; + private static final String nullForValueParameter = "parameter forValue cannot be null"; private static final String nullFactoryParameter = "parameter factory cannot be null"; @Nullable public List sendCollection( @@ -324,7 +325,7 @@ private void closeResponse(boolean closeResponse, Response response) { @Nonnull final Class targetClass, @Nullable final HashMap> errorMappings) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); - Objects.requireNonNull(targetClass, nullTargetClassParameter); + Objects.requireNonNull(targetClass, nullForValueParameter); final Span span = startSpan(requestInfo, "sendPrimitiveAsync"); try (final Scope scope = span.makeCurrent()) { Response response = this.getHttpResponseMessage(requestInfo, span, span, null); @@ -415,10 +416,10 @@ private void closeResponse(boolean closeResponse, Response response) { @Nullable public > ModelType sendEnum( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, + @Nonnull final Function forValue, @Nullable final HashMap> errorMappings) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); - Objects.requireNonNull(targetClass, nullTargetClassParameter); + Objects.requireNonNull(forValue, nullForValueParameter); final Span span = startSpan(requestInfo, "sendEnumAsync"); try (final Scope scope = span.makeCurrent()) { Response response = this.getHttpResponseMessage(requestInfo, span, span, null); @@ -441,7 +442,7 @@ private void closeResponse(boolean closeResponse, Response response) { .setParent(Context.current().with(span)) .startSpan(); try (final Scope deserializationScope = deserializationSpan.makeCurrent()) { - final Object result = rootNode.getEnumValue(targetClass); + final Object result = rootNode.getEnumValue(forValue); setResponseType(result, span); return (ModelType) result; } finally { @@ -461,10 +462,10 @@ private void closeResponse(boolean closeResponse, Response response) { @Nullable public > List sendEnumCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, + @Nonnull final Function forValue, @Nullable final HashMap> errorMappings) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); - Objects.requireNonNull(targetClass, nullTargetClassParameter); + Objects.requireNonNull(forValue, nullForValueParameter); final Span span = startSpan(requestInfo, "sendEnumCollectionAsync"); try (final Scope scope = span.makeCurrent()) { Response response = this.getHttpResponseMessage(requestInfo, span, span, null); @@ -487,7 +488,7 @@ private void closeResponse(boolean closeResponse, Response response) { .setParent(Context.current().with(span)) .startSpan(); try (final Scope deserializationScope = deserializationSpan.makeCurrent()) { - final Object result = rootNode.getCollectionOfEnumValues(targetClass); + final Object result = rootNode.getCollectionOfEnumValues(forValue); setResponseType(result, span); return (List) result; } finally { diff --git a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java index 3f895cca1..4f66e6420 100644 --- a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java +++ b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java @@ -4,7 +4,6 @@ import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; import java.io.UnsupportedEncodingException; -import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; @@ -23,6 +22,7 @@ import java.util.Objects; import java.util.UUID; import java.util.function.Consumer; +import java.util.function.Function; /** ParseNode implementation for URI form encoded payloads */ public class FormParseNode implements ParseNode { @@ -260,8 +260,8 @@ public T next() { } @Nullable public > List getCollectionOfEnumValues( - @Nonnull final Class targetEnum) { - Objects.requireNonNull(targetEnum, "parameter targetEnum cannot be null"); + @Nonnull final Function fromValue) { + Objects.requireNonNull(fromValue, "parameter fromValue cannot be null"); final String stringValue = getStringValue(); if (stringValue == null || stringValue.isEmpty()) { return null; @@ -269,7 +269,7 @@ public T next() { final String[] array = stringValue.split(","); final ArrayList result = new ArrayList<>(); for (final String item : array) { - result.add(getEnumValueInt(item, targetEnum)); + result.add(fromValue.apply(item)); } return result; } @@ -282,41 +282,29 @@ public T next() { return item; } - @Nullable public > T getEnumValue(@Nonnull final Class targetEnum) { + @Nullable public > T getEnumValue(@Nonnull final Function forValue) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - return getEnumValueInt(rawValue, targetEnum); + return forValue.apply(rawValue); } - @SuppressWarnings("unchecked") - private > T getEnumValueInt( - @Nonnull final String rawValue, @Nonnull final Class targetEnum) { - try { - return (T) targetEnum.getMethod("forValue", String.class).invoke(null, rawValue); - } catch (SecurityException - | IllegalAccessException - | InvocationTargetException - | NoSuchMethodException ex) { - return null; - } - } - - @Nullable public > EnumSet getEnumSetValue(@Nonnull final Class targetEnum) { + @Nullable public > EnumSet getEnumSetValue( + @Nonnull final Function forValue) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - final EnumSet result = EnumSet.noneOf(targetEnum); + final List result = new ArrayList<>(); final String[] rawValues = rawValue.split(","); for (final String rawValueItem : rawValues) { - final T value = getEnumValueInt(rawValueItem, targetEnum); + final T value = forValue.apply(rawValueItem); if (value != null) { result.add(value); } } - return result; + return EnumSet.copyOf(result); } private void assignFieldValues( diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java index bc0832bda..a9c5f8fda 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java @@ -7,7 +7,6 @@ import com.microsoft.kiota.PeriodAndDuration; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; -import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalTime; @@ -184,12 +183,12 @@ private List iterateOnArray(Function fn) { } @Nullable public > List getCollectionOfEnumValues( - @Nonnull final Class targetEnum) { - Objects.requireNonNull(targetEnum, "parameter targetEnum cannot be null"); + @Nonnull final Function forValue) { + Objects.requireNonNull(forValue, "parameter targetEnum cannot be null"); if (currentNode.isJsonNull()) { return null; } else if (currentNode.isJsonArray()) { - return iterateOnArray(itemNode -> itemNode.getEnumValue(targetEnum)); + return iterateOnArray(itemNode -> itemNode.getEnumValue(forValue)); } else throw new RuntimeException("invalid state expected to have an array node"); } @@ -200,41 +199,29 @@ private List iterateOnArray(Function fn) { return item; } - @Nullable public > T getEnumValue(@Nonnull final Class targetEnum) { + @Nullable public > T getEnumValue(@Nonnull final Function forValue) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - return getEnumValueInt(rawValue, targetEnum); + return forValue.apply(rawValue); } - @SuppressWarnings("unchecked") - private > T getEnumValueInt( - @Nonnull final String rawValue, @Nonnull final Class targetEnum) { - try { - return (T) targetEnum.getMethod("forValue", String.class).invoke(null, rawValue); - } catch (NoSuchMethodException - | IllegalAccessException - | InvocationTargetException - | SecurityException ex) { - return null; - } - } - - @Nullable public > EnumSet getEnumSetValue(@Nonnull final Class targetEnum) { + @Nullable public > EnumSet getEnumSetValue( + @Nonnull final Function forValue) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - final EnumSet result = EnumSet.noneOf(targetEnum); + final List result = new ArrayList<>(); final String[] rawValues = rawValue.split(","); for (final String rawValueItem : rawValues) { - final T value = getEnumValueInt(rawValueItem, targetEnum); + final T value = forValue.apply(rawValueItem); if (value != null) { result.add(value); } } - return result; + return EnumSet.copyOf(result); } private void assignFieldValues( diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java index d5ec68c6b..786b7db3e 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java @@ -131,7 +131,7 @@ public Map> getFieldDeserializers() { put( "myEnum", (n) -> { - setMyEnum(n.getEnumValue(MyEnum.class)); + setMyEnum(n.getEnumValue(MyEnum::forValue)); }); put( "createdDateTime", diff --git a/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java b/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java index 3028234b7..757997ca8 100644 --- a/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java +++ b/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java @@ -3,7 +3,6 @@ import com.microsoft.kiota.PeriodAndDuration; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; -import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalTime; @@ -14,6 +13,7 @@ import java.util.Objects; import java.util.UUID; import java.util.function.Consumer; +import java.util.function.Function; /** ParseNode implementation for text/plain */ public class TextParseNode implements ParseNode { @@ -103,7 +103,7 @@ public TextParseNode(@Nonnull final String rawText) { } @Nullable public > List getCollectionOfEnumValues( - @Nonnull final Class targetEnum) { + @Nonnull final Function fromValue) { throw new UnsupportedOperationException(NO_STRUCTURED_DATA_MESSAGE); } @@ -111,28 +111,16 @@ public TextParseNode(@Nonnull final String rawText) { throw new UnsupportedOperationException(NO_STRUCTURED_DATA_MESSAGE); } - @Nullable public > T getEnumValue(@Nonnull final Class targetEnum) { + @Nullable public > T getEnumValue(@Nonnull final Function forValue) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - return getEnumValueInt(rawValue, targetEnum); - } - - @SuppressWarnings("unchecked") - private > T getEnumValueInt( - @Nonnull final String rawValue, @Nonnull final Class targetEnum) { - try { - return (T) targetEnum.getMethod("forValue", String.class).invoke(null, rawValue); - } catch (SecurityException - | IllegalAccessException - | InvocationTargetException - | NoSuchMethodException ex) { - return null; - } + return forValue.apply(rawValue); } - @Nullable public > EnumSet getEnumSetValue(@Nonnull final Class targetEnum) { + @Nullable public > EnumSet getEnumSetValue( + @Nonnull final Function forValue) { throw new UnsupportedOperationException(NO_STRUCTURED_DATA_MESSAGE); } From 8d4ba12f61c883d940b45c0c8be6894b851220ff Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 11:53:54 +0000 Subject: [PATCH 02/10] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21b801634..303f7c6d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added Spotless as an automatic formatting tool for the entire codebase - Changed some internal implementations of JsonParse for performance and readability reasons +- [breaking] Removed the usage of reflection for enum deserialization ## [0.9.2] - 2023-11-16 From 2a045ff7cea51ed533029d2e8f664b52e82a89ed Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 15:09:47 +0000 Subject: [PATCH 03/10] address the linting issues --- .../com/microsoft/kiota/RequestAdapter.java | 14 +++++------ .../kiota/serialization/ParseNode.java | 13 +++++------ .../kiota/serialization/ValuedEnumParser.java | 13 +++++++++++ .../kiota/http/OkHttpRequestAdapter.java | 23 ++++++++++--------- .../kiota/serialization/FormParseNode.java | 15 ++++++------ .../kiota/serialization/JsonParseNode.java | 14 +++++------ .../kiota/serialization/TextParseNode.java | 9 ++++---- gradle.properties | 4 ++-- 8 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java index 8809619c9..c98ec871d 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java @@ -3,12 +3,12 @@ import com.microsoft.kiota.serialization.Parsable; import com.microsoft.kiota.serialization.ParsableFactory; import com.microsoft.kiota.serialization.SerializationWriterFactory; +import com.microsoft.kiota.serialization.ValuedEnumParser; import com.microsoft.kiota.store.BackingStoreFactory; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; import java.util.HashMap; import java.util.List; -import java.util.function.Function; /** Service responsible for translating abstract Request Info into concrete native HTTP requests. */ public interface RequestAdapter { @@ -81,28 +81,28 @@ public interface RequestAdapter { /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum value. * @param requestInfo the request info to execute. - * @param forValue a function that retrieves an enum from the corresponding string. * @param errorMappings the error factories mapping to use in case of a failed request. + * @param enumParser a parser from string to enum instances. * @param the type of the response model to deserialize the response into. * @return the deserialized primitive response model. */ @Nullable > ModelType sendEnum( @Nonnull final RequestInformation requestInfo, - @Nonnull final Function forValue, - @Nullable final HashMap> errorMappings); + @Nullable final HashMap> errorMappings, + @Nonnull final ValuedEnumParser enumParser); /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum collection value. * @param requestInfo the request info to execute. - * @param forValue a function that retrieves an enum from the corresponding string. * @param errorMappings the error factories mapping to use in case of a failed request. + * @param enumParser a parser from string to enum instances. * @param the type of the response model to deserialize the response into. * @return the deserialized primitive response model. */ @Nullable > List sendEnumCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final Function forValue, - @Nullable final HashMap> errorMappings); + @Nullable final HashMap> errorMappings, + @Nonnull final ValuedEnumParser enumParser); /** * Sets The base url for every request. diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java index 877af8b72..916a75762 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ParseNode.java @@ -11,7 +11,6 @@ import java.util.List; import java.util.UUID; import java.util.function.Consumer; -import java.util.function.Function; /** * Interface for a deserialization node in a parse tree. This interface provides an abstraction layer over serialization formats, libraries and implementations. @@ -111,18 +110,18 @@ public interface ParseNode { /** * Gets the Enum value of the node. * @return the Enum value of the node. - * @param forValue a function that given a string returns the corresponding enum + * @param enumParser the parser for Enums * @param the type of the enum. */ - @Nullable > T getEnumValue(@Nonnull final Function forValue); + @Nullable > T getEnumValue(@Nonnull final ValuedEnumParser enumParser); /** * Gets the EnumSet value of the node. * @return the EnumSet value of the node. - * @param forValue a function that given a string returns the corresponding enum + * @param enumParser the parser for Enums * @param the type of the enum. */ - @Nullable > EnumSet getEnumSetValue(@Nonnull final Function forValue); + @Nullable > EnumSet getEnumSetValue(@Nonnull final ValuedEnumParser enumParser); /** * Gets the collection of primitive values of the node. @@ -145,10 +144,10 @@ public interface ParseNode { * Gets the collection of Enum values of the node. * @return the collection of Enum values of the node. * @param the type of the enum. - * @param forValue a function that given a string returns the corresponding enum + * @param enumParser the parser for Enums */ @Nullable > List getCollectionOfEnumValues( - @Nonnull final Function forValue); + @Nonnull final ValuedEnumParser enumParser); /** * Gets the model object value of the node. diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java new file mode 100644 index 000000000..dd96b3102 --- /dev/null +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java @@ -0,0 +1,13 @@ +package com.microsoft.kiota.serialization; + +import jakarta.annotation.Nonnull; + +/** The interface for a valued enum parser. */ +public interface ValuedEnumParser { + /** + * Gets an enum from it's string value. + * @param value the string value of the enum. + * @return the enum value derived from the string. + */ + @Nonnull T forValue(@Nonnull String value); +} diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index 2bd8e9c81..294854941 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -10,6 +10,7 @@ import com.microsoft.kiota.serialization.ParseNodeFactoryRegistry; import com.microsoft.kiota.serialization.SerializationWriterFactory; import com.microsoft.kiota.serialization.SerializationWriterFactoryRegistry; +import com.microsoft.kiota.serialization.ValuedEnumParser; import com.microsoft.kiota.store.BackingStoreFactory; import com.microsoft.kiota.store.BackingStoreFactorySingleton; import io.opentelemetry.api.GlobalOpenTelemetry; @@ -38,7 +39,6 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; -import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import okhttp3.*; @@ -173,7 +173,7 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF } private static final String nullRequestInfoParameter = "parameter requestInfo cannot be null"; - private static final String nullForValueParameter = "parameter forValue cannot be null"; + private static final String nullEnumParserParameter = "parameter enumParser cannot be null"; private static final String nullFactoryParameter = "parameter factory cannot be null"; @Nullable public List sendCollection( @@ -325,7 +325,7 @@ private void closeResponse(boolean closeResponse, Response response) { @Nonnull final Class targetClass, @Nullable final HashMap> errorMappings) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); - Objects.requireNonNull(targetClass, nullForValueParameter); + Objects.requireNonNull(targetClass, "parameter targetClass cannot be null"); final Span span = startSpan(requestInfo, "sendPrimitiveAsync"); try (final Scope scope = span.makeCurrent()) { Response response = this.getHttpResponseMessage(requestInfo, span, span, null); @@ -416,10 +416,10 @@ private void closeResponse(boolean closeResponse, Response response) { @Nullable public > ModelType sendEnum( @Nonnull final RequestInformation requestInfo, - @Nonnull final Function forValue, - @Nullable final HashMap> errorMappings) { + @Nullable final HashMap> errorMappings, + @Nonnull final ValuedEnumParser enumParser) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); - Objects.requireNonNull(forValue, nullForValueParameter); + Objects.requireNonNull(enumParser, nullEnumParserParameter); final Span span = startSpan(requestInfo, "sendEnumAsync"); try (final Scope scope = span.makeCurrent()) { Response response = this.getHttpResponseMessage(requestInfo, span, span, null); @@ -442,7 +442,7 @@ private void closeResponse(boolean closeResponse, Response response) { .setParent(Context.current().with(span)) .startSpan(); try (final Scope deserializationScope = deserializationSpan.makeCurrent()) { - final Object result = rootNode.getEnumValue(forValue); + final Object result = rootNode.getEnumValue(enumParser::forValue); setResponseType(result, span); return (ModelType) result; } finally { @@ -462,10 +462,10 @@ private void closeResponse(boolean closeResponse, Response response) { @Nullable public > List sendEnumCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final Function forValue, - @Nullable final HashMap> errorMappings) { + @Nullable final HashMap> errorMappings, + @Nonnull final ValuedEnumParser enumParser) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); - Objects.requireNonNull(forValue, nullForValueParameter); + Objects.requireNonNull(enumParser, nullEnumParserParameter); final Span span = startSpan(requestInfo, "sendEnumCollectionAsync"); try (final Scope scope = span.makeCurrent()) { Response response = this.getHttpResponseMessage(requestInfo, span, span, null); @@ -488,7 +488,8 @@ private void closeResponse(boolean closeResponse, Response response) { .setParent(Context.current().with(span)) .startSpan(); try (final Scope deserializationScope = deserializationSpan.makeCurrent()) { - final Object result = rootNode.getCollectionOfEnumValues(forValue); + final Object result = + rootNode.getCollectionOfEnumValues(enumParser::forValue); setResponseType(result, span); return (List) result; } finally { diff --git a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java index 4f66e6420..e1a568c8d 100644 --- a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java +++ b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNode.java @@ -22,7 +22,6 @@ import java.util.Objects; import java.util.UUID; import java.util.function.Consumer; -import java.util.function.Function; /** ParseNode implementation for URI form encoded payloads */ public class FormParseNode implements ParseNode { @@ -260,8 +259,8 @@ public T next() { } @Nullable public > List getCollectionOfEnumValues( - @Nonnull final Function fromValue) { - Objects.requireNonNull(fromValue, "parameter fromValue cannot be null"); + @Nonnull final ValuedEnumParser enumParser) { + Objects.requireNonNull(enumParser, "parameter enumParser cannot be null"); final String stringValue = getStringValue(); if (stringValue == null || stringValue.isEmpty()) { return null; @@ -269,7 +268,7 @@ public T next() { final String[] array = stringValue.split(","); final ArrayList result = new ArrayList<>(); for (final String item : array) { - result.add(fromValue.apply(item)); + result.add(enumParser.forValue(item)); } return result; } @@ -282,16 +281,16 @@ public T next() { return item; } - @Nullable public > T getEnumValue(@Nonnull final Function forValue) { + @Nullable public > T getEnumValue(@Nonnull final ValuedEnumParser enumParser) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - return forValue.apply(rawValue); + return enumParser.forValue(rawValue); } @Nullable public > EnumSet getEnumSetValue( - @Nonnull final Function forValue) { + @Nonnull final ValuedEnumParser enumParser) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; @@ -299,7 +298,7 @@ public T next() { final List result = new ArrayList<>(); final String[] rawValues = rawValue.split(","); for (final String rawValueItem : rawValues) { - final T value = forValue.apply(rawValueItem); + final T value = enumParser.forValue(rawValueItem); if (value != null) { result.add(value); } diff --git a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java index a9c5f8fda..dbfdd75d5 100644 --- a/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java +++ b/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java @@ -183,12 +183,12 @@ private List iterateOnArray(Function fn) { } @Nullable public > List getCollectionOfEnumValues( - @Nonnull final Function forValue) { - Objects.requireNonNull(forValue, "parameter targetEnum cannot be null"); + @Nonnull final ValuedEnumParser enumParser) { + Objects.requireNonNull(enumParser, "parameter enumParser cannot be null"); if (currentNode.isJsonNull()) { return null; } else if (currentNode.isJsonArray()) { - return iterateOnArray(itemNode -> itemNode.getEnumValue(forValue)); + return iterateOnArray(itemNode -> itemNode.getEnumValue(enumParser)); } else throw new RuntimeException("invalid state expected to have an array node"); } @@ -199,16 +199,16 @@ private List iterateOnArray(Function fn) { return item; } - @Nullable public > T getEnumValue(@Nonnull final Function forValue) { + @Nullable public > T getEnumValue(@Nonnull final ValuedEnumParser enumParser) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - return forValue.apply(rawValue); + return enumParser.forValue(rawValue); } @Nullable public > EnumSet getEnumSetValue( - @Nonnull final Function forValue) { + @Nonnull final ValuedEnumParser enumParser) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; @@ -216,7 +216,7 @@ private List iterateOnArray(Function fn) { final List result = new ArrayList<>(); final String[] rawValues = rawValue.split(","); for (final String rawValueItem : rawValues) { - final T value = forValue.apply(rawValueItem); + final T value = enumParser.forValue(rawValueItem); if (value != null) { result.add(value); } diff --git a/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java b/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java index 757997ca8..bae1911c6 100644 --- a/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java +++ b/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java @@ -13,7 +13,6 @@ import java.util.Objects; import java.util.UUID; import java.util.function.Consumer; -import java.util.function.Function; /** ParseNode implementation for text/plain */ public class TextParseNode implements ParseNode { @@ -103,7 +102,7 @@ public TextParseNode(@Nonnull final String rawText) { } @Nullable public > List getCollectionOfEnumValues( - @Nonnull final Function fromValue) { + @Nonnull final ValuedEnumParser enumParser) { throw new UnsupportedOperationException(NO_STRUCTURED_DATA_MESSAGE); } @@ -111,16 +110,16 @@ public TextParseNode(@Nonnull final String rawText) { throw new UnsupportedOperationException(NO_STRUCTURED_DATA_MESSAGE); } - @Nullable public > T getEnumValue(@Nonnull final Function forValue) { + @Nullable public > T getEnumValue(@Nonnull final ValuedEnumParser enumParser) { final String rawValue = this.getStringValue(); if (rawValue == null || rawValue.isEmpty()) { return null; } - return forValue.apply(rawValue); + return enumParser.forValue(rawValue); } @Nullable public > EnumSet getEnumSetValue( - @Nonnull final Function forValue) { + @Nonnull final ValuedEnumParser enumParser) { throw new UnsupportedOperationException(NO_STRUCTURED_DATA_MESSAGE); } diff --git a/gradle.properties b/gradle.properties index 73f091d4e..7524b49e2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -25,8 +25,8 @@ org.gradle.caching=true mavenGroupId = com.microsoft.kiota mavenMajorVersion = 0 -mavenMinorVersion = 9 -mavenPatchVersion = 2 +mavenMinorVersion = 10 +mavenPatchVersion = 0 mavenArtifactSuffix = #These values are used to run functional tests From d60859860f87abe64ff1fba7ff189fd963bb732f Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 15:24:33 +0000 Subject: [PATCH 04/10] Update components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java Co-authored-by: Vincent Biret --- .../java/com/microsoft/kiota/serialization/ValuedEnumParser.java | 1 + 1 file changed, 1 insertion(+) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java index dd96b3102..4344c435d 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java @@ -3,6 +3,7 @@ import jakarta.annotation.Nonnull; /** The interface for a valued enum parser. */ +@FunctionalInterface public interface ValuedEnumParser { /** * Gets an enum from it's string value. From 54ad1c7e08a77a6f04ee5b92fa81226c314ae1b0 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 15:30:32 +0000 Subject: [PATCH 05/10] update changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 303f7c6d9..2d267014b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +## [0.9.2] - 2023-11-16 + ### Changed - Added Spotless as an automatic formatting tool for the entire codebase - Changed some internal implementations of JsonParse for performance and readability reasons -- [breaking] Removed the usage of reflection for enum deserialization +- [breaking] Removed the usage of reflection for enum deserialization and reordered `RequestAdapter` arguments order ## [0.9.2] - 2023-11-16 From 95040ab2fcf4f9a2fd261a4f9493e15e1a932a00 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 15:35:31 +0000 Subject: [PATCH 06/10] minor --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d267014b..4787e2512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -## [0.9.2] - 2023-11-16 +## [0.10.0] - 2023-11-22 ### Changed From f7fe7a6bcf4b9aa2cf7ac90be09b2a073bb64b4e Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 16:16:12 +0000 Subject: [PATCH 07/10] args reordering --- .../com/microsoft/kiota/RequestAdapter.java | 22 +++++++++---------- .../kiota/http/OkHttpRequestAdapter.java | 16 +++++++------- .../kiota/http/OkHttpRequestAdapterTest.java | 10 ++++----- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java index c98ec871d..28c939a2d 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java @@ -27,43 +27,43 @@ public interface RequestAdapter { /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized response model. * @param requestInfo the request info to execute. - * @param factory the factory to create the parsable object from the type discriminator. * @param errorMappings the error factories mapping to use in case of a failed request. + * @param factory the factory to create the parsable object from the type discriminator. * @param the type of the response model to deserialize the response into. * @return the deserialized response model. */ @SuppressWarnings("LambdaLast") @Nullable ModelType send( @Nonnull final RequestInformation requestInfo, - @Nonnull final ParsableFactory factory, - @Nullable final HashMap> errorMappings); + @Nullable final HashMap> errorMappings, + @Nonnull final ParsableFactory factory); /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection. * @param requestInfo the request info to execute. - * @param factory the factory to create the parsable object from the type discriminator. * @param errorMappings the error factories mapping to use in case of a failed request. + * @param factory the factory to create the parsable object from the type discriminator. * @param the type of the response model to deserialize the response into. * @return the deserialized response model collection. */ @SuppressWarnings("LambdaLast") @Nullable List sendCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final ParsableFactory factory, - @Nullable final HashMap> errorMappings); + @Nullable final HashMap> errorMappings, + @Nonnull final ParsableFactory factory); /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model. * @param requestInfo the request info to execute. - * @param targetClass the class of the response model to deserialize the response into. * @param errorMappings the error factories mapping to use in case of a failed request. + * @param targetClass the class of the response model to deserialize the response into. * @param the type of the response model to deserialize the response into. * @return the deserialized primitive response model. */ @Nullable ModelType sendPrimitive( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, - @Nullable final HashMap> errorMappings); + @Nullable final HashMap> errorMappings, + @Nonnull final Class targetClass); /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive collection response model. @@ -75,8 +75,8 @@ public interface RequestAdapter { */ @Nullable List sendPrimitiveCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, - @Nullable final HashMap> errorMappings); + @Nullable final HashMap> errorMappings, + @Nonnull final Class targetClass); /** * Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum value. diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index 294854941..4d9dbabb1 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -178,8 +178,8 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF @Nullable public List sendCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final ParsableFactory factory, - @Nullable final HashMap> errorMappings) { + @Nullable final HashMap> errorMappings, + @Nonnull final ParsableFactory factory) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); Objects.requireNonNull(factory, nullFactoryParameter); @@ -259,8 +259,8 @@ private Span startSpan( @Nullable public ModelType send( @Nonnull final RequestInformation requestInfo, - @Nonnull final ParsableFactory factory, - @Nullable final HashMap> errorMappings) { + @Nullable final HashMap> errorMappings, + @Nonnull final ParsableFactory factory) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); Objects.requireNonNull(factory, nullFactoryParameter); @@ -322,8 +322,8 @@ private void closeResponse(boolean closeResponse, Response response) { @Nullable public ModelType sendPrimitive( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, - @Nullable final HashMap> errorMappings) { + @Nullable final HashMap> errorMappings, + @Nonnull final Class targetClass) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); Objects.requireNonNull(targetClass, "parameter targetClass cannot be null"); final Span span = startSpan(requestInfo, "sendPrimitiveAsync"); @@ -509,8 +509,8 @@ private void closeResponse(boolean closeResponse, Response response) { @Nullable public List sendPrimitiveCollection( @Nonnull final RequestInformation requestInfo, - @Nonnull final Class targetClass, - @Nullable final HashMap> errorMappings) { + @Nullable final HashMap> errorMappings, + @Nonnull final Class targetClass) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); final Span span = startSpan(requestInfo, "sendPrimitiveCollectionAsync"); diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java index b18aa87e8..53257d781 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java @@ -96,7 +96,7 @@ void SendStreamReturnsUsableStream(int statusCode) throws Exception { }; InputStream response = null; try { - response = requestAdapter.sendPrimitive(requestInformation, InputStream.class, null); + response = requestAdapter.sendPrimitive(requestInformation, null, InputStream.class); assertNotNull(response); assertEquals(text, new String(response.readAllBytes(), StandardCharsets.UTF_8)); } finally { @@ -131,7 +131,7 @@ public void SendStreamReturnsNullOnNoContent(int statusCode) throws Exception { } }; final var response = - requestAdapter.sendPrimitive(requestInformation, InputStream.class, null); + requestAdapter.sendPrimitive(requestInformation, null, InputStream.class); assertNull(response); } @@ -161,7 +161,7 @@ public void SendReturnsNullOnNoContent(int statusCode) throws Exception { }; final var mockEntity = mock(Parsable.class); when(mockEntity.getFieldDeserializers()).thenReturn(new HashMap<>()); - final var response = requestAdapter.send(requestInformation, (node) -> mockEntity, null); + final var response = requestAdapter.send(requestInformation, null, (node) -> mockEntity); assertNull(response); } @@ -200,7 +200,7 @@ public void SendReturnsObjectOnContent(int statusCode) throws Exception { when(mockFactory.getValidContentType()).thenReturn("application/json"); final var requestAdapter = new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client); - final var response = requestAdapter.send(requestInformation, (node) -> mockEntity, null); + final var response = requestAdapter.send(requestInformation, null, (node) -> mockEntity); assertNotNull(response); } @@ -242,7 +242,7 @@ public void throwsAPIException() throws Exception { final var exception = assertThrows( ApiException.class, - () -> requestAdapter.send(requestInformation, (node) -> mockEntity, null)); + () -> requestAdapter.send(requestInformation, null, (node) -> mockEntity)); assertNotNull(exception); assertEquals(404, exception.getResponseStatusCode()); assertTrue(exception.getResponseHeaders().containsKey("request-id")); From 1bf33fe1614c450fb1787812660ca90c5743ed1a Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 17:28:26 +0000 Subject: [PATCH 08/10] use the expected implementation in the tests --- .../microsoft/kiota/serialization/ValuedEnumParser.java | 3 ++- .../com/microsoft/kiota/serialization/mocks/MyEnum.java | 5 +++-- .../microsoft/kiota/serialization/mocks/TestEntity.java | 7 ++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java index 4344c435d..35afdff04 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/serialization/ValuedEnumParser.java @@ -1,6 +1,7 @@ package com.microsoft.kiota.serialization; import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; /** The interface for a valued enum parser. */ @FunctionalInterface @@ -10,5 +11,5 @@ public interface ValuedEnumParser { * @param value the string value of the enum. * @return the enum value derived from the string. */ - @Nonnull T forValue(@Nonnull String value); + @Nullable T forValue(@Nonnull String value); } diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java index 65067051f..178dd144b 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java @@ -1,9 +1,10 @@ package com.microsoft.kiota.serialization.mocks; import com.microsoft.kiota.serialization.ValuedEnum; +import com.microsoft.kiota.serialization.ValuedEnumParser; import java.util.Objects; -public enum MyEnum implements ValuedEnum { +public enum MyEnum implements ValuedEnum, ValuedEnumParser { MY_VALUE1("VALUE1"), MY_VALUE2("VALUE2"); public final String value; @@ -16,7 +17,7 @@ public enum MyEnum implements ValuedEnum { return this.value; } - @jakarta.annotation.Nullable public static MyEnum forValue(@jakarta.annotation.Nonnull final String searchValue) { + @jakarta.annotation.Nullable public MyEnum forValue(@jakarta.annotation.Nonnull final String searchValue) { Objects.requireNonNull(searchValue); switch (searchValue) { case "VALUE1": diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java index 786b7db3e..cf08986aa 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java @@ -131,7 +131,12 @@ public Map> getFieldDeserializers() { put( "myEnum", (n) -> { - setMyEnum(n.getEnumValue(MyEnum::forValue)); + setMyEnum( + n.getEnumValue( + value -> + (value == null || MyEnum.values().length < 1) + ? null + : MyEnum.values()[0].forValue(value))); }); put( "createdDateTime", From f488ee95668da44e12a42cc406e69552a105515f Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 21 Nov 2023 17:40:13 +0000 Subject: [PATCH 09/10] remove LambdaLast exclusions --- .../src/main/java/com/microsoft/kiota/RequestAdapter.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java index 28c939a2d..a7213eca1 100644 --- a/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java +++ b/components/abstractions/src/main/java/com/microsoft/kiota/RequestAdapter.java @@ -32,7 +32,6 @@ public interface RequestAdapter { * @param the type of the response model to deserialize the response into. * @return the deserialized response model. */ - @SuppressWarnings("LambdaLast") @Nullable ModelType send( @Nonnull final RequestInformation requestInfo, @Nullable final HashMap> errorMappings, @@ -46,7 +45,6 @@ public interface RequestAdapter { * @param the type of the response model to deserialize the response into. * @return the deserialized response model collection. */ - @SuppressWarnings("LambdaLast") @Nullable List sendCollection( @Nonnull final RequestInformation requestInfo, @Nullable final HashMap> errorMappings, From 3813d3510c09bf9a0526d1f9fd8c6b908a82fa82 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Wed, 22 Nov 2023 09:46:50 +0000 Subject: [PATCH 10/10] review comments --- .../com/microsoft/kiota/serialization/mocks/MyEnum.java | 9 +++++---- .../microsoft/kiota/serialization/mocks/TestEntity.java | 7 +------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java index 178dd144b..e91ac7175 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/MyEnum.java @@ -1,10 +1,11 @@ package com.microsoft.kiota.serialization.mocks; import com.microsoft.kiota.serialization.ValuedEnum; -import com.microsoft.kiota.serialization.ValuedEnumParser; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; import java.util.Objects; -public enum MyEnum implements ValuedEnum, ValuedEnumParser { +public enum MyEnum implements ValuedEnum { MY_VALUE1("VALUE1"), MY_VALUE2("VALUE2"); public final String value; @@ -13,11 +14,11 @@ public enum MyEnum implements ValuedEnum, ValuedEnumParser { this.value = value; } - @jakarta.annotation.Nonnull public String getValue() { + @Nonnull public String getValue() { return this.value; } - @jakarta.annotation.Nullable public MyEnum forValue(@jakarta.annotation.Nonnull final String searchValue) { + @Nullable public static MyEnum forValue(@jakarta.annotation.Nonnull final String searchValue) { Objects.requireNonNull(searchValue); switch (searchValue) { case "VALUE1": diff --git a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java index cf08986aa..786b7db3e 100644 --- a/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java +++ b/components/serialization/json/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java @@ -131,12 +131,7 @@ public Map> getFieldDeserializers() { put( "myEnum", (n) -> { - setMyEnum( - n.getEnumValue( - value -> - (value == null || MyEnum.values().length < 1) - ? null - : MyEnum.values()[0].forValue(value))); + setMyEnum(n.getEnumValue(MyEnum::forValue)); }); put( "createdDateTime",