Skip to content

Commit 907dba8

Browse files
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

+6
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

+29
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

-30
This file was deleted.
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

+17
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+
}
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

+7
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

+6
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

+5
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

+2
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
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace LEGO.AsyncAPI.Any
2+
{
3+
public class Boolean : Primitive<bool?>
4+
{
5+
private bool? _value;
6+
7+
/// <summary>
8+
/// The type of <see cref="IOpenApiAny"/>.
9+
/// </summary>
10+
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Boolean;
11+
12+
public override bool? Value
13+
{
14+
get { return _value; }
15+
set { _value = value; }
16+
}
17+
}
18+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Any
4+
{
5+
/// <summary>
6+
/// Async API null.
7+
/// </summary>
8+
public class Double : Primitive<double?>
9+
{
10+
private double? _value;
11+
12+
/// <summary>
13+
/// The type of <see cref="IOpenApiAny"/>.
14+
/// </summary>
15+
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Double;
16+
17+
public override double? Value
18+
{
19+
get { return _value; }
20+
set { _value = value; }
21+
}
22+
}
23+
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Any
4+
{
5+
/// <summary>
6+
/// Async API null.
7+
/// </summary>
8+
public class Long : Primitive<long?>
9+
{
10+
/// <summary>
11+
/// The type of <see cref="IOpenApiAny"/>.
12+
/// </summary>
13+
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Long;
14+
15+
public override long? Value { get; set; }
16+
}
17+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace LEGO.AsyncAPI.Any
2+
{
3+
public abstract class Primitive<T> : IAny
4+
{
5+
public AnyType AnyType => AnyType.Primitive;
6+
7+
public abstract PrimitiveType PrimitiveType { get; }
8+
9+
public abstract T? Value { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace LEGO.AsyncAPI.Any
2+
{
3+
public enum PrimitiveType
4+
{
5+
String,
6+
Long,
7+
Double,
8+
Boolean
9+
}
10+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Any
4+
{
5+
/// <summary>
6+
/// Async API null.
7+
/// </summary>
8+
public class String : Primitive<string>
9+
{
10+
public String()
11+
{
12+
}
13+
14+
public String(string? value)
15+
{
16+
Value = value;
17+
}
18+
19+
/// <summary>
20+
/// The type of <see cref="IOpenApiAny"/>.
21+
/// </summary>
22+
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.String;
23+
24+
public override string? Value { get; set; }
25+
}
26+
}

src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LEGO.AsyncAPI.Models
44
{
55
using System.Collections.Generic;
6+
using Newtonsoft.Json;
67

78
/// <summary>
89
/// This is the root document object for the API specification. It combines resource listing and API declaration together into one document.
@@ -12,22 +13,25 @@ public class AsyncApiDocument : IExtensible
1213
/// <summary>
1314
/// REQUIRED. Specifies the AsyncAPI Specification version being used.
1415
/// </summary>
16+
[JsonProperty("asyncapi")]
1517
public string AsyncApi { get; set; }
1618

1719
/// <summary>
1820
/// Identifier of the application the AsyncAPI document is defining.
1921
/// </summary>
22+
[JsonProperty("id")]
2023
public string Id { get; set; }
2124

2225
/// <summary>
2326
/// REQUIRED. Provides metadata about the API. The metadata can be used by the clients if needed.
2427
/// </summary>
28+
[JsonProperty("info")]
2529
public Info Info { get; set; }
2630

2731
/// <summary>
2832
/// Provides connection details of servers. Field pattern ^[A-Za-z0-9_\-]+$.
2933
/// </summary>
30-
public IDictionary<string, Server> Servers { get; set; } = new Dictionary<string, Server>();
34+
public IDictionary<string, Server> Servers { get; set; }
3135

3236
/// <summary>
3337
/// Default content type to use when encoding/decoding a message's payload.
@@ -41,6 +45,7 @@ public class AsyncApiDocument : IExtensible
4145
/// <summary>
4246
/// REQUIRED. The available channels and messages for the API.
4347
/// </summary>
48+
[JsonProperty("channels")]
4449
public IDictionary<string, Channel> Channels { get; set; } = new Dictionary<string, Channel>();
4550

4651
/// <summary>
@@ -51,14 +56,15 @@ public class AsyncApiDocument : IExtensible
5156
/// <summary>
5257
/// A list of tags used by the specification with additional metadata. Each tag name in the list MUST be unique.
5358
/// </summary>
54-
public IList<Tag> Tags { get; set; } = new List<Tag>();
59+
public IList<Tag> Tags { get; set; }
5560

5661
/// <summary>
5762
/// Additional external documentation.
5863
/// </summary>
5964
public ExternalDocumentation ExternalDocs { get; set; }
6065

6166
/// <inheritdoc/>
67+
[JsonIgnore]
6268
public IDictionary<string, string> Extensions { get; set; } = new Dictionary<string, string>();
6369
}
6470
}

src/LEGO.AsyncAPI/Models/CorrelationId.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) The LEGO Group. All rights reserved.
22

3+
using Newtonsoft.Json;
4+
35
namespace LEGO.AsyncAPI.Models
46
{
57
/// <summary>
@@ -18,12 +20,14 @@ public class CorrelationId : IReferenceable, IExtensible
1820
public string Location { get; set; }
1921

2022
/// <inheritdoc/>
23+
[JsonProperty("unresolvedReference")]
2124
public bool UnresolvedReference { get; set; }
2225

2326
/// <inheritdoc/>
2427
public Reference Reference { get; set; }
2528

2629
/// <inheritdoc/>
30+
[JsonIgnore]
2731
public IDictionary<string, string> Extensions { get; set; } = new Dictionary<string, string>();
2832
}
2933
}

0 commit comments

Comments
 (0)