Skip to content

Commit 132ba34

Browse files
committed
fix: handle missing primitive values in ORM
1 parent 1e4a811 commit 132ba34

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/it/java/io/weaviate/integration/ORMITest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.weaviate.integration;
22

3+
import java.io.IOException;
34
import java.time.OffsetDateTime;
45
import java.util.List;
56
import java.util.Map;
@@ -14,6 +15,7 @@
1415
import io.weaviate.ConcurrentTest;
1516
import io.weaviate.client6.v1.api.WeaviateClient;
1617
import io.weaviate.client6.v1.api.collections.CollectionConfig;
18+
import io.weaviate.client6.v1.api.collections.WeaviateObject;
1719
import io.weaviate.client6.v1.api.collections.annotations.Collection;
1820
import io.weaviate.client6.v1.api.collections.annotations.Property;
1921
import io.weaviate.client6.v1.api.collections.data.InsertManyResponse.InsertObject;
@@ -23,7 +25,7 @@
2325
public class ORMITest extends ConcurrentTest {
2426
private static WeaviateClient client = Container.WEAVIATE.getClient();
2527

26-
@Collection("ORMITest")
28+
@Collection("ORMITestThings")
2729
static record Thing(
2830
// text / text[]
2931
String text,
@@ -95,7 +97,7 @@ public void test_createCollection() throws Exception {
9597

9698
// Assert
9799
Assertions.assertThat(config).get()
98-
.returns("ORMITest", CollectionConfig::collectionName)
100+
.returns("ORMITestThings", CollectionConfig::collectionName)
99101
.extracting(CollectionConfig::properties,
100102
InstanceOfAssertFactories.list(io.weaviate.client6.v1.api.collections.Property.class))
101103
.extracting(p -> Map.entry(
@@ -307,4 +309,45 @@ public void test_insertManyAndQuery() throws Exception {
307309
.usingRecursiveComparison(COMPARISON_CONFIG)
308310
.asInstanceOf(InstanceOfAssertFactories.list(Thing.class));
309311
}
312+
313+
@Collection("ORMITestSongs")
314+
record Song(
315+
String title,
316+
String album,
317+
int year,
318+
boolean hasAward,
319+
Long monthlyListeners) {
320+
}
321+
322+
/**
323+
* Test that serialization works correctly when some fields are null and
324+
* deserialization works correctly when some properties are not returned.
325+
*/
326+
@Test
327+
public void test_partialScan() throws IOException {
328+
client.collections.create(Song.class);
329+
330+
var songs = client.collections.use(Song.class);
331+
332+
// Act: insert with nulls
333+
var dystopia = songs.data.insert(new Song(
334+
"Dystopia",
335+
null,
336+
2016,
337+
true,
338+
null));
339+
340+
// Act: return subset of the properties
341+
var got = songs.query.byId(dystopia.uuid(),
342+
q -> q.returnProperties("title", "hasAward"));
343+
344+
// Assert
345+
Assertions.assertThat(got).get()
346+
.extracting(WeaviateObject::properties)
347+
.returns("Dystopia", Song::title)
348+
.returns(null, Song::album)
349+
.returns(0, Song::year)
350+
.returns(true, Song::hasAward)
351+
.returns(null, Song::monthlyListeners);
352+
}
310353
}

src/main/java/io/weaviate/client6/v1/internal/orm/PojoBuilder.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ static record Arg(Class<?> type, Object value) {
1919
Arg withValue(Object value) {
2020
return new Arg(this.type, value);
2121
}
22+
23+
public Object value() {
24+
if (value != null) {
25+
return value;
26+
}
27+
28+
if (type == boolean.class) {
29+
return false;
30+
} else if (type == short.class) {
31+
return (short) 0;
32+
} else if (type == int.class) {
33+
return 0;
34+
} else if (type == long.class) {
35+
return 0L;
36+
} else if (type == float.class) {
37+
return 0f;
38+
} else if (type == double.class) {
39+
return 0d;
40+
}
41+
42+
throw new IllegalArgumentException(type.getName() + " property data type is not supported");
43+
}
2244
}
2345

2446
PojoBuilder(PojoDescriptor<PropertiesT> descriptor) {

0 commit comments

Comments
 (0)