spring-cloud / spring-cloud-schema-registry

A schema registry implementation for Spring Cloud Stream
47 stars 27 forks source link

Introduce schema reference support #30

Open yeralin opened 4 years ago

yeralin commented 4 years ago

Addresses 3rd solution from #29

Looks up optional Schema-Reference header that is suppose to contain referenced subjects in the following format: 1) Schema-Reference: <referenced-subject-1>; <referenced-subject-2>; // without version 2) Schema-Reference: <referenced-subject>+v<version-number>;

Example usage: First request:

$ curl --location --request POST 'http://localhost:8990' \
--header 'Content-Type: application/json' \
--data-raw '{
    "subject": "sub",
    "format": "avro",
    "definition": { # the definition is not in a string format for readability purposes
        "type": "record",
        "namespace": "test",
        "name": "sub",
        "fields": [
            {
                "name": "state",
                "type": "string"
            }
        ]
    }
}'

Response: 201 Created

Second request (referencing test.sub schema):

$ curl --location --request POST 'http://localhost:8990' \
--header 'Content-Type: application/json' \
--header 'Schema-Reference: sub+v1' \ # referencing previously created schema by its subject
--data-raw '{
    "subject": "main",
    "format": "avro",
    "definition": {
        "type": "record",
        "namespace": "test",
        "name": "main",
        "fields": [
            {
                "name": "subField",
                "type": "test.sub"
            }
        ]
    }
}'

Response: 201 Created

Explanation: while creating the second schema, it: 1) Parses Schema-Reference header 2) Looks up sub subject with version 1 in the repository 3) Creates AVRO parser 4) Parses looked up sub subject's schema definition 5) Parses passed main subject's schema definition (since it parsed sub schema, now it successfully validates test.sub type)