forked from ByteBardOrg/AsyncAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncApiServerDeserializer.cs
More file actions
91 lines (83 loc) · 2.9 KB
/
AsyncApiServerDeserializer.cs
File metadata and controls
91 lines (83 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using System;
/// <summary>
/// Class containing logic to deserialize AsyncApi document into
/// runtime AsyncApi object model.
/// </summary>
internal static partial class AsyncApiV2Deserializer
{
private static readonly FixedFieldMap<AsyncApiServer> serverFixedFields = new()
{
{
"url", (a, n) => { SetHostAndPathname(a, n); }
},
//this is workaround for some reason we parse twice this...
{
"host", (a, n) => { a.Host = n.GetScalarValue(); }
},
{
"pathname", (a, n) => { a.PathName = n.GetScalarValue(); }
},
{
"description", (a, n) => { a.Description = n.GetScalarValue(); }
},
{
"variables", (a, n) => { a.Variables = n.CreateMap(LoadServerVariable); }
},
{
"security", (a, n) => { a.Security = n.CreateList(LoadSecurityRequirement); }
},
{
"tags", (a, n) => { a.Tags = n.CreateList(LoadTag); }
},
{
"bindings", (o, n) => { o.Bindings = LoadServerBindings(n); }
},
{
"protocolVersion", (a, n) => { a.ProtocolVersion = n.GetScalarValue(); }
},
{
"protocol", (a, n) => { a.Protocol = n.GetScalarValue(); }
},
};
private static void SetHostAndPathname(AsyncApiServer a, ParseNode n)
{
var value = n.GetScalarValue();
if (!value.Contains("://"))
{
// Set arbitrary protocol.
value = "unknown://" + value;
}
if (Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out var uri))
{
a.Host = uri.Authority;
a.PathName = uri.LocalPath == "/" ? null : uri.LocalPath;
}
else
{
a.Host = n.GetScalarValue();
}
}
private static readonly PatternFieldMap<AsyncApiServer> serverPatternFields =
new()
{
{ s => s.StartsWith("x-"), (a, p, n) => a.AddExtension(p, LoadExtension(p, n)) },
};
public static AsyncApiServer LoadServer(ParseNode node)
{
var mapNode = node.CheckMapNode("servers");
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return new AsyncApiServerReference(pointer);
}
var server = new AsyncApiServer();
ParseMap(mapNode, server, serverFixedFields, serverPatternFields);
return server;
}
}
}