{"record":{"id":"9640a8b38761777e","repo":"apache/beam","slug":"expected-map-for-type-fieldtype-but-got-value-getclass","errorCode":null,"errorMessage":"Expected Map for type \" + fieldType + \", but got: \" + value.getClass().getName()","messagePattern":"Expected Map 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":173,"sourceCode":"              \"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)) {\n          throw new IllegalArgumentException(\n              \"Expected Map for type \" + fieldType + \", but got: \" + value.getClass().getName());\n        }\n        Map<?, ?> map = (Map<?, ?>) value;\n        Map<String, @Nullable Object> rowMap = new HashMap<>();\n        FieldType valueType = fieldType.getMapValueType();\n        if (valueType == null) {\n          throw new IllegalArgumentException(\n              \"Map value type cannot be null for type: \" + fieldType);\n        }\n        for (Map.Entry<?, ?> entry : map.entrySet()) {\n          rowMap.put(\n              String.valueOf(entry.getKey()), convertFromBsonValue(entry.getValue(), valueType));\n        }\n        return rowMap;\n      case ROW:\n        Schema rowSchema = fieldType.getRowSchema();\n        if (rowSchema == null) {\n          throw new IllegalArgumentException(\"Row schema cannot be null for type: \" + fieldType);","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbUtils.java#L155-L191","documentation":"MongoDbUtils.convertFromBsonValue is converting a BSON value into a Beam value for a schema field whose FieldType is MAP. It requires the raw Java value to already be a java.util.Map (e.g. a BSON Document), but the runtime value is some other class. This is a programming/configuration mismatch: the declared Beam schema does not match the shape of the data coming out of MongoDB.","triggerScenarios":"A field in the Beam schema is declared with FieldType MAP, but convertFromBsonValue receives a non-Map object for that field — e.g. the BSON value is a scalar, array, Document subclass variant, or a decoded type not implementing Map. Reached via toRow or recursively via convertFromBsonValue for nested collections.","commonSituations":"Schema auto-inference from a changed MongoDB document; storing a string or ObjectId where a map is expected; nested arrays of maps where the element type was inferred as MAP instead of ARRAY<MAP>; driver version changes altering decoded classes (Document vs RawBsonDocument).","solutions":["Inspect the offending MongoDB document and correct the Beam schema FieldType (e.g. use a ROW or STRING type instead of MAP) to match the stored data.","If the value is an array, change the field type to ARRAY<MAP<STRING,STRING>> so the iterable is unwrapped before the MAP case runs.","Add a pre-conversion normalization step (custom DoFn) that converts non-Map BSON values (e.g. Document wrapped in other types) into java.util.Map before toRow.","Pin/align the MongoDB Java driver version so decoded values are org.bson.Document (a Map) as expected."],"exampleFix":"// before: schema field declared as MAP but data is scalar\nfields.add(Field.of(\"attrs\", FieldType.map(FieldType.STRING)));\n// after: match the actual stored type (e.g. string JSON)\nfields.add(Field.of(\"attrs\", FieldType.STRING));","handlingStrategy":"validation","validationCode":"if (!(bsonValue instanceof Map) && fieldType.getTypeName().equals(TypeName.MAP)) {\n  throw new IllegalStateException(\"Field expects a document but got: \" + bsonValue.getClass());\n}","typeGuard":"static boolean isConvertibleMap(Object v) { return v instanceof java.util.Map; }","tryCatchPattern":"try {\n  Row row = MongoDbUtils.toRow(doc, schema);\n} catch (IllegalArgumentException e) {\n  LOG.error(\"BSON->Row conversion failed for schema {}: {}\", schema, e.getMessage());\n  throw e;\n}","preventionTips":["Derive the Beam schema from representative production documents, not a single sample.","Log document class names for mismatched fields before conversion.","Pin the MongoDB driver version in your build."],"tags":["mongodb","apache-beam","schema","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}