Skip to content

Commit 907dba8

Browse files
author
Eamonn Moloney
committed
Added readers and writers for the asyncapiAPI based on newtonsoft
1 parent 8766942 commit 907dba8

Some content is hidden

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

45 files changed

+4250
-33
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,9 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
# MacOS
353+
**/.DS_Store
354+
355+
# Jetbrains
356+
**/.idea

AsyncAPI.sln

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LEGO.AsyncAPI.Tests", "test\LEGO.AsyncAPI.Tests\LEGO.AsyncAPI.Tests.csproj", "{32C4357C-F664-4C6C-B87B-AEF1759EC060}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LEGO.AsyncAPI", "src\LEGO.AsyncAPI\LEGO.AsyncAPI.csproj", "{D4559946-300D-4C6F-B92B-4376414D19C9}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LEGO.AsyncAPI.E2E.Tests", "test\LEGO.AsyncAPI.E2E.Tests\LEGO.AsyncAPI.E2E.Tests.csproj", "{85E0F568-3B0C-4253-A03B-83578865A788}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{32C4357C-F664-4C6C-B87B-AEF1759EC060}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{32C4357C-F664-4C6C-B87B-AEF1759EC060}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{32C4357C-F664-4C6C-B87B-AEF1759EC060}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{32C4357C-F664-4C6C-B87B-AEF1759EC060}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{D4559946-300D-4C6F-B92B-4376414D19C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D4559946-300D-4C6F-B92B-4376414D19C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D4559946-300D-4C6F-B92B-4376414D19C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D4559946-300D-4C6F-B92B-4376414D19C9}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{85E0F568-3B0C-4253-A03B-83578865A788}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{85E0F568-3B0C-4253-A03B-83578865A788}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{85E0F568-3B0C-4253-A03B-83578865A788}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{85E0F568-3B0C-4253-A03B-83578865A788}.Release|Any CPU.Build.0 = Release|Any CPU
28+
EndGlobalSection
29+
EndGlobal

src/LEGO.AsyncAPI.sln

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using LEGO.AsyncAPI.Any;
2+
using LEGO.AsyncAPI.Models;
3+
using Newtonsoft.Json;
4+
5+
namespace LEGO.AsyncAPI
6+
{
7+
public class AsyncApiReaderNewtonJson<T>: IReader<T>
8+
{
9+
public T Consume(Stream stream)
10+
{
11+
return JsonConvert.DeserializeObject<T>(new StreamReader(stream).ReadToEnd(), new PayloadConverter());
12+
}
13+
}
14+
}

src/LEGO.AsyncAPI/AsyncAPIWriter.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Newtonsoft.Json;
2+
3+
namespace LEGO.AsyncAPI
4+
{
5+
public class AsyncApiWriter<T>
6+
{
7+
public string Produce(T asyncApiDocument)
8+
{
9+
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
10+
{
11+
NullValueHandling = NullValueHandling.Ignore,
12+
Formatting = Formatting.Indented,
13+
};
14+
return JsonConvert.SerializeObject(asyncApiDocument, jsonSerializerSettings);
15+
}
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Text.Json;
2+
using Json.Schema;
3+
using LEGO.AsyncAPI.Any;
4+
using LEGO.AsyncAPI.Models;
5+
using Newtonsoft.Json;
6+
7+
namespace LEGO.AsyncAPI
8+
{
9+
public class AsyncApiSchemaValidator
10+
{
11+
private const string SampleFolderPath = "";
12+
13+
public async Task<ValidationResults> Validate(JsonDocument jsonDocument)
14+
{
15+
var type = typeof(AsyncApiSchemaValidator);
16+
var schemaPath = type.Namespace + "." +
17+
Path.Combine(SampleFolderPath, "schema-v2.3.0.json").Replace("/", ".");
18+
Stream schemaStream = type.Assembly.GetManifestResourceStream(schemaPath);
19+
20+
var schema = JsonSchema.FromText(new StreamReader(schemaStream).ReadToEnd());
21+
22+
23+
var json = jsonDocument;
24+
25+
var options = new ValidationOptions();
26+
options.DefaultBaseUri =
27+
new Uri("https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/schemas/2.3.0.json");
28+
var result = schema.Validate(json.RootElement, options);
29+
return result;
30+
}
31+
}
32+
}

src/LEGO.AsyncAPI/IReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace LEGO.AsyncAPI
2+
{
3+
public interface IReader<T>
4+
{
5+
public T Consume(Stream jsonDocument);
6+
}
7+
}

src/LEGO.AsyncAPI/LEGO.AsyncAPI.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21+
<PackageReference Include="JsonSchema.Net" Version="2.0.1" />
22+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
2123
<PackageReference Include="SharpYaml" Version="1.8.0" />
2224
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
2325
<PrivateAssets>all</PrivateAssets>
2426
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2527
</PackageReference>
2628
</ItemGroup>
2729

30+
<ItemGroup>
31+
<EmbeddedResource Include="schema-v2.3.0.json" />
32+
</ItemGroup>
33+
2834
</Project>

src/LEGO.AsyncAPI/Models/Any/AnyType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ public enum AnyType
2626
/// Object.
2727
/// </summary>
2828
Object,
29+
30+
/// <summary>
31+
/// Undefined
32+
/// </summary>
33+
Undefined
2934
}
3035
}

src/LEGO.AsyncAPI/Models/Any/Array.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public class Array : List<IAny>, IAny
1111
/// The type of <see cref="IOpenApiAny"/>.
1212
/// </summary>
1313
public AnyType AnyType { get; } = AnyType.Array;
14+
15+
public List<IAny> Value { get; set; }
1416
}
1517
}

0 commit comments

Comments
 (0)