33import java .io .IOException ;
44import java .lang .reflect .ParameterizedType ;
55import java .util .ArrayList ;
6+ import java .util .HashMap ;
67import java .util .List ;
78import java .util .Map ;
9+ import java .util .UUID ;
810import java .util .function .Function ;
911
1012import com .google .gson .Gson ;
1921import com .google .gson .stream .JsonReader ;
2022import com .google .gson .stream .JsonWriter ;
2123
22- import io .weaviate .client6 .v1 .api .collections .WeaviateObject ;
2324import io .weaviate .client6 .v1 .api .collections .Vectors ;
25+ import io .weaviate .client6 .v1 .api .collections .WeaviateObject ;
2426import io .weaviate .client6 .v1 .internal .ObjectBuilder ;
2527
2628public record WriteWeaviateObject <PropertiesT >(
@@ -52,12 +54,18 @@ public WriteWeaviateObject(Builder<PropertiesT> builder) {
5254 }
5355
5456 public static class Builder <PropertiesT > implements ObjectBuilder <WriteWeaviateObject <PropertiesT >> {
55- private String uuid ;
57+ /**
58+ * The server <i>should be</i> providing default UUIDs, but it does not do that
59+ * during batch inserts and we have to provide our own.
60+ * Rather than make this behaviour special to {@code insertMany}, we are going
61+ * to provide a fallback UUID "globally".
62+ */
63+ private String uuid = UUID .randomUUID ().toString ();
5664 private String collection ;
5765 private String tenant ;
5866 private PropertiesT properties ;
5967 private Vectors vectors ;
60- private Map <String , List <Reference >> references ;
68+ private Map <String , List <Reference >> references = new HashMap <>() ;
6169
6270 public Builder <PropertiesT > uuid (String uuid ) {
6371 this .uuid = uuid ;
@@ -122,28 +130,28 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
122130 var rawType = typeToken .getRawType ();
123131 if (rawType != WriteWeaviateObject .class ||
124132 !(type instanceof ParameterizedType parameterized )
125- || parameterized .getActualTypeArguments ().length < 1 ) {
133+ || parameterized .getActualTypeArguments ().length != 1 ) {
126134 return null ;
127135 }
128136
129137 var typeParams = parameterized .getActualTypeArguments ();
130138 final var propertiesType = typeParams [0 ];
131139
132- final TypeAdapter < WriteWeaviateObject <?>> delegate = (TypeAdapter <WriteWeaviateObject <?>>) gson
140+ final var delegate = (TypeAdapter <WriteWeaviateObject <?>>) gson
133141 .getDelegateAdapter (this , typeToken );
134- final var propertiesAdapter = gson .getAdapter (TypeToken .get (propertiesType ));
142+ final var propertiesAdapter = ( TypeAdapter < Object >) gson .getAdapter (TypeToken .get (propertiesType ));
135143 final var referencesAdapter = gson .getAdapter (Reference .class );
136144
137145 return (TypeAdapter <T >) new TypeAdapter <WriteWeaviateObject <?>>() {
138146
139147 @ Override
140148 public void write (JsonWriter out , WriteWeaviateObject <?> value ) throws IOException {
141149 var json = delegate .toJsonTree (value ).getAsJsonObject ();
142- var properties = (( TypeAdapter < Object >) propertiesAdapter )
143- .toJsonTree (value .properties ())
144- . getAsJsonObject ();
150+ var properties = value . properties () != null
151+ ? propertiesAdapter .toJsonTree (value .properties ()). getAsJsonObject ( )
152+ : new JsonObject ();
145153
146- if (!value .references ().isEmpty ()) {
154+ if (value . references () != null && !value .references ().isEmpty ()) {
147155 for (var refEntry : value .references ().entrySet ()) {
148156 var beacons = new JsonArray ();
149157 for (var reference : refEntry .getValue ()) {
@@ -154,7 +162,7 @@ public void write(JsonWriter out, WriteWeaviateObject<?> value) throws IOExcepti
154162 }
155163 }
156164
157- json .add ("properties" , json );
165+ json .add ("properties" , properties );
158166 json .remove ("references" );
159167 Streams .write (json , out );
160168 }
@@ -185,6 +193,7 @@ public WriteWeaviateObject<?> read(JsonReader in) throws IOException {
185193 }
186194
187195 json .add ("references" , objectReferences );
196+ json .add ("properties" , objectProperties );
188197 return delegate .fromJsonTree (json );
189198 }
190199 }.nullSafe ();
0 commit comments