Skip to content

Commit

Permalink
Allow non-null but empty Parameters object in inference response (#11)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JakobEdding authored Feb 15, 2024
1 parent e6b19b5 commit f602a96
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/bakdata/kserve/predictv2/Parameters.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <a href="https://kserve.github.io/website/modelserving/inference_api/#parameters">
* parameters as defined in the v2 prediction protocol</a>.
* A class to represent <a href="https://kserve.github.io/website/modelserving/inference_api/#parameters"> parameters as
* defined in the v2 prediction protocol</a>.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Parameters {
private String contentType;
private Object extra;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
19 changes: 19 additions & 0 deletions src/test/resources/json_inference_responses/parameters_empty.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
19 changes: 19 additions & 0 deletions src/test/resources/json_inference_responses/parameters_null.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}

0 comments on commit f602a96

Please sign in to comment.