schema_registry_client 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/lib/schema_registry_client/avro_schema_store.rb +10 -10
- data/lib/schema_registry_client/cached_confluent_schema_registry.rb +6 -6
- data/lib/schema_registry_client/confluent_schema_registry.rb +13 -13
- data/lib/schema_registry_client/output/json_schema.rb +13 -13
- data/lib/schema_registry_client/output/proto_text.rb +46 -46
- data/lib/schema_registry_client/schema/avro.rb +9 -9
- data/lib/schema_registry_client/schema/base.rb +5 -5
- data/lib/schema_registry_client/schema/proto_json_schema.rb +4 -4
- data/lib/schema_registry_client/schema/protobuf.rb +22 -22
- data/lib/schema_registry_client/version.rb +2 -2
- data/lib/schema_registry_client/wire.rb +1 -1
- data/lib/schema_registry_client.rb +138 -134
- data/schema_registry_client.gemspec +20 -20
- data/spec/decoding_spec.rb +54 -54
- data/spec/encoding_spec.rb +91 -91
- data/spec/gen/everything/everything_pb.rb +10 -10
- data/spec/gen/referenced/referer_pb.rb +8 -8
- data/spec/gen/simple/simple_pb.rb +3 -3
- data/spec/json_schema_spec.rb +2 -2
- data/spec/proto_text_spec.rb +1 -1
- data/spec/spec_helper.rb +5 -5
- metadata +1 -1
data/spec/decoding_spec.rb
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
RSpec.describe
|
|
3
|
+
RSpec.describe "decoding" do
|
|
4
4
|
let(:schema_registry_client) do
|
|
5
|
-
SchemaRegistry.new(
|
|
6
|
-
registry_url:
|
|
5
|
+
SchemaRegistry::Client.new(
|
|
6
|
+
registry_url: "http://localhost:8081"
|
|
7
7
|
)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
it
|
|
10
|
+
it "should decode a simple message" do
|
|
11
11
|
schema = File.read("#{__dir__}/schemas/simple/simple.proto")
|
|
12
|
-
stub = stub_request(:get,
|
|
13
|
-
|
|
14
|
-
msg = Simple::V1::SimpleMessage.new(name:
|
|
12
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/15")
|
|
13
|
+
.to_return_json(body: {schema: schema})
|
|
14
|
+
msg = Simple::V1::SimpleMessage.new(name: "my name")
|
|
15
15
|
encoded = "\u0000\u0000\u0000\u0000\u000F\u0000#{msg.to_proto}"
|
|
16
16
|
expect(schema_registry_client.decode(encoded)).to eq(msg)
|
|
17
17
|
|
|
@@ -21,12 +21,12 @@ RSpec.describe 'decoding' do
|
|
|
21
21
|
expect(stub).to have_been_requested.once
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
it
|
|
24
|
+
it "should decode a complex message" do
|
|
25
25
|
schema = File.read("#{__dir__}/schemas/referenced/referer.proto")
|
|
26
|
-
stub = stub_request(:get,
|
|
27
|
-
|
|
26
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/20")
|
|
27
|
+
.to_return_json(body: {schema: schema})
|
|
28
28
|
msg = Referenced::V1::MessageB::MessageBA.new(
|
|
29
|
-
simple: Simple::V1::SimpleMessage.new(name:
|
|
29
|
+
simple: Simple::V1::SimpleMessage.new(name: "my name")
|
|
30
30
|
)
|
|
31
31
|
encoded = "\u0000\u0000\u0000\u0000\u0014\u0004\u0002\u0000#{msg.to_proto}"
|
|
32
32
|
expect(schema_registry_client.decode(encoded)).to eq(msg)
|
|
@@ -36,20 +36,20 @@ RSpec.describe 'decoding' do
|
|
|
36
36
|
expect(stub).to have_been_requested.once
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
describe
|
|
39
|
+
describe "with JSON" do
|
|
40
40
|
let(:schema_registry_client) do
|
|
41
|
-
SchemaRegistry.new(
|
|
42
|
-
registry_url:
|
|
41
|
+
SchemaRegistry::Client.new(
|
|
42
|
+
registry_url: "http://localhost:8081",
|
|
43
43
|
schema_type: SchemaRegistry::Schema::ProtoJsonSchema
|
|
44
44
|
)
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
it
|
|
47
|
+
it "should decode a simple message" do
|
|
48
48
|
schema = File.read("#{__dir__}/schemas/simple/simple.json")
|
|
49
|
-
stub = stub_request(:get,
|
|
50
|
-
|
|
49
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/15")
|
|
50
|
+
.to_return_json(body: {schema: schema})
|
|
51
51
|
encoded = "\u0000\u0000\u0000\u0000\u000F{\"name\":\"my name\"}"
|
|
52
|
-
msg = {
|
|
52
|
+
msg = {"name" => "my name"}
|
|
53
53
|
expect(schema_registry_client.decode(encoded)).to eq(msg)
|
|
54
54
|
|
|
55
55
|
# if we do it again we should not see any more requests
|
|
@@ -59,11 +59,11 @@ RSpec.describe 'decoding' do
|
|
|
59
59
|
end
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
describe
|
|
62
|
+
describe "with Avro" do
|
|
63
63
|
let(:schema_registry_client) do
|
|
64
64
|
SchemaRegistry.avro_schema_path = "#{__dir__}/schemas"
|
|
65
|
-
SchemaRegistry.new(
|
|
66
|
-
registry_url:
|
|
65
|
+
SchemaRegistry::Client.new(
|
|
66
|
+
registry_url: "http://localhost:8081",
|
|
67
67
|
schema_type: SchemaRegistry::Schema::Avro
|
|
68
68
|
)
|
|
69
69
|
end
|
|
@@ -72,16 +72,16 @@ RSpec.describe 'decoding' do
|
|
|
72
72
|
SchemaRegistry.avro_schema_path = nil
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
it
|
|
75
|
+
it "should decode a simple message" do
|
|
76
76
|
schema = File.read("#{__dir__}/schemas/simple/v1/SimpleMessage.avsc")
|
|
77
|
-
stub = stub_request(:get,
|
|
78
|
-
|
|
77
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/15")
|
|
78
|
+
.to_return_json(body: {schema: schema})
|
|
79
79
|
|
|
80
80
|
# Avro-encoded data: "my name" as string (length 0x0E + bytes)
|
|
81
81
|
encoded = "\u0000\u0000\u0000\u0000\u000F\u000Emy name"
|
|
82
82
|
decoded = schema_registry_client.decode(encoded)
|
|
83
83
|
|
|
84
|
-
expect(decoded).to eq({
|
|
84
|
+
expect(decoded).to eq({"name" => "my name"})
|
|
85
85
|
|
|
86
86
|
# if we do it again we should not see any more requests
|
|
87
87
|
expect(schema_registry_client.decode(encoded)).to eq(decoded)
|
|
@@ -89,40 +89,40 @@ RSpec.describe 'decoding' do
|
|
|
89
89
|
expect(stub).to have_been_requested.once
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
-
it
|
|
92
|
+
it "should decode a complex message with nested record" do
|
|
93
93
|
schema = File.read("#{__dir__}/schemas/referenced/v1/MessageBA.avsc")
|
|
94
|
-
stub = stub_request(:get,
|
|
95
|
-
|
|
94
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/20")
|
|
95
|
+
.to_return_json(body: {schema: schema})
|
|
96
96
|
|
|
97
97
|
# Avro-encoded nested record
|
|
98
98
|
encoded = "\u0000\u0000\u0000\u0000\u0014\u000Emy name"
|
|
99
99
|
decoded = schema_registry_client.decode(encoded)
|
|
100
100
|
|
|
101
101
|
expect(decoded).to eq({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
"simple" => {
|
|
103
|
+
"name" => "my name"
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
106
|
|
|
107
107
|
# if we do it again we should not see any more requests
|
|
108
108
|
expect(schema_registry_client.decode(encoded)).to eq(decoded)
|
|
109
109
|
expect(stub).to have_been_requested.once
|
|
110
110
|
end
|
|
111
111
|
|
|
112
|
-
it
|
|
112
|
+
it "should decode a message with multiple fields" do
|
|
113
113
|
multi_schema = {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
{
|
|
119
|
-
{
|
|
114
|
+
"type" => "record",
|
|
115
|
+
"name" => "MultiFieldMessage",
|
|
116
|
+
"namespace" => "test.v1",
|
|
117
|
+
"fields" => [
|
|
118
|
+
{"name" => "name", "type" => "string"},
|
|
119
|
+
{"name" => "age", "type" => "int"}
|
|
120
120
|
]
|
|
121
121
|
}
|
|
122
122
|
schema_json = JSON.generate(multi_schema)
|
|
123
123
|
|
|
124
|
-
stub = stub_request(:get,
|
|
125
|
-
|
|
124
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/25")
|
|
125
|
+
.to_return_json(body: {schema: schema_json})
|
|
126
126
|
|
|
127
127
|
# Manually encode the message for testing
|
|
128
128
|
# Alice = 0x0A (length 5*2) + "Alice" bytes
|
|
@@ -130,39 +130,39 @@ RSpec.describe 'decoding' do
|
|
|
130
130
|
encoded = "\u0000\u0000\u0000\u0000\u0019\u000AAlice\u003C"
|
|
131
131
|
decoded = schema_registry_client.decode(encoded)
|
|
132
132
|
|
|
133
|
-
expect(decoded).to eq({
|
|
133
|
+
expect(decoded).to eq({"name" => "Alice", "age" => 30})
|
|
134
134
|
|
|
135
135
|
expect(stub).to have_been_requested.once
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
-
it
|
|
138
|
+
it "should handle schema evolution with reader schema" do
|
|
139
139
|
# Writer schema (what was used to encode) - has an additional field with default
|
|
140
140
|
writer_schema = {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
{
|
|
146
|
-
{
|
|
141
|
+
"type" => "record",
|
|
142
|
+
"name" => "SimpleMessage",
|
|
143
|
+
"namespace" => "simple.v1",
|
|
144
|
+
"fields" => [
|
|
145
|
+
{"name" => "name", "type" => "string"},
|
|
146
|
+
{"name" => "age", "type" => "int", "default" => 0}
|
|
147
147
|
]
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
# Reader schema (what we have locally) - doesn't have the age field
|
|
151
151
|
# This simulates reading old data with a newer schema or vice versa
|
|
152
152
|
|
|
153
|
-
stub = stub_request(:get,
|
|
154
|
-
|
|
153
|
+
stub = stub_request(:get, "http://localhost:8081/schemas/ids/15")
|
|
154
|
+
.to_return_json(body: {schema: JSON.generate(writer_schema)})
|
|
155
155
|
|
|
156
156
|
# Encoded with writer schema: "my name" (0x0E + bytes) + age 25 (zigzag encoded as 50 = 0x32)
|
|
157
157
|
encoded = "\u0000\u0000\u0000\u0000\u000F\u000Emy name\u0032"
|
|
158
158
|
decoded = schema_registry_client.decode(encoded)
|
|
159
159
|
|
|
160
160
|
# Decoded value should only have 'name' from reader schema, age is ignored
|
|
161
|
-
expect(decoded).to eq({
|
|
161
|
+
expect(decoded).to eq({"name" => "my name"})
|
|
162
162
|
expect(stub).to have_been_requested.once
|
|
163
163
|
end
|
|
164
164
|
|
|
165
|
-
it
|
|
165
|
+
it "should raise error for invalid magic byte" do
|
|
166
166
|
# Wrong magic byte (0x01 instead of 0x00)
|
|
167
167
|
encoded = "\u0001\u0000\u0000\u0000\u000F\u000Emy name"
|
|
168
168
|
|
|
@@ -171,7 +171,7 @@ RSpec.describe 'decoding' do
|
|
|
171
171
|
end.to raise_error(/Expected data to begin with a magic byte/)
|
|
172
172
|
end
|
|
173
173
|
|
|
174
|
-
it
|
|
174
|
+
it "should raise error for unknown schema id" do
|
|
175
175
|
# Schema ID 999 is not stubbed, so decoding should fail
|
|
176
176
|
encoded = "\u0000\u0000\u0000\u0003\u00E7\u000Emy name"
|
|
177
177
|
|
data/spec/encoding_spec.rb
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
RSpec.describe
|
|
3
|
+
RSpec.describe "encoding" do
|
|
4
4
|
let(:schema_registry_client) do
|
|
5
|
-
SchemaRegistry.new(
|
|
6
|
-
registry_url:
|
|
5
|
+
SchemaRegistry::Client.new(
|
|
6
|
+
registry_url: "http://localhost:8081"
|
|
7
7
|
)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
it
|
|
10
|
+
it "should encode a simple message" do
|
|
11
11
|
schema = File.read("#{__dir__}/schemas/simple/simple.proto")
|
|
12
|
-
stub = stub_request(:post,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
msg = Simple::V1::SimpleMessage.new(name:
|
|
17
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
12
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/simple/versions")
|
|
13
|
+
.with(body: {"schemaType" => "PROTOBUF",
|
|
14
|
+
"references" => [],
|
|
15
|
+
"schema" => schema}).to_return_json(body: {id: 15})
|
|
16
|
+
msg = Simple::V1::SimpleMessage.new(name: "my name")
|
|
17
|
+
encoded = schema_registry_client.encode(msg, subject: "simple")
|
|
18
18
|
expect(encoded).to eq("\u0000\u0000\u0000\u0000\u000F\u0000#{msg.to_proto}")
|
|
19
19
|
|
|
20
20
|
# if we do it again we should not see any more requests
|
|
21
|
-
encoded2 = schema_registry_client.encode(msg, subject:
|
|
21
|
+
encoded2 = schema_registry_client.encode(msg, subject: "simple")
|
|
22
22
|
expect(encoded2).to eq(encoded)
|
|
23
23
|
|
|
24
24
|
expect(stub).to have_been_requested.once
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
it
|
|
27
|
+
it "should encode a complex message" do
|
|
28
28
|
schema = File.read("#{__dir__}/schemas/referenced/referer.proto")
|
|
29
29
|
dep_schema = File.read("#{__dir__}/schemas/simple/simple.proto")
|
|
30
|
-
dep_stub = stub_request(:post,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
version_stub = stub_request(:get,
|
|
35
|
-
|
|
36
|
-
stub = stub_request(:post,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
30
|
+
dep_stub = stub_request(:post, "http://localhost:8081/subjects/simple%2Fsimple.proto/versions")
|
|
31
|
+
.with(body: {"schemaType" => "PROTOBUF",
|
|
32
|
+
"references" => [],
|
|
33
|
+
"schema" => dep_schema}).to_return_json(body: {id: 15})
|
|
34
|
+
version_stub = stub_request(:get, "http://localhost:8081/schemas/ids/15/versions")
|
|
35
|
+
.to_return_json(body: [{version: 1, subject: "simple/simple.proto"}])
|
|
36
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/referenced/versions")
|
|
37
|
+
.with(body: {"schemaType" => "PROTOBUF",
|
|
38
|
+
"references" => [
|
|
39
|
+
{
|
|
40
|
+
name: "simple/simple.proto",
|
|
41
|
+
subject: "simple/simple.proto",
|
|
42
|
+
version: 1
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
"schema" => schema}).to_return_json(body: {id: 20})
|
|
46
46
|
msg = Referenced::V1::MessageB::MessageBA.new(
|
|
47
|
-
simple: Simple::V1::SimpleMessage.new(name:
|
|
47
|
+
simple: Simple::V1::SimpleMessage.new(name: "my name")
|
|
48
48
|
)
|
|
49
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
49
|
+
encoded = schema_registry_client.encode(msg, subject: "referenced")
|
|
50
50
|
expect(encoded).to eq("\u0000\u0000\u0000\u0000\u0014\u0004\u0002\u0000#{msg.to_proto}")
|
|
51
51
|
|
|
52
52
|
# if we do it again we should not see any more requests
|
|
53
|
-
encoded2 = schema_registry_client.encode(msg, subject:
|
|
53
|
+
encoded2 = schema_registry_client.encode(msg, subject: "referenced")
|
|
54
54
|
expect(encoded2).to eq(encoded)
|
|
55
55
|
expect(stub).to have_been_requested.once
|
|
56
56
|
expect(dep_stub).to have_been_requested.once
|
|
57
57
|
expect(version_stub).to have_been_requested.once
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
-
describe
|
|
60
|
+
describe "with JSON" do
|
|
61
61
|
let(:schema_registry_client) do
|
|
62
|
-
SchemaRegistry.new(
|
|
63
|
-
registry_url:
|
|
62
|
+
SchemaRegistry::Client.new(
|
|
63
|
+
registry_url: "http://localhost:8081",
|
|
64
64
|
schema_type: SchemaRegistry::Schema::ProtoJsonSchema
|
|
65
65
|
)
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
-
it
|
|
68
|
+
it "should encode a simple message" do
|
|
69
69
|
schema = File.read("#{__dir__}/schemas/simple/simple.json").strip
|
|
70
|
-
stub = stub_request(:post,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
msg = Simple::V1::SimpleMessage.new(name:
|
|
75
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
70
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/simple/versions")
|
|
71
|
+
.with(body: {"schemaType" => "JSON",
|
|
72
|
+
"references" => [],
|
|
73
|
+
"schema" => schema}).to_return_json(body: {id: 15})
|
|
74
|
+
msg = Simple::V1::SimpleMessage.new(name: "my name")
|
|
75
|
+
encoded = schema_registry_client.encode(msg, subject: "simple")
|
|
76
76
|
expect(encoded).to eq("\u0000\u0000\u0000\u0000\u000F{\"name\":\"my name\"}")
|
|
77
77
|
|
|
78
78
|
# if we do it again we should not see any more requests
|
|
79
|
-
encoded2 = schema_registry_client.encode(msg, subject:
|
|
79
|
+
encoded2 = schema_registry_client.encode(msg, subject: "simple")
|
|
80
80
|
expect(encoded2).to eq(encoded)
|
|
81
81
|
|
|
82
82
|
expect(stub).to have_been_requested.once
|
|
83
83
|
end
|
|
84
84
|
|
|
85
|
-
it
|
|
85
|
+
it "should encode a complex message" do
|
|
86
86
|
schema = File.read("#{__dir__}/schemas/referenced/referenced.json").strip
|
|
87
|
-
stub = stub_request(:post,
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/referenced/versions")
|
|
88
|
+
.with(body: {"schemaType" => "JSON",
|
|
89
|
+
"references" => [],
|
|
90
|
+
"schema" => schema}).to_return_json(body: {id: 20})
|
|
91
91
|
msg = Referenced::V1::MessageB::MessageBA.new(
|
|
92
|
-
simple: Simple::V1::SimpleMessage.new(name:
|
|
92
|
+
simple: Simple::V1::SimpleMessage.new(name: "my name")
|
|
93
93
|
)
|
|
94
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
94
|
+
encoded = schema_registry_client.encode(msg, subject: "referenced")
|
|
95
95
|
expect(encoded).to eq("\u0000\u0000\u0000\u0000\u0014{\"simple\":{\"name\":\"my name\"}}")
|
|
96
96
|
|
|
97
97
|
# if we do it again we should not see any more requests
|
|
98
|
-
encoded2 = schema_registry_client.encode(msg, subject:
|
|
98
|
+
encoded2 = schema_registry_client.encode(msg, subject: "referenced")
|
|
99
99
|
expect(encoded2).to eq(encoded)
|
|
100
100
|
expect(stub).to have_been_requested.once
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
|
|
104
|
-
describe
|
|
104
|
+
describe "with Avro" do
|
|
105
105
|
let(:schema_registry_client) do
|
|
106
106
|
SchemaRegistry.avro_schema_path = "#{__dir__}/schemas"
|
|
107
|
-
SchemaRegistry.new(
|
|
108
|
-
registry_url:
|
|
107
|
+
SchemaRegistry::Client.new(
|
|
108
|
+
registry_url: "http://localhost:8081",
|
|
109
109
|
schema_type: SchemaRegistry::Schema::Avro
|
|
110
110
|
)
|
|
111
111
|
end
|
|
@@ -114,74 +114,74 @@ RSpec.describe 'encoding' do
|
|
|
114
114
|
SchemaRegistry.avro_schema_path = nil
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
-
it
|
|
117
|
+
it "should encode a simple message" do
|
|
118
118
|
schema = File.read("#{__dir__}/schemas/simple/v1/SimpleMessage.avsc")
|
|
119
|
-
stub = stub_request(:post,
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
msg = {
|
|
124
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
119
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/simple/versions")
|
|
120
|
+
.with(body: {"schemaType" => "AVRO",
|
|
121
|
+
"references" => [],
|
|
122
|
+
"schema" => schema}).to_return_json(body: {id: 15})
|
|
123
|
+
msg = {"name" => "my name"}
|
|
124
|
+
encoded = schema_registry_client.encode(msg, subject: "simple", schema_name: "simple.v1.SimpleMessage")
|
|
125
125
|
# Avro encoding: magic byte (0x00) + schema id (4 bytes, big-endian) + Avro binary data
|
|
126
126
|
# "my name" encoded as Avro string: length (0x0E = 14) + "my name" bytes
|
|
127
127
|
expect(encoded).to eq("\u0000\u0000\u0000\u0000\u000F\u000Emy name")
|
|
128
128
|
|
|
129
129
|
# if we do it again we should not see any more requests
|
|
130
|
-
encoded2 = schema_registry_client.encode(msg, subject:
|
|
130
|
+
encoded2 = schema_registry_client.encode(msg, subject: "simple", schema_name: "simple.v1.SimpleMessage")
|
|
131
131
|
expect(encoded2).to eq(encoded)
|
|
132
132
|
|
|
133
133
|
expect(stub).to have_been_requested.once
|
|
134
134
|
end
|
|
135
135
|
|
|
136
|
-
it
|
|
136
|
+
it "should encode a complex message with nested record" do
|
|
137
137
|
schema = File.read("#{__dir__}/schemas/referenced/v1/MessageBA.avsc")
|
|
138
|
-
stub = stub_request(:post,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
138
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/referenced/versions")
|
|
139
|
+
.with(body: {"schemaType" => "AVRO",
|
|
140
|
+
"references" => [],
|
|
141
|
+
"schema" => schema}).to_return_json(body: {id: 20})
|
|
142
142
|
msg = {
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
"simple" => {
|
|
144
|
+
"name" => "my name"
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
147
|
+
encoded = schema_registry_client.encode(msg, subject: "referenced", schema_name: "referenced.v1.MessageBA")
|
|
148
148
|
# Avro encoding: magic byte + schema id + Avro binary for nested record
|
|
149
149
|
expect(encoded).to eq("\u0000\u0000\u0000\u0000\u0014\u000Emy name")
|
|
150
150
|
|
|
151
151
|
# if we do it again we should not see any more requests
|
|
152
|
-
encoded2 = schema_registry_client.encode(msg, subject:
|
|
152
|
+
encoded2 = schema_registry_client.encode(msg, subject: "referenced", schema_name: "referenced.v1.MessageBA")
|
|
153
153
|
expect(encoded2).to eq(encoded)
|
|
154
154
|
expect(stub).to have_been_requested.once
|
|
155
155
|
end
|
|
156
156
|
|
|
157
|
-
it
|
|
157
|
+
it "should handle multiple fields" do
|
|
158
158
|
# Create a temporary schema file for testing
|
|
159
159
|
multi_schema_path = "#{__dir__}/schemas/test/v1"
|
|
160
160
|
FileUtils.mkdir_p(multi_schema_path)
|
|
161
161
|
|
|
162
162
|
multi_schema = {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
{
|
|
168
|
-
{
|
|
163
|
+
"type" => "record",
|
|
164
|
+
"name" => "MultiFieldMessage",
|
|
165
|
+
"namespace" => "test.v1",
|
|
166
|
+
"fields" => [
|
|
167
|
+
{"name" => "name", "type" => "string"},
|
|
168
|
+
{"name" => "age", "type" => "int"}
|
|
169
169
|
]
|
|
170
170
|
}
|
|
171
171
|
schema_json = JSON.pretty_generate(multi_schema)
|
|
172
172
|
File.write("#{multi_schema_path}/MultiFieldMessage.avsc", schema_json)
|
|
173
173
|
|
|
174
|
-
stub = stub_request(:post,
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
174
|
+
stub = stub_request(:post, "http://localhost:8081/subjects/multi/versions")
|
|
175
|
+
.with(body: {"schemaType" => "AVRO",
|
|
176
|
+
"references" => [],
|
|
177
|
+
"schema" => schema_json}).to_return_json(body: {id: 25})
|
|
178
178
|
|
|
179
|
-
msg = {
|
|
180
|
-
encoded = schema_registry_client.encode(msg, subject:
|
|
179
|
+
msg = {"name" => "Alice", "age" => 30}
|
|
180
|
+
encoded = schema_registry_client.encode(msg, subject: "multi", schema_name: "test.v1.MultiFieldMessage")
|
|
181
181
|
|
|
182
182
|
# Verify encoding starts with magic byte and schema id
|
|
183
183
|
expect(encoded[0]).to eq("\u0000")
|
|
184
|
-
expect(encoded[1..4].unpack1(
|
|
184
|
+
expect(encoded[1..4].unpack1("N")).to eq(25)
|
|
185
185
|
|
|
186
186
|
expect(stub).to have_been_requested.once
|
|
187
187
|
|
|
@@ -189,18 +189,18 @@ RSpec.describe 'encoding' do
|
|
|
189
189
|
FileUtils.rm_rf("#{__dir__}/schemas/test")
|
|
190
190
|
end
|
|
191
191
|
|
|
192
|
-
it
|
|
192
|
+
it "should validate schema before encoding" do
|
|
193
193
|
schema = File.read("#{__dir__}/schemas/simple/v1/SimpleMessage.avsc")
|
|
194
|
-
stub_request(:post,
|
|
195
|
-
.with(body: {
|
|
196
|
-
|
|
197
|
-
|
|
194
|
+
stub_request(:post, "http://localhost:8081/subjects/simple/versions")
|
|
195
|
+
.with(body: {"schemaType" => "AVRO",
|
|
196
|
+
"references" => [],
|
|
197
|
+
"schema" => schema}).to_return_json(body: {id: 15})
|
|
198
198
|
|
|
199
199
|
# Invalid message - missing required field
|
|
200
|
-
msg = {
|
|
200
|
+
msg = {"invalid_field" => "value"}
|
|
201
201
|
|
|
202
202
|
expect do
|
|
203
|
-
schema_registry_client.encode(msg, subject:
|
|
203
|
+
schema_registry_client.encode(msg, subject: "simple", schema_name: "simple.v1.SimpleMessage")
|
|
204
204
|
end.to raise_error(Avro::SchemaValidator::ValidationError)
|
|
205
205
|
end
|
|
206
206
|
end
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
4
4
|
# source: everything/everything.proto
|
|
5
5
|
|
|
6
|
-
require
|
|
6
|
+
require "google/protobuf"
|
|
7
7
|
|
|
8
|
-
require
|
|
9
|
-
require
|
|
8
|
+
require "simple/simple_pb"
|
|
9
|
+
require "google/protobuf/descriptor_pb"
|
|
10
10
|
|
|
11
11
|
descriptor_data = "\n\x1b\x65verything/everything.proto\x12\reverything.v1\x1a\x13simple/simple.proto\x1a google/protobuf/descriptor.proto\"*\n\x0e\x46oreignMessage\x12\n\n\x02id\x18\x01 \x01(\x05\x12\x0c\n\x04name\x18\x02 \x01(\t\"\xc0\x12\n\x0cTestAllTypes\x12\x16\n\x0eoptional_int32\x18\x01 \x01(\x05\x12\x16\n\x0eoptional_int64\x18\x02 \x01(\x03\x12\x17\n\x0foptional_uint32\x18\x03 \x01(\r\x12\x17\n\x0foptional_uint64\x18\x04 \x01(\x04\x12\x17\n\x0foptional_sint32\x18\x05 \x01(\x11\x12\x17\n\x0foptional_sint64\x18\x06 \x01(\x12\x12\x18\n\x10optional_fixed32\x18\x07 \x01(\x07\x12\x18\n\x10optional_fixed64\x18\x08 \x01(\x06\x12\x19\n\x11optional_sfixed32\x18\t \x01(\x0f\x12\x19\n\x11optional_sfixed64\x18\n \x01(\x10\x12\x16\n\x0eoptional_float\x18\x0b \x01(\x02\x12\x17\n\x0foptional_double\x18\x0c \x01(\x01\x12\x15\n\roptional_bool\x18\r \x01(\x08\x12\x17\n\x0foptional_string\x18\x0e \x01(\t\x12\x16\n\x0eoptional_bytes\x18\x0f \x01(\x0c\x12J\n\x17optional_nested_message\x18\x12 \x01(\x0b\x32).everything.v1.TestAllTypes.NestedMessage\x12?\n\x18optional_foreign_message\x18\x13 \x01(\x0b\x32\x1d.everything.v1.ForeignMessage\x12\x39\n\x17optional_import_message\x18\x14 \x01(\x0b\x32\x18.simple.v1.SimpleMessage\x12\x44\n\x14optional_nested_enum\x18\x15 \x01(\x0e\x32&.everything.v1.TestAllTypes.NestedEnum\x12\x39\n\x15optional_foreign_enum\x18\x16 \x01(\x0e\x32\x1a.everything.v1.ForeignEnum\x12\x33\n\x14optional_import_enum\x18\x17 \x01(\x0e\x32\x15.simple.v1.SimpleEnum\x12!\n\x15optional_string_piece\x18\x18 \x01(\tB\x02\x08\x02\x12\x19\n\roptional_cord\x18\x19 \x01(\tB\x02\x08\x01\x12\x1f\n\x13optional_bytes_cord\x18V \x01(\x0c\x42\x02\x08\x01\x12L\n\x15optional_lazy_message\x18\x1b \x01(\x0b\x32).everything.v1.TestAllTypes.NestedMessageB\x02(\x01\x12W\n optional_unverified_lazy_message\x18\x1c \x01(\x0b\x32).everything.v1.TestAllTypes.NestedMessageB\x02x\x01\x12\x16\n\x0erepeated_int32\x18\x1f \x03(\x05\x12\x16\n\x0erepeated_int64\x18 \x03(\x03\x12\x17\n\x0frepeated_uint32\x18! \x03(\r\x12\x17\n\x0frepeated_uint64\x18\" \x03(\x04\x12\x17\n\x0frepeated_sint32\x18# \x03(\x11\x12\x17\n\x0frepeated_sint64\x18$ \x03(\x12\x12\x18\n\x10repeated_fixed32\x18% \x03(\x07\x12\x18\n\x10repeated_fixed64\x18& \x03(\x06\x12\x19\n\x11repeated_sfixed32\x18' \x03(\x0f\x12\x19\n\x11repeated_sfixed64\x18( \x03(\x10\x12\x16\n\x0erepeated_float\x18) \x03(\x02\x12\x17\n\x0frepeated_double\x18* \x03(\x01\x12\x15\n\rrepeated_bool\x18+ \x03(\x08\x12\x17\n\x0frepeated_string\x18, \x03(\t\x12\x16\n\x0erepeated_bytes\x18- \x03(\x0c\x12J\n\x17repeated_nested_message\x18\x30 \x03(\x0b\x32).everything.v1.TestAllTypes.NestedMessage\x12?\n\x18repeated_foreign_message\x18\x31 \x03(\x0b\x32\x1d.everything.v1.ForeignMessage\x12\x39\n\x17repeated_import_message\x18\x32 \x03(\x0b\x32\x18.simple.v1.SimpleMessage\x12\x44\n\x14repeated_nested_enum\x18\x33 \x03(\x0e\x32&.everything.v1.TestAllTypes.NestedEnum\x12\x39\n\x15repeated_foreign_enum\x18\x34 \x03(\x0e\x32\x1a.everything.v1.ForeignEnum\x12\x33\n\x14repeated_import_enum\x18\x35 \x03(\x0e\x32\x15.simple.v1.SimpleEnum\x12!\n\x15repeated_string_piece\x18\x36 \x03(\tB\x02\x08\x02\x12\x19\n\rrepeated_cord\x18\x37 \x03(\tB\x02\x08\x01\x12L\n\x15repeated_lazy_message\x18\x39 \x03(\x0b\x32).everything.v1.TestAllTypes.NestedMessageB\x02(\x01\x12\x16\n\x0coneof_uint32\x18o \x01(\rH\x00\x12I\n\x14oneof_nested_message\x18p \x01(\x0b\x32).everything.v1.TestAllTypes.NestedMessageH\x00\x12\x16\n\x0coneof_string\x18q \x01(\tH\x00\x12\x15\n\x0boneof_bytes\x18r \x01(\x0cH\x00\x12\x18\n\noneof_cord\x18s \x01(\tB\x02\x08\x01H\x00\x12 \n\x12oneof_string_piece\x18t \x01(\tB\x02\x08\x02H\x00\x12R\n\x19oneof_lazy_nested_message\x18u \x01(\x0b\x32).everything.v1.TestAllTypes.NestedMessageB\x02(\x01H\x00\x1a\"\n\rNestedMessage\x12\x11\n\x02\x62\x62\x18\x01 \x01(\x05\x42\x05\x90\x82\x19\xd2\t\x1a\x1a\n\rOptionalGroup\x12\t\n\x01\x61\x18\x11 \x01(\x05\x1a\x1a\n\rRepeatedGroup\x12\t\n\x01\x61\x18/ \x01(\x05\"'\n\nNestedEnum\x12\x07\n\x03\x46OO\x10\x00\x12\x07\n\x03\x42\x41R\x10\x02\x12\x07\n\x03\x42\x41Z\x10\x03\x42\r\n\x0boneof_field*/\n\x0b\x46oreignEnum\x12\x0f\n\x0b\x46OREIGN_FOO\x10\x00\x12\x0f\n\x0b\x46OREIGN_BAR\x10\x01\x32Y\n\x0bTestService\x12J\n\nTestMethod\x12\x1b.everything.v1.TestAllTypes\x1a\x1d.everything.v1.ForeignMessage\"\x00:4\n\x0bsome_option\x12\x1d.google.protobuf.FieldOptions\x18\xa2\x90\x03 \x01(\x05\x62\x06proto3"
|
|
12
12
|
|
|
@@ -15,12 +15,12 @@ pool.add_serialized_file(descriptor_data)
|
|
|
15
15
|
|
|
16
16
|
module Everything
|
|
17
17
|
module V1
|
|
18
|
-
ForeignMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
19
|
-
TestAllTypes = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
20
|
-
TestAllTypes::NestedMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
21
|
-
TestAllTypes::OptionalGroup = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
22
|
-
TestAllTypes::RepeatedGroup = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
23
|
-
TestAllTypes::NestedEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
24
|
-
ForeignEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
18
|
+
ForeignMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.ForeignMessage").msgclass
|
|
19
|
+
TestAllTypes = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.TestAllTypes").msgclass
|
|
20
|
+
TestAllTypes::NestedMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.TestAllTypes.NestedMessage").msgclass
|
|
21
|
+
TestAllTypes::OptionalGroup = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.TestAllTypes.OptionalGroup").msgclass
|
|
22
|
+
TestAllTypes::RepeatedGroup = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.TestAllTypes.RepeatedGroup").msgclass
|
|
23
|
+
TestAllTypes::NestedEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.TestAllTypes.NestedEnum").enummodule
|
|
24
|
+
ForeignEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("everything.v1.ForeignEnum").enummodule
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
4
4
|
# source: referenced/referer.proto
|
|
5
5
|
|
|
6
|
-
require
|
|
6
|
+
require "google/protobuf"
|
|
7
7
|
|
|
8
|
-
require
|
|
8
|
+
require "simple/simple_pb"
|
|
9
9
|
|
|
10
10
|
descriptor_data = "\n\x18referenced/referer.proto\x12\rreferenced.v1\x1a\x13simple/simple.proto\"j\n\x08MessageA\x1a\x43\n\tMessageAA\x12\x0c\n\x04name\x18\x01 \x01(\t\x12(\n\x06simple\x18\x02 \x01(\x0b\x32\x18.simple.v1.SimpleMessage\x1a\x19\n\tMessageAB\x12\x0c\n\x04name\x18\x01 \x01(\t\"j\n\x08MessageB\x1a\x43\n\tMessageBA\x12\x0c\n\x04name\x18\x01 \x01(\t\x12(\n\x06simple\x18\x02 \x01(\x0b\x32\x18.simple.v1.SimpleMessage\x1a\x19\n\tMessageBB\x12\x0c\n\x04name\x18\x01 \x01(\tb\x06proto3"
|
|
11
11
|
|
|
@@ -14,11 +14,11 @@ pool.add_serialized_file(descriptor_data)
|
|
|
14
14
|
|
|
15
15
|
module Referenced
|
|
16
16
|
module V1
|
|
17
|
-
MessageA = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
18
|
-
MessageA::MessageAA = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
19
|
-
MessageA::MessageAB = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
20
|
-
MessageB = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
21
|
-
MessageB::MessageBA = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
22
|
-
MessageB::MessageBB = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
17
|
+
MessageA = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("referenced.v1.MessageA").msgclass
|
|
18
|
+
MessageA::MessageAA = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("referenced.v1.MessageA.MessageAA").msgclass
|
|
19
|
+
MessageA::MessageAB = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("referenced.v1.MessageA.MessageAB").msgclass
|
|
20
|
+
MessageB = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("referenced.v1.MessageB").msgclass
|
|
21
|
+
MessageB::MessageBA = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("referenced.v1.MessageB.MessageBA").msgclass
|
|
22
|
+
MessageB::MessageBB = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("referenced.v1.MessageB.MessageBB").msgclass
|
|
23
23
|
end
|
|
24
24
|
end
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
|
4
4
|
# source: simple/simple.proto
|
|
5
5
|
|
|
6
|
-
require
|
|
6
|
+
require "google/protobuf"
|
|
7
7
|
|
|
8
8
|
descriptor_data = "\n\x13simple/simple.proto\x12\tsimple.v1\"\x1d\n\rSimpleMessage\x12\x0c\n\x04name\x18\x01 \x01(\t*,\n\nSimpleEnum\x12\x0e\n\nSIMPLE_FOO\x10\x00\x12\x0e\n\nSIMPLE_BAR\x10\x01\x62\x06proto3"
|
|
9
9
|
|
|
@@ -12,7 +12,7 @@ pool.add_serialized_file(descriptor_data)
|
|
|
12
12
|
|
|
13
13
|
module Simple
|
|
14
14
|
module V1
|
|
15
|
-
SimpleMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
16
|
-
SimpleEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(
|
|
15
|
+
SimpleMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("simple.v1.SimpleMessage").msgclass
|
|
16
|
+
SimpleEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("simple.v1.SimpleEnum").enummodule
|
|
17
17
|
end
|
|
18
18
|
end
|