{"record":{"id":"16955764eff8aa81","repo":"apache/beam","slug":"expected-iterable-for-type-fieldtype-but-got-value-getclass","errorCode":null,"errorMessage":"Expected Iterable for type \" + fieldType + \", but got: \" + value.getClass().getName()","messagePattern":"Expected Iterable for type \" \\+ fieldType \\+ \", but got: \" \\+ value\\.getClass\\(\\)\\.getName\\(\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbUtils.java","lineNumber":154,"sourceCode":"        if (value instanceof java.util.Date) {\n          return new Instant(((java.util.Date) value).getTime());\n        } else if (value instanceof Number) {\n          return new Instant(((Number) value).longValue());\n        } else {\n          return Instant.parse(value.toString());\n        }\n      case BYTES:\n        if (value instanceof Binary) {\n          return ((Binary) value).getData();\n        } else if (value instanceof byte[]) {\n          return (byte[]) value;\n        } else {\n          return value.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);\n        }\n      case ARRAY:\n      case ITERABLE:\n        if (!(value instanceof Iterable)) {\n          throw new IllegalArgumentException(\n              \"Expected Iterable for type \"\n                  + fieldType\n                  + \", but got: \"\n                  + value.getClass().getName());\n        }\n        Iterable<?> iterable = (Iterable<?>) value;\n        List<@Nullable Object> rowList = new ArrayList<>();\n        FieldType elementType = fieldType.getCollectionElementType();\n        if (elementType == null) {\n          throw new IllegalArgumentException(\n              \"Collection element type cannot be null for type: \" + fieldType);\n        }\n        for (Object item : iterable) {\n          rowList.add(convertFromBsonValue(item, elementType));\n        }\n        return rowList;\n      case MAP:\n        if (!(value instanceof Map)) {","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbUtils.java#L136-L172","documentation":"In MongoDbUtils.convertFromBsonValue, when the Beam schema field type is ARRAY or ITERABLE, the BSON value must itself be an Iterable. If a scalar (or non-iterable value) appears where an array was declared, this IllegalArgumentException is thrown naming the field type and actual value class.","triggerScenarios":"Reading MongoDB documents where a field declared as ARRAY/ITERABLE in the Row schema holds a non-array BSON value (string, int, object) — e.g., schema says array but some documents store a scalar.","commonSituations":"Heterogeneous MongoDB collections where a field evolved from scalar to array (or vice versa); schema defined from a sample document that doesn't match all documents.","solutions":["Align the Row schema field type with the actual BSON type (declare it scalar if the data is scalar)","Cleanse/migrate MongoDB data so the field is consistently an array","Pre-filter or normalize documents (e.g., wrap scalars in a single-element list) before conversion"],"exampleFix":"// before\n// schema: \"tags\" ARRAY<STRING>, document: { \"tags\": \"one-tag\" }\n// after\n// normalize document before reading:\ndoc.put(\"tags\", doc.get(\"tags\") instanceof List ? doc.get(\"tags\") : List.of((String) doc.get(\"tags\")));","handlingStrategy":"validation","validationCode":"Object tags = doc.get(\"tags\");\nif (!(tags instanceof Iterable)) {\n  throw new IllegalArgumentException(\"Field 'tags' declared ARRAY but value is \" + (tags == null ? \"null\" : tags.getClass()));\n}","typeGuard":"static boolean isIterableValue(Object value) {\n  return value instanceof Iterable;\n}","tryCatchPattern":"try {\n  Row row = MongoDbUtils.toRow(doc, schema);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Expected Iterable for type\")) {\n    // coerce scalar to single-element list or fix schema type\n  }\n}","preventionTips":["Ensure schema field types match actual BSON types in all documents, not just samples","Normalize scalars-vs-arrays in MongoDB before reading","Use MongoDB schema validation ($jsonSchema) to enforce consistent field types"],"tags":["java","apache-beam","mongodb","type-mismatch","array"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}