Skip to content

Commit a0c6d7f

Browse files
authored
feat(serialization)!: migrate from YamlDotnet to System.Text.Json (LEGO#125)
1 parent 19f62ea commit a0c6d7f

63 files changed

Lines changed: 624 additions & 1598 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace LEGO.AsyncAPI.Bindings.Sns
22
{
33
using System;
44
using System.Collections.Generic;
5+
using LEGO.AsyncAPI.Models;
56
using LEGO.AsyncAPI.Models.Interfaces;
67
using LEGO.AsyncAPI.Writers;
78

@@ -10,7 +11,7 @@ public class FilterPolicy : IAsyncApiExtensible
1011
/// <summary>
1112
/// A map of a message attribute to an array of possible matches. The match may be a simple string for an exact match, but it may also be an object that represents a constraint and values for that constraint.
1213
/// </summary>
13-
public IAsyncApiAny Attributes { get; set; }
14+
public AsyncApiAny Attributes { get; set; }
1415

1516
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
1617

‎src/LEGO.AsyncAPI.Bindings/Sns/Policy.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace LEGO.AsyncAPI.Bindings.Sns
22
{
33
using System;
44
using System.Collections.Generic;
5-
using LEGO.AsyncAPI.Models;
65
using LEGO.AsyncAPI.Models.Interfaces;
76
using LEGO.AsyncAPI.Writers;
87

‎src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace LEGO.AsyncAPI.Bindings.Sns
22
{
33
using System;
44
using System.Collections.Generic;
5-
using LEGO.AsyncAPI.Models.Any;
65
using LEGO.AsyncAPI.Readers.ParseNodes;
76
using LEGO.AsyncAPI.Writers;
87

‎src/LEGO.AsyncAPI.Bindings/Sns/Statement.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace LEGO.AsyncAPI.Bindings.Sns
33
using System;
44
using System.Collections.Generic;
55
using LEGO.AsyncAPI.Attributes;
6-
using LEGO.AsyncAPI.Models;
76
using LEGO.AsyncAPI.Models.Interfaces;
87
using LEGO.AsyncAPI.Writers;
98

‎src/LEGO.AsyncAPI.Bindings/Sqs/Queue.cs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ namespace LEGO.AsyncAPI.Bindings.Sqs
22
{
33
using System;
44
using System.Collections.Generic;
5-
using LEGO.AsyncAPI.Models;
65
using LEGO.AsyncAPI.Models.Interfaces;
76
using LEGO.AsyncAPI.Writers;
8-
using Extensions;
9-
using LEGO.AsyncAPI.Readers;
10-
using LEGO.AsyncAPI.Readers.ParseNodes;
117

128
public class Queue : IAsyncApiExtensible
139
{
Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1-
using System;
2-
using System.Linq;
3-
using LEGO.AsyncAPI.Models.Any;
4-
using LEGO.AsyncAPI.Models.Interfaces;
5-
using LEGO.AsyncAPI.Readers.ParseNodes;
1+
// Copyright (c) The LEGO Group. All rights reserved.
62

73
namespace LEGO.AsyncAPI.Bindings
84
{
5+
using System;
6+
using System.Linq;
7+
using System.Text.Json;
8+
using System.Text.Json.Nodes;
9+
using LEGO.AsyncAPI.Models;
10+
using LEGO.AsyncAPI.Models.Interfaces;
11+
using LEGO.AsyncAPI.Readers.ParseNodes;
12+
913
public class StringOrStringList : IAsyncApiElement
1014
{
11-
public StringOrStringList(IAsyncApiAny value)
15+
public StringOrStringList(AsyncApiAny value)
1216
{
13-
this.Value = value switch
17+
this.Value = value.Node switch
1418
{
15-
AsyncApiArray array => IsValidStringList(array) ? array : throw new ArgumentException($"{nameof(StringOrStringList)} value should only contain string items."),
16-
AsyncApiPrimitive<string> => value,
19+
JsonArray array => IsValidStringList(array) ? new AsyncApiAny(array) : throw new ArgumentException($"{nameof(StringOrStringList)} value should only contain string items."),
20+
JsonValue jValue => IsString(jValue) ? new AsyncApiAny(jValue) : throw new ArgumentException($"{nameof(StringOrStringList)} should be a string value or a string list."),
1721
_ => throw new ArgumentException($"{nameof(StringOrStringList)} should be a string value or a string list.")
1822
};
1923
}
2024

21-
public IAsyncApiAny Value { get; }
25+
public AsyncApiAny Value { get; }
2226

2327
public static StringOrStringList Parse(ParseNode node)
2428
{
2529
switch (node)
2630
{
2731
case ValueNode:
28-
return new StringOrStringList(new AsyncApiString(node.GetScalarValue()));
32+
return new StringOrStringList(new AsyncApiAny(node.GetScalarValue()));
2933
case ListNode:
3034
{
31-
var asyncApiArray = new AsyncApiArray();
32-
asyncApiArray.AddRange(node.CreateSimpleList(s => new AsyncApiString(s.GetScalarValue())));
35+
var jsonArray = new JsonArray();
36+
foreach (var item in node as ListNode)
37+
{
38+
jsonArray.Add(item.GetScalarValue());
39+
}
3340

34-
return new StringOrStringList(asyncApiArray);
41+
return new StringOrStringList(new AsyncApiAny(jsonArray));
3542
}
3643

3744
default:
@@ -40,9 +47,15 @@ public static StringOrStringList Parse(ParseNode node)
4047
}
4148
}
4249

43-
private static bool IsValidStringList(AsyncApiArray array)
50+
private static bool IsString(JsonNode value)
51+
{
52+
var element = JsonDocument.Parse(value.ToJsonString()).RootElement;
53+
return element.ValueKind == JsonValueKind.String;
54+
}
55+
56+
private static bool IsValidStringList(JsonArray array)
4457
{
45-
return array.All(x => x is AsyncApiPrimitive<string>);
58+
return array.All(x => IsString(x));
4659
}
4760
}
4861
}

‎src/LEGO.AsyncAPI.Readers/AsyncApiReaderSettings.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace LEGO.AsyncAPI.Readers
55
using System;
66
using System.Collections.Generic;
77
using System.IO;
8+
using LEGO.AsyncAPI.Models;
89
using LEGO.AsyncAPI.Models.Interfaces;
910
using LEGO.AsyncAPI.Readers.Interface;
1011
using LEGO.AsyncAPI.Validations;
@@ -36,10 +37,10 @@ public class AsyncApiReaderSettings
3637
/// <summary>
3738
/// Dictionary of parsers for converting extensions into strongly typed classes.
3839
/// </summary>
39-
public Dictionary<string, Func<IAsyncApiAny, IAsyncApiExtension>>
40+
public Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>>
4041
ExtensionParsers
4142
{ get; set; } =
42-
new Dictionary<string, Func<IAsyncApiAny, IAsyncApiExtension>>();
43+
new Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>>();
4344

4445
public List<IBindingParser<IBinding>>
4546
Bindings

‎src/LEGO.AsyncAPI.Readers/AsyncApiTextReader.cs‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ namespace LEGO.AsyncAPI.Readers
44
{
55
using System.IO;
66
using System.Linq;
7+
using System.Text.Json;
8+
using System.Text.Json.Nodes;
79
using System.Threading.Tasks;
810
using LEGO.AsyncAPI.Models;
911
using LEGO.AsyncAPI.Models.Interfaces;
1012
using LEGO.AsyncAPI.Readers.Interface;
11-
using YamlDotNet.Core;
1213
using YamlDotNet.RepresentationModel;
1314

1415
/// <summary>
@@ -35,21 +36,21 @@ public AsyncApiTextReader(AsyncApiReaderSettings settings = null)
3536
/// <returns>Instance of newly created AsyncApiDocument.</returns>
3637
public AsyncApiDocument Read(TextReader input, out AsyncApiDiagnostic diagnostic)
3738
{
38-
YamlDocument yamlDocument;
39+
JsonNode jsonNode;
3940

4041
// Parse the YAML/JSON text in the TextReader into the YamlDocument
4142
try
4243
{
43-
yamlDocument = LoadYamlDocument(input);
44+
jsonNode = LoadYamlDocument(input);
4445
}
45-
catch (YamlException ex)
46+
catch (JsonException ex)
4647
{
4748
diagnostic = new AsyncApiDiagnostic();
48-
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.Start.Line}", ex.Message));
49+
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.LineNumber}", ex.Message));
4950
return new AsyncApiDocument();
5051
}
5152

52-
return new AsyncApiYamlDocumentReader(this.settings).Read(yamlDocument, out diagnostic);
53+
return new AsyncApiJsonDocumentReader(this.settings).Read(jsonNode, out diagnostic);
5354
}
5455

5556
/// <summary>
@@ -59,25 +60,25 @@ public AsyncApiDocument Read(TextReader input, out AsyncApiDiagnostic diagnostic
5960
/// <returns>A ReadResult instance that contains the resulting AsyncApiDocument and a diagnostics instance.</returns>
6061
public async Task<ReadResult> ReadAsync(TextReader input)
6162
{
62-
YamlDocument yamlDocument;
63+
JsonNode jsonNode;
6364

6465
// Parse the YAML/JSON text in the TextReader into the YamlDocument
6566
try
6667
{
67-
yamlDocument = LoadYamlDocument(input);
68+
jsonNode = LoadYamlDocument(input);
6869
}
69-
catch (YamlException ex)
70+
catch (JsonException ex)
7071
{
7172
var diagnostic = new AsyncApiDiagnostic();
72-
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.Start.Line}", ex.Message));
73+
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.LineNumber}", ex.Message));
7374
return new ReadResult
7475
{
7576
AsyncApiDocument = null,
7677
AsyncApiDiagnostic = diagnostic,
7778
};
7879
}
7980

80-
return await new AsyncApiYamlDocumentReader(this.settings).ReadAsync(yamlDocument);
81+
return await new AsyncApiJsonDocumentReader(this.settings).ReadAsync(jsonNode);
8182
}
8283

8384
/// <summary>
@@ -90,21 +91,21 @@ public async Task<ReadResult> ReadAsync(TextReader input)
9091
public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApiDiagnostic diagnostic)
9192
where T : IAsyncApiElement
9293
{
93-
YamlDocument yamlDocument;
94+
JsonNode jsonNode;
9495

9596
// Parse the YAML/JSON
9697
try
9798
{
98-
yamlDocument = LoadYamlDocument(input);
99+
jsonNode = LoadYamlDocument(input);
99100
}
100-
catch (YamlException ex)
101+
catch (JsonException ex)
101102
{
102103
diagnostic = new AsyncApiDiagnostic();
103-
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.Start.Line}", ex.Message));
104+
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.LineNumber}", ex.Message));
104105
return default;
105106
}
106107

107-
return new AsyncApiYamlDocumentReader(this.settings).ReadFragment<T>(yamlDocument, version,
108+
return new AsyncApiJsonDocumentReader(this.settings).ReadFragment<T>(jsonNode, version,
108109
out diagnostic);
109110
}
110111

@@ -113,11 +114,11 @@ public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApi
113114
/// </summary>
114115
/// <param name="input">Stream containing YAML formatted text.</param>
115116
/// <returns>Instance of a YamlDocument.</returns>
116-
static YamlDocument LoadYamlDocument(TextReader input)
117+
static JsonNode LoadYamlDocument(TextReader input)
117118
{
118119
var yamlStream = new YamlStream();
119120
yamlStream.Load(input);
120-
return yamlStream.Documents.First();
121+
return yamlStream.Documents.First().ToJsonNode();
121122
}
122123
}
123124
}

‎src/LEGO.AsyncAPI.Readers/AsyncApiYamlDocumentReader.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ namespace LEGO.AsyncAPI.Readers
44
{
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Text.Json.Nodes;
78
using System.Threading.Tasks;
89
using LEGO.AsyncAPI.Exceptions;
910
using LEGO.AsyncAPI.Extensions;
1011
using LEGO.AsyncAPI.Models;
1112
using LEGO.AsyncAPI.Models.Interfaces;
1213
using LEGO.AsyncAPI.Readers.Interface;
1314
using LEGO.AsyncAPI.Validations;
14-
using YamlDotNet.RepresentationModel;
1515

1616
/// <summary>
1717
/// Service class for converting contents of TextReader into AsyncApiDocument instances.
1818
/// </summary>
19-
internal class AsyncApiYamlDocumentReader : IAsyncApiReader<YamlDocument, AsyncApiDiagnostic>
19+
internal class AsyncApiJsonDocumentReader : IAsyncApiReader<JsonNode, AsyncApiDiagnostic>
2020
{
2121
private readonly AsyncApiReaderSettings settings;
2222

2323
/// <summary>
2424
/// Create stream reader with custom settings if desired.
2525
/// </summary>
2626
/// <param name="settings"></param>
27-
public AsyncApiYamlDocumentReader(AsyncApiReaderSettings settings = null)
27+
public AsyncApiJsonDocumentReader(AsyncApiReaderSettings settings = null)
2828
{
2929
this.settings = settings ?? new AsyncApiReaderSettings();
3030
}
@@ -35,7 +35,7 @@ public AsyncApiYamlDocumentReader(AsyncApiReaderSettings settings = null)
3535
/// <param name="input">TextReader containing AsyncApi description to parse.</param>
3636
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
3737
/// <returns>Instance of newly created AsyncApiDocument.</returns>
38-
public AsyncApiDocument Read(YamlDocument input, out AsyncApiDiagnostic diagnostic)
38+
public AsyncApiDocument Read(JsonNode input, out AsyncApiDiagnostic diagnostic)
3939
{
4040
diagnostic = new AsyncApiDiagnostic();
4141
var context = new ParsingContext(diagnostic)
@@ -76,7 +76,7 @@ public AsyncApiDocument Read(YamlDocument input, out AsyncApiDiagnostic diagnost
7676
return document;
7777
}
7878

79-
public Task<ReadResult> ReadAsync(YamlDocument input)
79+
public Task<ReadResult> ReadAsync(JsonNode input)
8080
{
8181
var diagnostic = new AsyncApiDiagnostic();
8282
var context = new ParsingContext(diagnostic)
@@ -140,7 +140,7 @@ private void ResolveReferences(AsyncApiDiagnostic diagnostic, AsyncApiDocument d
140140
/// <param name="version">Version of the AsyncApi specification that the fragment conforms to.</param>
141141
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
142142
/// <returns>Instance of newly created AsyncApiDocument.</returns>
143-
public T ReadFragment<T>(YamlDocument input, AsyncApiVersion version, out AsyncApiDiagnostic diagnostic)
143+
public T ReadFragment<T>(JsonNode input, AsyncApiVersion version, out AsyncApiDiagnostic diagnostic)
144144
where T : IAsyncApiElement
145145
{
146146
diagnostic = new AsyncApiDiagnostic();

‎src/LEGO.AsyncAPI.Readers/Exceptions/AsyncApiReaderException.cs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace LEGO.AsyncAPI.Readers.Exceptions
44
{
55
using System;
66
using LEGO.AsyncAPI.Exceptions;
7-
using YamlDotNet.RepresentationModel;
87

98
[Serializable]
109
public class AsyncApiReaderException : AsyncApiException
@@ -24,14 +23,6 @@ public AsyncApiReaderException(string message, ParsingContext context)
2423
this.Pointer = context.GetLocation();
2524
}
2625

27-
public AsyncApiReaderException(string message, YamlNode node)
28-
: base(message)
29-
{
30-
// This only includes line because using a char range causes tests to break due to CR/LF & LF differences
31-
// See https://tools.ietf.org/html/rfc5147 for syntax
32-
this.Pointer = $"#line={node.Start.Line}";
33-
}
34-
3526
public AsyncApiReaderException(string message, Exception innerException)
3627
: base(message, innerException)
3728
{

0 commit comments

Comments
 (0)