confluentinc / schema-registry

Confluent Schema Registry for Kafka
https://docs.confluent.io/current/schema-registry/docs/index.html
Other
2.22k stars 1.11k forks source link

BACKWARD Compatibility Type does not support add optional field #3140

Closed ThinhLe30 closed 5 months ago

ThinhLe30 commented 5 months ago

Here is my case: The registered old schema:

{
  "type": "record",
  "name": "user",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "favorite_number",
      "type": "int"
    }
  ]
}

The new schema which I have added a new optional field:

{
  "type": "record",
  "name": "user",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "favorite_number",
      "type": "int"
    },
    {
      "name": "favorite_color", // the optional field
      "type": "string",
      "default": "green"
    }
  ]
}

Then I use the rest-API to check compatibility: According to these pages: https://docs.confluent.io/platform/current/schema-registry/fundamentals/schema-evolution.html#backward-compatibility https://docs.confluent.io/platform/current/schema-registry/develop/api.html#sr-api-compatibility

http://localhost:8081/compatibility/subjects/my-subject/versions/latest?verbose=true

and the request body:

{
  "schema": "[\"my.namespace.user\"]",
  "references": [
    {
      "name": "my.namespace.user",
      "subject": "seenow.dev.temp.my.namespace.user",
      "version": 1
    }
  ]
}

and the response:

{
    "is_compatible": false,
    "messages": [
        "{errorType:'MISSING_UNION_BRANCH', description:'The new schema is missing a type inside a union field at path '/0' in the old schema', additionalInfo:'reader union lacking writer type: RECORD'}",
        "{oldSchemaVersion: 1}",
        "{oldSchema: '[\"my.namespace.user\"]'}",
        "{compatibility: 'BACKWARD'}"
    ]
}

This seems weird because the BACKWARD compatibility already supports adding an optional field :(( Anybody can explain it to me?w

rayokota commented 5 months ago

An optional field is a union of null and the type, such as "type": ["null", "string"]

ThinhLe30 commented 5 months ago

already try it, it's the same response

ThinhLe30 commented 5 months ago

old schema:

{
    "type": "record",
    "name": "user",
    "fields": [
        {"name": "name", "type": "string"},
        {"name": "favorite_number",  "type": "int"}
    ]
}

new schema:

{
    "type": "record",
    "name": "user",
    "fields": [
        {"name": "name", "type": "string"},
        {"name": "favorite_number",  "type": "int"},
        {"name": "favorite_color", "type": ["null", "string"], "default": null}
    ]
}

and the response: image :(((