From f602a968e3dc014da5ffca81b9786036295610ba Mon Sep 17 00:00:00 2001
From: Jakob Edding <15202881+JakobEdding@users.noreply.github.com>
Date: Thu, 15 Feb 2024 12:47:56 +0100
Subject: [PATCH] Allow non-null but empty Parameters object in inference
response (#11)
So far, kserve-client can only deserialize `parameters: null` or a
`parameters: { ... }` object (with all its fields filled) from an
inference response.
However, some KServe InferenceServices return `parameters: {}` as part
of an inference response. This case should also be accepted as a valid
response.
---
build.gradle.kts | 1 +
.../bakdata/kserve/predictv2/Parameters.java | 10 ++-
.../predictv2/InferenceResponseTest.java | 64 +++++++++++++++++++
.../parameters_empty.json | 19 ++++++
.../parameters_null.json | 19 ++++++
5 files changed, 110 insertions(+), 3 deletions(-)
create mode 100644 src/test/java/com/bakdata/kserve/predictv2/InferenceResponseTest.java
create mode 100644 src/test/resources/json_inference_responses/parameters_empty.json
create mode 100644 src/test/resources/json_inference_responses/parameters_null.json
diff --git a/build.gradle.kts b/build.gradle.kts
index 41f5bc9..853aa0e 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -37,6 +37,7 @@ dependencies {
val junitVersion: String by project
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter-api", version = junitVersion)
+ testImplementation(group = "org.junit.jupiter", name = "junit-jupiter-params", version = junitVersion)
testRuntimeOnly(group = "org.junit.jupiter", name = "junit-jupiter-engine", version = junitVersion)
testImplementation(group = "org.assertj", name = "assertj-core", version = "3.25.1")
testImplementation(group = "com.squareup.okhttp3", name = "mockwebserver", version = okHttpVersion)
diff --git a/src/main/java/com/bakdata/kserve/predictv2/Parameters.java b/src/main/java/com/bakdata/kserve/predictv2/Parameters.java
index c1f2b39..e53d8d4 100644
--- a/src/main/java/com/bakdata/kserve/predictv2/Parameters.java
+++ b/src/main/java/com/bakdata/kserve/predictv2/Parameters.java
@@ -1,7 +1,7 @@
/*
* MIT License
*
- * Copyright (c) 2022 bakdata
+ * Copyright (c) 2024 bakdata GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,15 +24,19 @@
package com.bakdata.kserve.predictv2;
+import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
+import lombok.NoArgsConstructor;
/**
- * A class to represent
- * parameters as defined in the v2 prediction protocol.
+ * A class to represent parameters as
+ * defined in the v2 prediction protocol.
*/
@Data
@Builder
+@NoArgsConstructor
+@AllArgsConstructor
public class Parameters {
private String contentType;
private Object extra;
diff --git a/src/test/java/com/bakdata/kserve/predictv2/InferenceResponseTest.java b/src/test/java/com/bakdata/kserve/predictv2/InferenceResponseTest.java
new file mode 100644
index 0000000..22a7200
--- /dev/null
+++ b/src/test/java/com/bakdata/kserve/predictv2/InferenceResponseTest.java
@@ -0,0 +1,64 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2024 bakdata GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.bakdata.kserve.predictv2;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.PropertyNamingStrategies;
+import java.io.IOException;
+import org.assertj.core.api.SoftAssertions;
+import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
+import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+@ExtendWith(SoftAssertionsExtension.class)
+public class InferenceResponseTest {
+ private ObjectMapper objectMapper;
+
+ @InjectSoftAssertions
+ private SoftAssertions softly;
+
+ @BeforeEach
+ void setUpObjectMapper() {
+ this.objectMapper = new ObjectMapper()
+ .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "json_inference_responses/parameters_null.json",
+ "json_inference_responses/parameters_empty.json"
+ })
+ void shouldDeserialize(String jsonFilePath) throws IOException {
+ byte[] resourceFileBytes = getClass().getClassLoader().getResourceAsStream(jsonFilePath).readAllBytes();
+ String jsonInferenceResponse = new String(resourceFileBytes);
+
+ this.softly.assertThatCode(() -> {
+ this.objectMapper.readValue(jsonInferenceResponse, InferenceResponse.class);
+ }).doesNotThrowAnyException();
+ }
+}
diff --git a/src/test/resources/json_inference_responses/parameters_empty.json b/src/test/resources/json_inference_responses/parameters_empty.json
new file mode 100644
index 0000000..7180f5b
--- /dev/null
+++ b/src/test/resources/json_inference_responses/parameters_empty.json
@@ -0,0 +1,19 @@
+{
+ "model_name": "fake-model-name",
+ "model_version": "1.0.0",
+ "id": "d26ad6e7-0cd7-4f37-b463-0409d9246b43",
+ "parameters": {},
+ "outputs": [
+ {
+ "name": "fake-output-name",
+ "shape": [
+ 1
+ ],
+ "datatype": "object",
+ "parameters": null,
+ "data": {
+ "output": "fake output"
+ }
+ }
+ ]
+}
diff --git a/src/test/resources/json_inference_responses/parameters_null.json b/src/test/resources/json_inference_responses/parameters_null.json
new file mode 100644
index 0000000..96639c6
--- /dev/null
+++ b/src/test/resources/json_inference_responses/parameters_null.json
@@ -0,0 +1,19 @@
+{
+ "model_name": "fake-model-name",
+ "model_version": "1.0.0",
+ "id": "d26ad6e7-0cd7-4f37-b463-0409d9246b43",
+ "parameters": null,
+ "outputs": [
+ {
+ "name": "fake-output-name",
+ "shape": [
+ 1
+ ],
+ "datatype": "object",
+ "parameters": null,
+ "data": {
+ "output": "fake output"
+ }
+ }
+ ]
+}