-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathParamHelper.cs
60 lines (51 loc) · 1.72 KB
/
ParamHelper.cs
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
using NJsonSchema;
using NSwag;
using Scriban.Runtime;
using System;
using System.Linq;
namespace LibKubernetesGenerator
{
internal class ParamHelper : IScriptObjectHelper
{
private readonly GeneralNameHelper generalNameHelper;
private readonly TypeHelper typeHelper;
public ParamHelper(GeneralNameHelper generalNameHelper, TypeHelper typeHelper)
{
this.generalNameHelper = generalNameHelper;
this.typeHelper = typeHelper;
}
public void RegisterHelper(ScriptObject scriptObject)
{
scriptObject.Import(nameof(GetModelCtorParam), new Func<JsonSchema, string>(GetModelCtorParam));
scriptObject.Import(nameof(IfParamContains), IfParamContains);
}
public static bool IfParamContains(OpenApiOperation operation, string name)
{
var found = false;
foreach (var param in operation.Parameters)
{
if (param.Name == name)
{
found = true;
break;
}
}
return found;
}
public string GetModelCtorParam(JsonSchema schema)
{
return string.Join(", ", schema.Properties.Values
.OrderBy(p => !p.IsRequired)
.Select(p =>
{
var sp =
$"{typeHelper.GetDotNetType(p)} {generalNameHelper.GetDotNetName(p.Name, "fieldctor")}";
if (!p.IsRequired)
{
sp = $"{sp} = null";
}
return sp;
}));
}
}
}