-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwaggerExtensions.cs
169 lines (147 loc) · 5.43 KB
/
SwaggerExtensions.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// -----------------------------------------------------------------------
// <copyright file="SwaggerExtensions.cs" company="Microsoft Corp.">
// Copyright (c) Microsoft Corp. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
namespace Microsoft.ArmSwashbuckleStarterKit.Swagger
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.ArmSwashbuckleStarterKit.Attributes;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using Newtonsoft.Json;
/// <summary>
/// Extension methods used by Swashbuckle filters. Almost certainly includes unused code. This was originally copied from another repo.
/// </summary>
public static class SwaggerExtensions
{
public static string Uncapitalize(this string the_string)
{
if (the_string == null)
{
return the_string;
}
else if (the_string.Length == 1)
{
return the_string.ToLower();
}
return (Char.ToLower(the_string[0]) + the_string.Substring(1));
}
public static string SerializeAsV2String(this OpenApiSchema schema)
{
using var stringWriter = new StringWriter();
var jsonWriter = new OpenApiJsonWriter(stringWriter);
schema.SerializeAsV2(jsonWriter);
return stringWriter.ToString();
}
/// <summary>
/// Gets the first generic argument that appears in a list of inheritances
/// </summary>
public static Type GetInheritedGeneric(this Type curType)
{
while (curType != typeof(object))
{
if (curType.GenericTypeArguments.Count() != 0)
{
return curType.GenericTypeArguments.First();
}
curType = curType.BaseType;
}
return null;
}
public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
{
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
public static bool IsSubclassOfRawGeneric(this Type toCheck, string genericName)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (genericName == cur.Name)
{
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
/// <summary>
/// Gets the last type in a series of generics
/// </summary>
public static Type GetInnerGeneric(this Type type)
{
while (type.GenericTypeArguments.Count() != 0)
{
type = type.GenericTypeArguments[0];
}
return type;
}
public static IDictionary<string, OpenApiSchema> GetPropertySchemas(this OpenApiSchema schema)
{
if (schema.AllOf.Count() != 0)
{
return schema.Properties.Concat(schema.AllOf.SelectMany(schema => schema.GetPropertySchemas()))
.ToDictionary(kv => kv.Key, kv => kv.Value);
}
return schema.Properties
.ToDictionary(kv => kv.Key, kv => kv.Value);
}
public static void SetPropertySchemas(this OpenApiSchema schema, IDictionary<string, OpenApiSchema> propertyBag)
{
foreach (var key in schema.Properties.Keys.ToList())
{
if (propertyBag.ContainsKey(key))
{
schema.Properties[key] = propertyBag[key];
propertyBag.Remove(key);
}
else
{
// If the propertyBag does not contain the key, then it was explicitly
// removed by the user. Reflect that removal in the original
schema.Properties.Remove(key);
}
}
foreach (var innerSchema in schema.AllOf)
{
innerSchema.SetPropertySchemas(propertyBag);
}
}
public static string GetOperationId(string inheritedGenericName, string opName)
{
return $"{inheritedGenericName}_{opName}";
}
public static IEnumerable<(string Title, string FilePath)> GetSwaggerExampleReferences(MethodInfo method)
{
foreach (var attribute in method.GetCustomAttributes<SwaggerLinkToExampleAttribute>())
{
var title = method.Name;
var fileName = attribute.OpName;
if (fileName == null)
{
fileName = title;
}
else
{
title = attribute.OpName;
}
var path = $"{SwaggerConstants.ExampleBasePath}/{fileName}.json";
yield return (title, path);
}
}
}
}