{"record":{"id":"7ddb21c15fe5ae06","repo":"apache/beam","slug":"can-t-convert-null-to-fieldtype","errorCode":null,"errorMessage":"Can't convert 'null' to FieldType","messagePattern":"Can't convert 'null' to FieldType","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java","lineNumber":1125,"sourceCode":"\n        case DOUBLE:\n          fieldType = FieldType.DOUBLE;\n          break;\n\n        case BOOLEAN:\n          fieldType = FieldType.BOOLEAN;\n          break;\n\n        case UNION:\n          fieldType =\n              FieldType.logicalType(\n                  OneOfType.create(\n                      avroSchema.getTypes().stream()\n                          .map(x -> Field.of(x.getName(), toFieldType(new TypeWithNullability(x))))\n                          .collect(Collectors.toList())));\n          break;\n        case NULL:\n          throw new IllegalArgumentException(\"Can't convert 'null' to FieldType\");\n\n        default:\n          throw new AssertionError(\"Unexpected AVRO Schema.Type: \" + avroSchema.getType());\n      }\n    }\n    fieldType = fieldType.withNullable(type.nullable);\n    return fieldType;\n  }\n\n  private static org.apache.avro.Schema getFieldSchema(\n      FieldType fieldType, String fieldName, String namespace) {\n    org.apache.avro.Schema baseType;\n    switch (fieldType.getTypeName()) {\n      case BYTE:\n      case INT16:\n      case INT32:\n        baseType = org.apache.avro.Schema.create(org.apache.avro.Schema.Type.INT);\n        break;","sourceCodeStart":1107,"sourceCodeEnd":1143,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java#L1107-L1143","documentation":"When converting an AVRO schema to a Beam Schema, a top-level Avro Schema.Type.NULL is encountered. Beam's FieldType model has no representation for a bare 'null' type (null is only modeled as nullability on another type), so the library throws IllegalArgumentException instead of guessing a representation.","triggerScenarios":"Calling AvroUtils.toBeamSchema (or fromAvroSchema-based helpers) on an Avro schema whose UNION is just ['null'] or whose field type is Type.NULL directly, i.e. toFieldType()/convertLogic hitting case NULL.","commonSituations":"Hand-written or third-party-generated Avro schemas containing a degenerate union ['null'] with no concrete branch; schema registries returning null-only fields; code generators emitting null placeholders for optional fields.","solutions":["Rewrite the Avro schema so 'null' only appears in unions with a concrete type, e.g. ['null','string'] instead of ['null'] or NULL.","If the field is truly optional, model it with a concrete union branch; Beam maps that to a nullable FieldType automatically.","Strip or default null-only fields from the schema before conversion.","Catch IllegalArgumentException around toBeamSchema and fail fast with a clear schema-validation message."],"exampleFix":"// before\n{\"name\":\"opt\",\"type\":\"null\"}\n// after\n{\"name\":\"opt\",\"type\":[\"null\",\"string\"]}","handlingStrategy":"validation","validationCode":"static boolean isConvertibleToBeam(org.apache.avro.Schema s) {\n  if (s.getType() == org.apache.avro.Schema.Type.NULL) return false;\n  if (s.getType() == org.apache.avro.Schema.Type.UNION) {\n    return s.getTypes().stream().allMatch(t -> t.getType() != org.apache.avro.Schema.Type.NULL || s.getTypes().size() > 1);\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n  Schema beamSchema = AvroUtils.toBeamSchema(avroSchema);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"Can't convert 'null' to FieldType\")) {\n    throw new SchemaValidationException(\"Null-only Avro type; use ['null', concreteType] unions\", e);\n  }\n  throw e;\n}","preventionTips":["Never author Avro fields whose type is 'null' alone; always pair null with a concrete type in unions.","Validate Avro schemas (avro Schema.Parser + custom lint) before feeding them to Beam conversion.","Treat nullability in Beam as FieldType.withNullable, not as a standalone type.","Add a unit test that round-trips all production schemas through toBeamSchema."],"tags":["avro","beam","schema-conversion","null-type"],"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"}