Skip to content

Commit 20cc96d

Browse files
committed
fix(json reference): fix resolving json reference after patching which caused a nil pointer panic
1 parent b40be93 commit 20cc96d

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

config/dynamic/resolve.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ func resolveUrl(ref string, cfg *Config) (*url.URL, error) {
204204
}
205205

206206
info := cfg.Info.Kernel()
207+
if info.Url == nil {
208+
return u, nil
209+
}
210+
207211
if len(info.Url.Opaque) > 0 {
208212
p := filepath.Join(filepath.Dir(info.Url.Opaque), u.Path)
209213
p = fmt.Sprintf("file:%v", p)

providers/asyncapi3/asyncapi3test/message.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,15 @@ func WithHeaders(s *schema.Schema) MessageOptions {
7272
m.Headers = &asyncapi3.SchemaRef{Value: s}
7373
}
7474
}
75+
76+
func WithMessageTitle(s string) MessageOptions {
77+
return func(m *asyncapi3.Message) {
78+
m.Title = s
79+
}
80+
}
81+
82+
func WithMessageSummary(s string) MessageOptions {
83+
return func(m *asyncapi3.Message) {
84+
m.Summary = s
85+
}
86+
}

runtime/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func New(cfg *static.Config, reader dynamic.Reader) *App {
5353
Events: em,
5454
Configs: map[string]*dynamic.Config{},
5555
http: &HttpStore{cfg: cfg, index: index, events: em, reader: reader},
56-
Kafka: &KafkaStore{monitor: m, cfg: cfg, index: index, events: em},
56+
Kafka: &KafkaStore{monitor: m, cfg: cfg, index: index, events: em, reader: reader},
5757
Mqtt: &MqttStore{monitor: m, cfg: cfg, sm: em},
5858
Ldap: &LdapStore{cfg: cfg, events: em, index: index},
5959
Mail: &MailStore{cfg: cfg, sm: em, index: index},

runtime/runtime_kafka_test.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func TestApp_AddKafka_Patching(t *testing.T) {
148148
testcases := []struct {
149149
name string
150150
configs []*dynamic.Config
151+
reader dynamic.Reader
151152
test func(t *testing.T, app *runtime.App)
152153
}{
153154
{
@@ -244,12 +245,86 @@ func TestApp_AddKafka_Patching(t *testing.T) {
244245
require.Equal(t, "patch", msg.Value.Summary)
245246
},
246247
},
248+
{
249+
name: "using relative file path",
250+
reader: &dynamictest.Reader{
251+
Data: map[string]*dynamic.Config{
252+
"bar.yaml": {
253+
Data: &asyncapi3.MessageRef{
254+
Value: asyncapi3test.NewMessage(
255+
asyncapi3test.WithMessageTitle("original"),
256+
),
257+
},
258+
},
259+
},
260+
},
261+
configs: []*dynamic.Config{
262+
getConfig("https://a.io/a", asyncapi3test.NewConfig(
263+
asyncapi3test.WithInfo("foo", "foo", ""),
264+
asyncapi3test.WithChannel("bar",
265+
asyncapi3test.UseMessage("bar",
266+
&asyncapi3.MessageRef{Reference: dynamic.Reference{Ref: "bar.yaml"}},
267+
),
268+
),
269+
)),
270+
getConfig("https://mokapi.io/b", asyncapi3test.NewConfig(
271+
asyncapi3test.WithInfo("foo", "bar", ""),
272+
)),
273+
},
274+
test: func(t *testing.T, app *runtime.App) {
275+
info := app.Kafka.Get("foo")
276+
ch := info.Channels["bar"]
277+
require.NotNil(t, ch)
278+
msg := ch.Value.Messages["bar"]
279+
require.NotNil(t, msg)
280+
require.Equal(t, "original", msg.Value.Title)
281+
},
282+
},
283+
{
284+
name: "using relative u path",
285+
reader: &dynamictest.Reader{
286+
Data: map[string]*dynamic.Config{
287+
"bar.yaml": {
288+
Data: &asyncapi3.MessageRef{
289+
Value: asyncapi3test.NewMessage(
290+
asyncapi3test.WithMessageTitle("original"),
291+
),
292+
},
293+
},
294+
},
295+
},
296+
configs: []*dynamic.Config{
297+
getConfig("https://a.io/a", asyncapi3test.NewConfig(
298+
asyncapi3test.WithInfo("foo", "foo", ""),
299+
asyncapi3test.WithChannel("bar",
300+
asyncapi3test.UseMessage("bar",
301+
&asyncapi3.MessageRef{Reference: dynamic.Reference{Ref: "bar.yaml"}},
302+
),
303+
),
304+
)),
305+
getConfig("https://mokapi.io/b", asyncapi3test.NewConfig(
306+
asyncapi3test.WithInfo("foo", "bar", ""),
307+
)),
308+
},
309+
test: func(t *testing.T, app *runtime.App) {
310+
info := app.Kafka.Get("foo")
311+
ch := info.Channels["bar"]
312+
require.NotNil(t, ch)
313+
msg := ch.Value.Messages["bar"]
314+
require.NotNil(t, msg)
315+
require.Equal(t, "original", msg.Value.Title)
316+
},
317+
},
247318
}
248319
for _, tc := range testcases {
249320
tc := tc
250321
t.Run(tc.name, func(t *testing.T) {
322+
r := tc.reader
323+
if r == nil {
324+
r = &dynamictest.Reader{}
325+
}
251326
cfg := &static.Config{}
252-
app := runtime.New(cfg, &dynamictest.Reader{})
327+
app := runtime.New(cfg, r)
253328
for _, c := range tc.configs {
254329
_, err := app.Kafka.Add(c, enginetest.NewEngine())
255330
require.NoError(t, err)

0 commit comments

Comments
 (0)