Skip to content

type.enum is valid while it should fail, good is type.type.enum according to spec #476

Closed
vectordotdev/vector
#26146

Description

@woile

Hi, a schema like this is parsed correctly by avro-rs, this tests fails (it should err):

fn test_wrong_enum() {
    let schema_str = r#"{
        "type": "record",
        "name": "ExampleEnum",
        "namespace": "com.schema",
        "fields": [
            {
            "name": "wrong_enum",
            "type": "enum",
            "symbols": ["INSERT", "UPDATE"]
            }
        ]
    }"#;
    let result = Schema::parse_str(schema_str);
    assert!(result.is_err());
}

While tools like avro-tools fail to create the enums with this schema.

The problem is that enum should be defined inside enum, this would be a good looking example.
Notice the type.type.enum:

{
  "type": "record",
  "name": "ExampleEnum",
  "namespace": "com.schema",
  "fields": [
    {
      "name": "wrong_enum",
      "type": {
        "name": "WrongEnum",
        "type": "enum",
        "symbols": ["INSERT", "UPDATE"]
      }
    }
  ]
}

I couldn't find any example in the specification of type.enum, and given that avro-tools fails to generate the java code, I assume type.enum is wrong. Let me know if that's not the case.

Best.

Activity

  1. Kriskras99 commented on Feb 20, 2026

    @Kriskras99
    Contributor

    I think you're correct. As far as I'm aware only primitive types and references are allowed to be declared directly, complex types need an extra object.

  2. Kriskras99 commented on Feb 20, 2026

    @Kriskras99
    Contributor

    The patch for this is trivial:

    --- a/avro/src/schema/record/field.rs
    +++ b/avro/src/schema/record/field.rs
    @@ -81,12 +81,8 @@ impl RecordField {
     
             validate_record_field_name(&name)?;
     
    -        // TODO: "type" = "<record name>"
    -        let schema = parser.parse_complex(
    -            field,
    -            &enclosing_record.namespace,
    -            RecordSchemaParseLocation::FromField,
    -        )?;
    +        let ty = field.get("type").ok_or(Details::GetRecordFieldTypeField)?;
    +        let schema = parser.parse(ty, &enclosing_record.namespace)?;
     
             let default = field.get("default").cloned();
             Self::resolve_default_value(

    But this breaks 26 tests, all because they use a complex type directly in a field:

    {
        "type": "record",
        "fields": [
            {            
                "name": "this_issue",
                "type": "enum",
                "symbols": ["INSERT", "UPDATE"]
            },
            {            
                "name": "direct_map",
                "type": "map",
                "values": "long"
            },
            {            
                "name": "direct_array",
                "type": "array",
                "items": "long"
            },
            {            
                "name": "direct_fixed",
                "type": "fixed",
                "size": 16
            }
        ]
    }

    The problem is the most pressing for enum and fixed as they currently always share their type name with the field name.
    @martin-g what do you think? I think this is the right thing to do.

  3. martin-g commented on Feb 20, 2026

    @martin-g
    Member

    Both ways are supported for backwards compatibility. The "flat" way was there since the donation of the original avro-rs crate to ASF.
    I'll have to check with the spec to verify that it is not supported. But I guess there are users of the flat way in the wild.

  4. Kriskras99 commented on Feb 20, 2026

    @Kriskras99
    Contributor

    One more issue with this change:

    {
        "type": "record",
        "name": "test",
        "fields": [
            {"name": "a", "type": {"type": "long", "logicalType": "timestamp-micros"}}
        ]
    }

    vs

    {
        "type": "record",
        "name": "test",
        "fields": [
            {"name": "a", "type": "long", "logicalType": "timestamp-micros"}
        ]
    }

    This does not parse the logical type with the patch provided above. We should probably check what the Java implementation does in these cases

  5. woile commented on Feb 20, 2026

    @woile
    ContributorAuthor

    avro-tools throws a warning for this case:

    WARN avro.Schema: Ignored the com.example.logicalType property ("timestamp-millis"). It should probably be nested inside the "type" for the field

  6. martin-g commented on Feb 23, 2026

    @martin-g
    Member

    Here are some IT tests in the Java SDK which also use the flatten layout:

    Here is a simple Java app that confirms that it supports the flatten way:

    1. pom.xml
    <dependency>
       <groupId>org.apache.avro</groupId>
       <artifactId>avro</artifactId>
       <version>1.12.1</version>
    </dependency>
    1. Java
    import org.apache.avro.Schema;
    import org.apache.avro.SchemaParser;
    
    public class App {
    
      public static final String SCHEMA = """
        {
          "namespace": "org.apache.avro.codegentest.testdata",
          "name": "customEnum",
          "type": "enum",
          "logicalType": "custom-enum",
          "symbols": ["ONE", "TWO", "THREE"]
        }
        """;
    
      void main(String[] args) {
        Schema schema = new SchemaParser()
          .parse(SCHEMA)
          .mainSchema();
        System.out.println(schema);
      }
    }
    1. Output
    {"type":"enum","name":"customEnum","namespace":"org.apache.avro.codegentest.testdata","symbols":["ONE","TWO","THREE"],"logicalType":"custom-enum"}
    

    I cannot tell what avro-tools.jar does differently than SchemaParser.

  7. martin-g commented on Feb 23, 2026

    @martin-g
    Member

    And here is a Python script that confirms the same:

    1. pyproject
    [project]
    name = "avrotest"
    version = "0.1.0"
    description = "Add your description here"
    readme = "README.md"
    requires-python = ">=3.12"
    dependencies = [
        "avro>=1.12.1",
    ]
    1. main.py
    import avro.schema
    
    def main():
      schema = avro.schema.parse(open("/tmp/schema.avsc", "rb").read())
      print(schema)
    
    if __name__ == "__main__":
        main()
    1. Output
    /private/tmp/avrotest/avrotest/.venv/lib/python3.12/site-packages/avro/schema.py:1233: IgnoredLogicalType: Unknown custom-enum, using enum.
      warnings.warn(avro.errors.IgnoredLogicalType(f"Unknown {logical_type}, using {type_}."))
    {"type": "enum", "logicalType": "custom-enum", "name": "customEnum", "namespace": "org.apache.avro.codegentest.testdata", "symbols": ["ONE", "TWO", "THREE"]}
    
  8. Kriskras99 commented on Feb 23, 2026

    @Kriskras99
    Contributor

    The two Java tests you linked, don't actually use the flattened layout.
    But it does indeed seem we need to accept flattened types (but not accept logical type).

  9. Kriskras99 commented on Feb 23, 2026

    @Kriskras99
    Contributor

    A type can only be flattened in a record field right?
    This is a type definition:

    {
        "namespace": "org.apache.avro.codegentest.testdata",
        "name": "FixedType",
        "type": "fixed",
        "size": 12,
        "logicalType": "decimal",
        "precision": 28,
        "scale": 15
    }

    If we would add that to a record the correct way:

    {
      "namespace": "org.apache.avro.codegentest.testdata",
      "type": "record",
      "name": "NestedLogicalTypesUnionFixedDecimal",
      "doc" : "Test nested types with logical types in generated Java classes",
      "fields": [
        {
          "name": "unionOfFixedDecimal",
          "type": {
            "namespace": "org.apache.avro.codegentest.testdata",
            "name": "FixedType",
            "type": "fixed",
            "size": 12,
            "logicalType": "decimal",
            "precision": 28,
            "scale": 15
          }
        }]
    }

    And flattened would look like this:

    {
      "namespace": "org.apache.avro.codegentest.testdata",
      "type": "record",
      "name": "NestedLogicalTypesUnionFixedDecimal",
      "doc" : "Test nested types with logical types in generated Java classes",
      "fields": [
        {
          "namespace": "org.apache.avro.codegentest.testdata",
          "name": "FixedType",
          "type": "fixed",
          "size": 12,
          "logicalType": "decimal",
          "precision": 28,
          "scale": 15
        }]
    }

    In the flattened case there are a few possible confusions:

    1. The name field is both used for the field name and the type name
    2. Custom attributes can be put both on the type and on the field
    3. logicalType is ignored in the Python (and probably Java) implementations

    In the test you linked, the type is a union which contains a fixed type. Which is correct.

  10. woile commented on Feb 23, 2026

    @woile
    ContributorAuthor

    Would you say this is a bug in avro-tools then?

  11. martin-g commented on Feb 23, 2026

    @martin-g
    Member

    No. I misunderstood the problem.
    @Kriskras99 explained it at #476 (comment)

  12. Kriskras99 commented on Feb 23, 2026

    @Kriskras99
    Contributor

    I just double checked with the python tooling and this fails:

    avro.schema.parse('{"type": "record", "name": "flattend", "fields": [{"name": "FixedType", "type": "fixed", "size": 12, "logicalType": "decimal", "precision": 28, "scale": 15}]}')

    So #479 is correct.

  13. KitFieldhouse commented on Mar 6, 2026

    @KitFieldhouse

    I think there might be a misunderstanding here on what the issue is.

    My understanding of how the java and python tools work is that for the example in the original post:

    {
            "type": "record",
            "name": "ExampleEnum",
            "namespace": "com.schema",
            "fields": [
                {
                "name": "wrong_enum",
                "type": "enum",
                "symbols": ["INSERT", "UPDATE"]
                }
            ]
    }
    

    this is interpreted not as an attempt to have a flattened type definition, but is instead interpreted as saying that the field "wrong_enum" is an instance of a named type that has the literal name "enum". I believe the extra JSON field (in this case "symbols" ) is effectively ignored.

    This stems from one of the unfortunate aspects (in my opinion) of the current avro specification, which is that it allows you to have a named type that has a name equal to that of a complex built-in type (but at least mandates that you cannot "redefine" primitive names in this way). Quoting the spec:

    Primitive type names (null, boolean, int, long, float, double, bytes, string) have no namespace and their names may not be defined in any namespace.

    Complex types (record, enum, array, map, fixed) have no namespace, but their names (as well as union) are permitted to be reused as type names. This can be confusing to the human reader, but is always unambiguous for binary serialization. Due to the limitations of JSON encoding, it is a best practice to use a namespace when using these names.

    A schema or protocol may not contain multiple definitions of a fullname. Further, a name must be defined before it is used (“before” in the depth-first, left-to-right traversal of the JSON parse

    For instance, we can make @Kriskras99's example work with the python tooling by defining a named type with name "fixed" earlier in the walk the parser takes. That is, instead of:

    {"type": "record", "name": "flattend", "fields": [
        {"name": "FixedType", "type": "fixed", "size": 12, "logicalType": "decimal", "precision": 28, "scale": 15}
    ]}
    

    we add another field where we have "defined" "fixed" like this:

    {"type": "record", "name": "flattend", "fields": [
        {"name": "FixedTypeDef", "type": {"name": "fixed", "type": "enum", "symbols": ["A", "B"]}},
        {"name": "FixedType", "type": "fixed", "size": 12, "logicalType": "decimal", "precision": 28, "scale": 15}
    ]}
    

    In my testing, python does not reject this schema and treats the second field "FixedType" as the enum we defined in "FixedTypeDef". The java build tool that generates classes based on avro schema also seems to work the exact same way. Here is how I tested the python tooling:

    import avro.schema
    
    schema_str = '''{
        "type": "record",
        "name": "flattened",
        "fields": [
            {
                "name": "FixedTypeDef",
                "type": {"name": "fixed", "type": "enum", "symbols": ["A", "B"]}
            },
            {
                "name": "FixedType",
                "type": "fixed",
                "size": 12,
                "logicalType": "decimal",
                "precision": 28,
                "scale": 15
            }
        ]
    }'''
    
    schema = avro.schema.parse(schema_str)
    
    fixed_def_type_field = schema.fields[0]
    print(f"Field name: {fixed_def_type_field.name}")
    print(f"Field type: {fixed_def_type_field.type}")
    print(f"Field type's type property: {fixed_def_type_field.type.type}")
    print(f"Field type's name: {fixed_def_type_field.type.name}")
    
    fixed_type_field = schema.fields[1]
    print(f"Field name: {fixed_type_field.name}")
    print(f"Field type: {fixed_type_field.type}")
    print(f"Field type's type property: {fixed_type_field.type.type}")
    print(f"Field type's name: {fixed_type_field.type.name}")

    So I think the issue in the original python example is not that it rejects the schema because it sees it as an attempt to "flatten" the definition, it rejects it because it can not find a named schema with the name "fixed".

    To avoid ambiguity of what we should do on the schema field "type", we have to know where we are in the parsing tree. If we are directly inside a schema, a "type" value equal to any of the complex type names indicates that we are defining a a type of that form. Inside "fields" for a record type; "type" indicates that we are looking for a named type with that name, a primitive type with that name, or is a new nested type definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions