Skip to content

Commit 8598935

Browse files
committed
refactor: privatize the avro schema property and add helpers
1 parent a9d501d commit 8598935

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/LEGO.AsyncAPI/Models/AsyncApiAvroSchemaPayload.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,43 @@ namespace LEGO.AsyncAPI.Models
44
{
55
using LEGO.AsyncAPI.Models.Interfaces;
66
using LEGO.AsyncAPI.Writers;
7+
using System;
78

89
public class AsyncApiAvroSchemaPayload : IAsyncApiMessagePayload
910
{
10-
public AvroSchema Schema { get; set; }
11+
private readonly AvroSchema schema;
1112

1213
public AsyncApiAvroSchemaPayload(AvroSchema schema)
1314
{
14-
this.Schema = schema;
15+
this.schema = schema;
1516
}
1617

1718
public AsyncApiAvroSchemaPayload()
1819
{
20+
this.schema = new AvroRecord();
1921
}
2022

2123
public bool TryGetAs<T>(out T schema)
2224
where T : AvroSchema
2325
{
24-
schema = this.Schema as T;
26+
schema = this.schema as T;
2527
return schema != null;
2628
}
2729

28-
public bool UnresolvedReference { get => this.Schema.UnresolvedReference; set => this.Schema.UnresolvedReference = value; }
30+
public bool Is<T>()
31+
where T : AvroSchema
32+
{
33+
return this.schema is T;
34+
}
35+
36+
public Type GetSchemaType()
37+
{
38+
return this.schema.GetType();
39+
}
40+
41+
public bool UnresolvedReference { get => this.schema.UnresolvedReference; set => this.schema.UnresolvedReference = value; }
2942

30-
public AsyncApiReference Reference { get => this.Schema.Reference; set => this.Schema.Reference = value; }
43+
public AsyncApiReference Reference { get => this.schema.Reference; set => this.schema.Reference = value; }
3144

3245
public void SerializeV2(IAsyncApiWriter writer)
3346
{
@@ -47,7 +60,7 @@ public void SerializeV2(IAsyncApiWriter writer)
4760

4861
public void SerializeV2WithoutReference(IAsyncApiWriter writer)
4962
{
50-
this.Schema.SerializeV2(writer);
63+
this.schema.SerializeV2(writer);
5164
}
5265
}
5366
}

test/LEGO.AsyncAPI.Tests/Models/AsyncApiMessage_Should.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,25 +189,23 @@ public void AsyncApiMessage_WithAvroSchemaFormat_Serializes()
189189

190190
var message = new AsyncApiMessage();
191191
message.SchemaFormat = "application/vnd.apache.avro";
192-
message.Payload = new AsyncApiAvroSchemaPayload()
192+
var schema = new AvroRecord()
193193
{
194-
Schema = new AvroRecord()
194+
Name = "User",
195+
Namespace = "com.example",
196+
Fields = new List<AvroField>
195197
{
196-
Name = "User",
197-
Namespace = "com.example",
198-
Fields = new List<AvroField>
198+
new AvroField()
199199
{
200-
new AvroField()
201-
{
202-
Name = "username",
203-
Type = AvroPrimitiveType.String,
204-
Doc = "The username of the user.",
205-
Default = new AsyncApiAny("guest"),
206-
Order = AvroFieldOrder.Ascending,
207-
},
200+
Name = "username",
201+
Type = AvroPrimitiveType.String,
202+
Doc = "The username of the user.",
203+
Default = new AsyncApiAny("guest"),
204+
Order = AvroFieldOrder.Ascending,
208205
},
209206
},
210207
};
208+
message.Payload = new AsyncApiAvroSchemaPayload(schema);
211209

212210
// Act
213211
var actual = message.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);

0 commit comments

Comments
 (0)