Repository navigation
Description
Summary
Type.convert(db, value, List.class) returns List.of(value) - a JDK immutable list - whenever the incoming value is not already a Collection. MutableDocument.set() routes every write through convertValueToSchemaType(), so a scalar written to a property declared as LIST is stored as an immutable one-element list, while the same property written with a Collection is stored as a mutable ArrayList. Two consequences:
MutableDocument.newEmbeddedDocument(type, propertyName)documents "If the property name already exists, and it is a collection, then the embedded document is added to the collection" and implements it as((Collection) old).add(emb). On a declaredLISTproperty the first call storesList.of(emb), so the second call takes exactly that documented branch and dies withUnsupportedOperationException. Building a two-element embedded list on a schema-declared LIST property is therefore impossible through the documented API.- Any caller that reads a LIST property back and mutates it in place gets a working list or an exception depending purely on whether the value was first written as a scalar or as a collection.
Note the undeclared case is wrong in a different way (silently drops the first embedded document rather than throwing) but is arguably within the javadoc's "and it is a collection" caveat; the declared case is not.
Where (HEAD 417314c)
engine/src/main/java/com/arcadedb/schema/Type.java:622
} else if (List.class.isAssignableFrom(targetClass)) {
// The caller specifically wants a List. If the value is a collection
// we will add all of the items in the collection to a List. Otherwise
// we will create a singleton List with only the value in it.
if (value instanceof Collection<?> collection) {
final List<Object> list = new ArrayList<Object>(collection);
return list;
} else {
return List.of(value);
}(The Set.of(value) arms at Type.java:620 and Type.java:642 are the identical sibling shape.)
engine/src/main/java/com/arcadedb/database/MutableDocument.java:269
public MutableDocument newEmbeddedDocument(final String embeddedTypeName, final String propertyName) {
final Object old = get(propertyName);
...
if (old instanceof Collection) {
((Collection<EmbeddedDocument>) old).add(emb); // <-- UOE when old came from List.of()Repro
$ javac -proc:none -cp "audit/engine/target/classes:$(cat engine-cp.txt)" -d repro2 repro2/EmbProbe.java
$ java -cp "repro2:audit/engine/target/classes:$(cat engine-cp.txt)" EmbProbe
[DocU] after 1st: com.arcadedb.database.MutableEmbeddedDocument = @Item[n=1]
[DocU] after 2nd: OK -> @Item[n=2] <- undeclared: first embedded doc silently replaced
[DocD] after 1st: java.util.ImmutableCollections$List12 = [@Item[n=1]]
[DocD] after 2nd: THREW java.lang.UnsupportedOperationException
(DocD declares t.createProperty("items", Type.LIST), DocU declares nothing; both call
d.newEmbeddedDocument("Item", "items") twice inside one transaction.)
And the plain set() form of the same asymmetry:
$ java -cp "repro2:..." EmbProbe2
set(scalar) -> java.util.ImmutableCollections$List12 [a]
add -> THREW java.lang.UnsupportedOperationException
set(collection) -> java.util.ArrayList [a]
add -> OK [a, b]
Suggested fix
Return new ArrayList<>(List.of(value)) (or new ArrayList<>(1) + add) from the non-collection arm so a LIST property always holds a mutable container, and the same for the two Set.of(value) arms (new HashSet<>). List.of additionally rejects a null element, which the mutable form would accept. Independently, newEmbeddedDocument could defensively replace a non-mutable container rather than trusting Collection.add.
Scope / confidence
Refutation attempted: (a) checked whether get() on an unset declared LIST already returns an immutable empty list and so the blame lay elsewhere - it returns null (probe GetProbe, prints get(items) on unset declared LIST -> null), so the immutable list genuinely comes from the first set(); (b) checked the undeclared path to be sure the schema declaration is what makes the difference - it is; (c) checked that convertValueToSchemaType is on the normal set() path and not a rare branch - MutableDocument.set() at lines 118/196/208/230/255 all call it. Falsified if a caller is expected to never mutate a value read back from a document, but newEmbeddedDocument's own javadoc and implementation do exactly that.
Activity
fix(#7777): Type.convert() wraps a scalar for a List/Set/Collection i…
robfrank commentedon Sep 29, 2026
Fixed in #8614. Writing a single value to a property declared as a list (or set/collection) stored it as an immutable one-element collection, so adding a second embedded document to that property failed with
UnsupportedOperationException. The single-value wrapping now produces a regular mutable list/set, andnewEmbeddedDocument()also copes with an immutable collection that the caller set itself by replacing it with a mutable copy that carries the new element.Still open: #8613 - calling
newEmbeddedDocument(type, property)on an undeclared property still overwrites the previous embedded document instead of building a collection (contract decision, not changed here). Everything else in this report is fixed.❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.
fix(#7777): Type.convert() wraps a scalar for a List/Set/Collection i…