-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from debitoor/master
Support schema references for Avro, Protocol Buffer, and JSON schema
- Loading branch information
Showing
13 changed files
with
1,766 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
id: schema-avro | ||
title: Example Avro Schemas | ||
sidebar_label: Example Avro Schemas | ||
--- | ||
|
||
## Schema with references to other schemas | ||
|
||
You might want to split the Avro definition into several schemas one for each type. | ||
|
||
```json | ||
{ | ||
"type" : "record", | ||
"namespace" : "test", | ||
"name" : "A", | ||
"fields" : [ | ||
{ "name" : "id" , "type" : "int" }, | ||
{ "name" : "b" , "type" : "test.B" } | ||
] | ||
} | ||
``` | ||
|
||
```json | ||
{ | ||
"type" : "record", | ||
"namespace" : "test", | ||
"name" : "B", | ||
"fields" : [ | ||
{ "name" : "id" , "type" : "int" } | ||
] | ||
} | ||
``` | ||
|
||
To register schemas with references, the schemas have to be registered in reverse order. The schema that references another schema has to be registered after the schema it references. In this example B has to be registered before A. Furthermore, when registering A, a list of references have to be provided. A reference consist of: | ||
|
||
* `name` - the fully qualified name of the referenced schema. Example: `test.B` | ||
* `subject` - the subject the schema is registered under in the registry | ||
* `version` - the version of the schema you want to use | ||
|
||
The library will handle an arbitrary number of nested levels. | ||
|
||
```js | ||
const schemaA = { | ||
type: 'record', | ||
namespace: 'test', | ||
name: 'A', | ||
fields: [ | ||
{ name: 'id', type: 'int' }, | ||
{ name: 'b', type: 'test.B' }, | ||
], | ||
} | ||
|
||
const schemaB = { | ||
type: 'record', | ||
namespace: 'test', | ||
name: 'B', | ||
fields: [{ name: 'id', type: 'int' }], | ||
} | ||
|
||
await schemaRegistry.register( | ||
{ type: SchemaType.AVRO, schema: JSON.stringify(schemaB) }, | ||
{ subject: 'Avro:B' }, | ||
) | ||
|
||
const response = await schemaRegistry.api.Subject.latestVersion({ subject: 'Avro:B' }) | ||
const { version } = JSON.parse(response.responseData) | ||
|
||
const { id } = await schemaRegistry.register( | ||
{ | ||
type: SchemaType.AVRO, | ||
schema: JSON.stringify(schemaA), | ||
references: [ | ||
{ | ||
name: 'test.B', | ||
subject: 'Avro:B', | ||
version, | ||
}, | ||
], | ||
}, | ||
{ subject: 'Avro:A' }, | ||
) | ||
|
||
const obj = { id: 1, b: { id: 2 } } | ||
|
||
const buffer = await schemaRegistry.encode(id, obj) | ||
const decodedObj = await schemaRegistry.decode(buffer) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
id: schema-json | ||
title: Example JSON Schemas | ||
sidebar_label: Example JSON Schemas | ||
--- | ||
|
||
## Schema with references to other schemas | ||
|
||
You might want to split the JSON definition into several schemas one for each type. | ||
|
||
```JSON | ||
{ | ||
"$id": "https://example.com/schemas/A", | ||
"type": "object", | ||
"properties": { | ||
"id": { "type": "number" }, | ||
"b": { "$ref": "https://example.com/schemas/B" } | ||
} | ||
} | ||
``` | ||
|
||
```JSON | ||
{ | ||
"$id": "https://example.com/schemas/B", | ||
"type": "object", | ||
"properties": { | ||
"id": { "type": "number" } | ||
} | ||
} | ||
``` | ||
|
||
To register schemas with references, the schemas have to be registered in reverse order. The schema that references another schema has to be registered after the schema it references. In this example B has to be registered before A. Furthermore, when registering A, a list of references have to be provided. A reference consist of: | ||
|
||
* `name` - A URL matching the `$ref` from the schema | ||
* `subject` - the subject the schema is registered under in the registry | ||
* `version` - the version of the schema you want to use | ||
|
||
The library will handle an arbitrary number of nested levels. | ||
|
||
```js | ||
const schemaA = { | ||
$id: 'https://example.com/schemas/A', | ||
type: 'object', | ||
properties: { | ||
id: { type: 'number' }, | ||
b: { $ref: 'https://example.com/schemas/B' }, | ||
}, | ||
} | ||
|
||
const schemaB = { | ||
$id: 'https://example.com/schemas/B', | ||
type: 'object', | ||
properties: { | ||
id: { type: 'number' }, | ||
}, | ||
} | ||
|
||
await schemaRegistry.register( | ||
{ type: SchemaType.JSON, schema: JSON.stringify(schemaB) }, | ||
{ subject: 'JSON:B' }, | ||
) | ||
|
||
const response = await schemaRegistry.api.Subject.latestVersion({ subject: 'JSON:B' }) | ||
const { version } = JSON.parse(response.responseData) | ||
|
||
const { id } = await schemaRegistry.register( | ||
{ | ||
type: SchemaType.JSON, | ||
schema: JSON.stringify(schemaA), | ||
references: [ | ||
{ | ||
name: 'https://example.com/schemas/B', | ||
subject: 'JSON:B', | ||
version, | ||
}, | ||
], | ||
}, | ||
{ subject: 'JSON:A' }, | ||
) | ||
|
||
const obj = { id: 1, b: { id: 2 } } | ||
|
||
const buffer = await schemaRegistry.encode(id, obj) | ||
const decodedObj = await schemaRegistry.decode(buffer) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
id: schema-protobuf | ||
title: Example Protobuf Schemas | ||
sidebar_label: Example Protobuf Schemas | ||
--- | ||
|
||
## Schema with references to other schemas | ||
|
||
You might want to split the Protobuf definition into several schemas, one for each type. | ||
|
||
```protobuf | ||
syntax = "proto3"; | ||
package test; | ||
import "test/B.proto"; | ||
message A { | ||
int32 id = 1; | ||
B b = 2; | ||
} | ||
``` | ||
|
||
```protobuf | ||
syntax = "proto3"; | ||
package test; | ||
message B { | ||
int32 id = 1; | ||
} | ||
``` | ||
|
||
To register schemas with references, the schemas have to be registered in reverse order. The schema that references another schema has to be registered after the schema it references. In this example B has to be registered before A. Furthermore, when registering A, a list of references have to be provided. A reference consist of: | ||
|
||
* `name` - String matching the import statement. For example: `test/B.proto` | ||
* `subject` - the subject the schema is registered under in the registry | ||
* `version` - the version of the schema you want to use | ||
|
||
The library will handle an arbitrary number of nested levels. | ||
|
||
```js | ||
const schemaA = ` | ||
syntax = "proto3"; | ||
package test; | ||
import "test/B.proto"; | ||
message A { | ||
int32 id = 1; | ||
B b = 2; | ||
}` | ||
|
||
const schemaB = ` | ||
syntax = "proto3"; | ||
package test; | ||
message B { | ||
int32 id = 1; | ||
}` | ||
|
||
await schemaRegistry.register( | ||
{ type: SchemaType.PROTOBUF, schema: schemaB }, | ||
{ subject: 'Proto:B' }, | ||
) | ||
|
||
const response = await schemaRegistry.api.Subject.latestVersion({ subject: 'Proto:B' }) | ||
const { version } = JSON.parse(response.responseData) | ||
|
||
const { id } = await schemaRegistry.register( | ||
{ | ||
type: SchemaType.PROTOBUF, | ||
schema: schemaA, | ||
references: [ | ||
{ | ||
name: 'test/B.proto', | ||
subject: 'Proto:B', | ||
version, | ||
}, | ||
], | ||
}, | ||
{ subject: 'Proto:A' }, | ||
) | ||
|
||
const obj = { id: 1, b: { id: 2 } } | ||
|
||
const buffer = await schemaRegistry.encode(id, obj) | ||
const decodedObj = await schemaRegistry.decode(buffer) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.