Skip to content

Commit e863d83

Browse files
authored
Merge pull request #10 from MobileTeleSystems/feature/asyncapi_dom_server
AsyncApi DOM. Read server object
2 parents 1bc3dd3 + cb78eb8 commit e863d83

File tree

9 files changed

+191
-43
lines changed

9 files changed

+191
-43
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ApiCodeGenerator.AsyncApi.DOM.Bindings.Amqp;
2+
3+
public class Server
4+
{
5+
}

src/ApiCodeGenerator.AsyncApi/DOM/Components.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33

44
namespace ApiCodeGenerator.AsyncApi.DOM
55
{
6-
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
76
public class Components
87
{
9-
[JsonProperty("messages", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
10-
public IDictionary<string, Message> Messages { get; set; }
8+
[JsonProperty("messages", DefaultValueHandling = DefaultValueHandling.Populate)]
9+
public IDictionary<string, Message> Messages { get; } = new Dictionary<string, Message>();
1110

12-
[JsonProperty("parameters", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
13-
public IDictionary<string, Parameter> Parameters { get; set; }
11+
[JsonProperty("parameters", DefaultValueHandling = DefaultValueHandling.Populate)]
12+
public IDictionary<string, Parameter> Parameters { get; } = new Dictionary<string, Parameter>();
1413

15-
[JsonProperty("schemas", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
16-
public IDictionary<string, JsonSchema> Schemas { get; set; }
14+
[JsonProperty("schemas", DefaultValueHandling = DefaultValueHandling.Populate)]
15+
public IDictionary<string, JsonSchema> Schemas { get; } = new Dictionary<string, JsonSchema>();
16+
17+
[JsonProperty("servers", DefaultValueHandling = DefaultValueHandling.Populate)]
18+
public IDictionary<string, Server> Servers { get; } = new Dictionary<string, Server>();
19+
20+
[JsonProperty("serverVariables", DefaultValueHandling = DefaultValueHandling.Populate)]
21+
public IDictionary<string, ServerVariable> ServerVariables { get; } = new Dictionary<string, ServerVariable>();
1722
}
18-
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
1923
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Newtonsoft.Json;
2+
3+
namespace ApiCodeGenerator.AsyncApi.DOM
4+
{
5+
6+
public class SecurityRequirement
7+
{
8+
}
9+
}

src/ApiCodeGenerator.AsyncApi/DOM/Server.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,30 @@
22

33
namespace ApiCodeGenerator.AsyncApi.DOM
44
{
5-
public class Server
5+
public class Server : RefObject<Server>
66
{
7+
[JsonProperty("url")]
8+
public required string Url { get; set; }
9+
10+
[JsonProperty("protocol")]
11+
public required string Protocol { get; set; }
12+
13+
[JsonProperty("protocolVersion")]
14+
public string? ProtocolVersion { get; set; }
15+
16+
[JsonProperty("description")]
17+
public string? Description { get; set; }
18+
19+
[JsonProperty("variables")]
20+
public IDictionary<string, ServerVariable>? Variables { get; set; }
21+
22+
[JsonProperty("security")]
23+
public ICollection<SecurityRequirement>? Security { get; set; }
24+
25+
[JsonProperty("tags")]
26+
public ICollection<Tag>? Tags { get; set; }
27+
28+
[JsonProperty("bindings")]
29+
public ServerBindings? Bindings { get; set; }
730
}
831
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Newtonsoft.Json;
2+
3+
namespace ApiCodeGenerator.AsyncApi.DOM
4+
{
5+
public class ServerBindings : RefObject<ServerBindings>
6+
{
7+
public Bindings.Amqp.Server? Amqp { get; set; }
8+
}
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Newtonsoft.Json;
2+
3+
namespace ApiCodeGenerator.AsyncApi.DOM
4+
{
5+
public class ServerVariable : RefObject<ServerVariable>
6+
{
7+
[JsonProperty("description")]
8+
public string? Description { get; set; }
9+
10+
[JsonProperty("enum")]
11+
public ICollection<string>? Enum { get; set; }
12+
13+
[JsonProperty("default")]
14+
public string? Default { get; set; }
15+
16+
[JsonProperty("examples")]
17+
public ICollection<string>? Examples { get; set; }
18+
}
19+
}

test/ApiCodeGenerator.AsyncApi.Tests/AsyncApiContentGeneratorTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ApiCodeGenerator.AsyncApi.DOM;
2+
using NUnit.Framework.Constraints;
23

34
namespace ApiCodeGenerator.AsyncApi.Tests;
45

@@ -150,5 +151,39 @@ private void ValidateDocument(AsyncApiDocument document)
150151
Assert.That(document.Components.Schemas["turnOnOffPayload"]?.ActualProperties,
151152
Is.Not.Null
152153
.And.ContainKey("command"));
154+
155+
//Read server object
156+
Assert.That(document.Servers["scram-connections"],
157+
Is.Not.Null
158+
.And.Property("Url").EqualTo("test.mykafkacluster.org:18092")
159+
.And.Property("Protocol").EqualTo("kafka-secure")
160+
.And.Property("Description").EqualTo("Test broker secured with scramSha256"));
161+
162+
// Resolve $ref in servers
163+
Assert.That(document.Servers["mtls-connections"],
164+
Is.Not.Null
165+
.And.Property("Reference").EqualTo(document.Components.Servers["mtls-connections"]));
166+
167+
// Resolve $ref in server variables
168+
Assert.Multiple(() =>
169+
{
170+
var variables = document.Components.Servers["mtls-connections"].Variables;
171+
Assert.That(variables,
172+
Is.Not.Null
173+
.And.ContainKey("someRefVariable")
174+
.And.ContainKey("someVariable"));
175+
176+
Assert.That(variables!["someRefVariable"],
177+
Is.Not.Null
178+
.And.Property("Reference").EqualTo(document.Components.ServerVariables["someRefVariable"]));
179+
});
180+
181+
//Read server variables
182+
Assert.That(document.Components.ServerVariables["someRefVariable"],
183+
new PredicateConstraint<ServerVariable>(a =>
184+
a.Description == "Some ref variable"
185+
&& a.Enum?.FirstOrDefault() == "def"
186+
&& a.Default == "def"
187+
&& a.Examples?.FirstOrDefault() == "exam"));
153188
}
154189
}

test/ApiCodeGenerator.AsyncApi.Tests/asyncApi/asyncapi.json

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,7 @@
3535
]
3636
},
3737
"mtls-connections": {
38-
"url": "test.mykafkacluster.org:28092",
39-
"protocol": "kafka-secure",
40-
"description": "Test broker secured with X509",
41-
"security": [
42-
{
43-
"certs": []
44-
}
45-
],
46-
"tags": [
47-
{
48-
"name": "env:test-mtls",
49-
"description": "This environment is meant for running internal tests through mtls"
50-
},
51-
{
52-
"name": "kind:remote",
53-
"description": "This server is a remote server. Not exposed by the application"
54-
},
55-
{
56-
"name": "visibility:private",
57-
"description": "This resource is private and only available to certain users"
58-
}
59-
]
38+
"$ref": "#/components/servers/mtls-connections"
6039
}
6140
},
6241
"defaultContentType": "application/json",
@@ -294,6 +273,52 @@
294273
}
295274
}
296275
}
276+
},
277+
"servers": {
278+
"mtls-connections": {
279+
"url": "test.mykafkacluster.org:28092",
280+
"protocol": "kafka-secure",
281+
"description": "Test broker secured with X509",
282+
"security": [
283+
{
284+
"certs": []
285+
}
286+
],
287+
"tags": [
288+
{
289+
"name": "env:test-mtls",
290+
"description": "This environment is meant for running internal tests through mtls"
291+
},
292+
{
293+
"name": "kind:remote",
294+
"description": "This server is a remote server. Not exposed by the application"
295+
},
296+
{
297+
"name": "visibility:private",
298+
"description": "This resource is private and only available to certain users"
299+
}
300+
],
301+
"variables": {
302+
"someRefVariable": {
303+
"$ref": "#/components/serverVariables/someRefVariable"
304+
},
305+
"someVariable": {
306+
"description": "Some variable"
307+
}
308+
}
309+
}
310+
},
311+
"serverVariables": {
312+
"someRefVariable": {
313+
"description": "Some ref variable",
314+
"enum": [
315+
"def"
316+
],
317+
"default": "def",
318+
"examples": [
319+
"exam"
320+
]
321+
}
297322
}
298323
}
299324
}

test/ApiCodeGenerator.AsyncApi.Tests/asyncApi/asyncapi.yml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,9 @@ servers:
2828
description: "This server is a remote server. Not exposed by the application"
2929
- name: "visibility:private"
3030
description: "This resource is private and only available to certain users"
31+
3132
mtls-connections:
32-
url: test.mykafkacluster.org:28092
33-
protocol: kafka-secure
34-
description: Test broker secured with X509
35-
security:
36-
- certs: []
37-
tags:
38-
- name: "env:test-mtls"
39-
description: "This environment is meant for running internal tests through mtls"
40-
- name: "kind:remote"
41-
description: "This server is a remote server. Not exposed by the application"
42-
- name: "visibility:private"
43-
description: "This resource is private and only available to certain users"
33+
$ref: "#/components/servers/mtls-connections"
4434

4535
defaultContentType: application/json
4636

@@ -194,3 +184,32 @@ components:
194184
clientId:
195185
type: string
196186
enum: ["my-app-id"]
187+
188+
servers:
189+
mtls-connections:
190+
url: test.mykafkacluster.org:28092
191+
protocol: kafka-secure
192+
description: Test broker secured with X509
193+
security:
194+
- certs: []
195+
tags:
196+
- name: "env:test-mtls"
197+
description: "This environment is meant for running internal tests through mtls"
198+
- name: "kind:remote"
199+
description: "This server is a remote server. Not exposed by the application"
200+
- name: "visibility:private"
201+
description: "This resource is private and only available to certain users"
202+
variables:
203+
someRefVariable:
204+
$ref: "#/components/serverVariables/someRefVariable"
205+
someVariable:
206+
description: Some variable
207+
208+
serverVariables:
209+
someRefVariable:
210+
description: Some ref variable
211+
default: def
212+
enum:
213+
- def
214+
examples:
215+
- exam

0 commit comments

Comments
 (0)