Skip to content

Commit eecc609

Browse files
bieluclaude
andcommitted
refactor: resolve bindings reference through Target only
Nothing in AsyncApiBindingsReference reads Target in a way that allows null, so the throw moves into Target and the extra Resolved property goes. Extensions now goes through Target too instead of the unresolved field, so it resolves the reference and fails clearly when it can't. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 42f01a7 commit eecc609

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

‎src/ByteBard.AsyncAPI/Models/References/AsyncApiBindingsReference{TBinding}.cs‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,29 @@ public class AsyncApiBindingsReference<TBinding> : AsyncApiBindings<TBinding>, I
1616

1717
private AsyncApiBindings<TBinding> target;
1818

19-
private AsyncApiBindings<TBinding> Resolved => this.Target ?? throw this.Reference.CreateUnresolvedException();
20-
2119
private AsyncApiBindings<TBinding> Target
2220
{
2321
get
2422
{
2523
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiBindings<TBinding>>(this.Reference);
26-
return this.target;
24+
return this.target ?? throw this.Reference.CreateUnresolvedException();
2725
}
2826
}
2927

3028
public override void Add(TBinding binding)
3129
{
32-
this.Resolved.Add(binding);
30+
this.Target.Add(binding);
3331
}
3432

35-
public override ICollection<string> Keys => this.Resolved.Keys;
33+
public override ICollection<string> Keys => this.Target.Keys;
3634

37-
public override ICollection<TBinding> Values => this.Resolved.Values;
35+
public override ICollection<TBinding> Values => this.Target.Values;
3836

39-
public override IDictionary<string, IAsyncApiExtension> Extensions => this.target.Extensions;
37+
public override IDictionary<string, IAsyncApiExtension> Extensions => this.Target.Extensions;
4038

41-
public override int Count => this.Resolved.Count;
39+
public override int Count => this.Target.Count;
4240

43-
public override bool IsReadOnly => this.Resolved.IsReadOnly;
41+
public override bool IsReadOnly => this.Target.IsReadOnly;
4442

4543
public AsyncApiBindingsReference(string reference)
4644
{
@@ -86,7 +84,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
8684
return;
8785
}
8886

89-
this.Resolved.SerializeV2(writer);
87+
this.Target.SerializeV2(writer);
9088
}
9189

9290
public override void SerializeV3(IAsyncApiWriter writer)
@@ -102,57 +100,57 @@ public override void SerializeV3(IAsyncApiWriter writer)
102100
return;
103101
}
104102

105-
this.Resolved.SerializeV3(writer);
103+
this.Target.SerializeV3(writer);
106104
}
107105

108106
public override void Add(string key, TBinding value)
109107
{
110-
this.Resolved.Add(key, value);
108+
this.Target.Add(key, value);
111109
}
112110

113111
public override bool ContainsKey(string key)
114112
{
115-
return this.Resolved.ContainsKey(key);
113+
return this.Target.ContainsKey(key);
116114
}
117115

118116
public override bool Remove(string key)
119117
{
120-
return this.Resolved.Remove(key);
118+
return this.Target.Remove(key);
121119
}
122120

123121
public override bool TryGetValue(string key, out TBinding value)
124122
{
125-
return this.Resolved.TryGetValue(key, out value);
123+
return this.Target.TryGetValue(key, out value);
126124
}
127125

128126
public override void Add(KeyValuePair<string, TBinding> item)
129127
{
130-
this.Resolved.Add(item);
128+
this.Target.Add(item);
131129
}
132130

133131
public override void Clear()
134132
{
135-
this.Resolved.Clear();
133+
this.Target.Clear();
136134
}
137135

138136
public override bool Contains(KeyValuePair<string, TBinding> item)
139137
{
140-
return this.Resolved.Contains(item);
138+
return this.Target.Contains(item);
141139
}
142140

143141
public override void CopyTo(KeyValuePair<string, TBinding>[] array, int arrayIndex)
144142
{
145-
this.Resolved.CopyTo(array, arrayIndex);
143+
this.Target.CopyTo(array, arrayIndex);
146144
}
147145

148146
public override bool Remove(KeyValuePair<string, TBinding> item)
149147
{
150-
return this.Resolved.Remove(item);
148+
return this.Target.Remove(item);
151149
}
152150

153151
public override IEnumerator<KeyValuePair<string, TBinding>> GetEnumerator()
154152
{
155-
return this.Resolved.GetEnumerator();
153+
return this.Target.GetEnumerator();
156154
}
157155
}
158156
}

‎test/ByteBard.AsyncAPI.Tests/Models/AsyncApiReferenceEquality_Should.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace ByteBard.AsyncAPI.Tests
44
using System.IO;
55
using System.Linq;
66
using ByteBard.AsyncAPI.Models;
7+
using ByteBard.AsyncAPI.Models.Interfaces;
78
using ByteBard.AsyncAPI.Readers;
89
using ByteBard.AsyncAPI.Writers;
910
using FluentAssertions;
@@ -118,6 +119,16 @@ public void SettingAPropertyOnAnUnresolvedReference_ThrowsWithTheReference()
118119
act.Should().Throw<InvalidOperationException>().WithMessage("*#/components/channels/missing*");
119120
}
120121

122+
[Test]
123+
public void ReadingExtensionsOnAnUnresolvedBindingsReference_ThrowsWithTheReference()
124+
{
125+
var reference = new AsyncApiBindingsReference<IChannelBinding>("#/components/channelBindings/missing");
126+
127+
var act = () => reference.Extensions;
128+
129+
act.Should().Throw<InvalidOperationException>().WithMessage("*#/components/channelBindings/missing*");
130+
}
131+
121132
[Test]
122133
public void ReadingAFlagOnAnUnresolvedSchemaReference_ReturnsFalse()
123134
{

0 commit comments

Comments
 (0)