6
6
using Microsoft . OpenApi . Models ;
7
7
8
8
namespace Kiota . Builder . Extensions ;
9
+
9
10
public static class OpenApiSchemaExtensions
10
11
{
11
12
private static readonly Func < OpenApiSchema , IList < OpenApiSchema > > classNamesFlattener = x =>
12
13
( x . AnyOf ?? Enumerable . Empty < OpenApiSchema > ( ) ) . Union ( x . AllOf ) . Union ( x . OneOf ) . ToList ( ) ;
14
+
13
15
public static IEnumerable < string > GetSchemaNames ( this OpenApiSchema schema )
14
16
{
15
17
if ( schema == null )
@@ -30,13 +32,15 @@ public static IEnumerable<string> GetSchemaNames(this OpenApiSchema schema)
30
32
return new [ ] { schema . Xml . Name } ;
31
33
return Enumerable . Empty < string > ( ) ;
32
34
}
35
+
33
36
internal static IEnumerable < OpenApiSchema > FlattenSchemaIfRequired ( this IList < OpenApiSchema > schemas , Func < OpenApiSchema , IList < OpenApiSchema > > subsequentGetter )
34
37
{
35
38
if ( schemas is null ) return Enumerable . Empty < OpenApiSchema > ( ) ;
36
39
return schemas . Count == 1 ?
37
40
schemas . FlattenEmptyEntries ( subsequentGetter , 1 ) :
38
41
schemas ;
39
42
}
43
+
40
44
private static IEnumerable < string > FlattenIfRequired ( this IList < OpenApiSchema > schemas , Func < OpenApiSchema , IList < OpenApiSchema > > subsequentGetter )
41
45
{
42
46
return schemas . FlattenSchemaIfRequired ( subsequentGetter ) . Where ( static x => ! string . IsNullOrEmpty ( x . Title ) ) . Select ( static x => x . Title ) ;
@@ -68,6 +72,12 @@ public static bool IsObject(this OpenApiSchema? schema)
68
72
{
69
73
return "object" . Equals ( schema ? . Type , StringComparison . OrdinalIgnoreCase ) ;
70
74
}
75
+
76
+ public static bool IsScalar ( this OpenApiSchema ? schema )
77
+ {
78
+ return schema ? . IsObject ( ) == false && schema ? . Reference ? . Id == null ;
79
+ }
80
+
71
81
public static bool IsInclusiveUnion ( this OpenApiSchema ? schema )
72
82
{
73
83
return schema ? . AnyOf ? . Count ( static x => IsSemanticallyMeaningful ( x , true ) ) > 1 ;
@@ -103,10 +113,12 @@ public static bool IsExclusiveUnion(this OpenApiSchema? schema)
103
113
return schema ? . OneOf ? . Count ( static x => IsSemanticallyMeaningful ( x , true ) ) > 1 ;
104
114
// so we don't consider one of object/nullable as a union type
105
115
}
116
+
106
117
private static readonly HashSet < string > oDataTypes = new ( StringComparer . OrdinalIgnoreCase ) {
107
118
"number" ,
108
119
"integer" ,
109
120
} ;
121
+
110
122
public static bool IsODataPrimitiveType ( this OpenApiSchema schema )
111
123
{
112
124
return schema . IsExclusiveUnion ( ) &&
@@ -121,18 +133,21 @@ public static bool IsODataPrimitiveType(this OpenApiSchema schema)
121
133
schema . AnyOf . Count ( static x => oDataTypes . Contains ( x . Type ) ) == 1 &&
122
134
schema . AnyOf . Count ( static x => "string" . Equals ( x . Type , StringComparison . OrdinalIgnoreCase ) ) == 1 ;
123
135
}
136
+
124
137
public static bool IsEnum ( this OpenApiSchema schema )
125
138
{
126
139
if ( schema is null ) return false ;
127
140
return schema . Enum . OfType < OpenApiString > ( ) . Any ( static x => ! string . IsNullOrEmpty ( x . Value ) ) &&
128
141
( string . IsNullOrEmpty ( schema . Type ) || "string" . Equals ( schema . Type , StringComparison . OrdinalIgnoreCase ) ) ; // number and boolean enums are not supported
129
142
}
143
+
130
144
public static bool IsComposedEnum ( this OpenApiSchema schema )
131
145
{
132
146
if ( schema is null ) return false ;
133
147
return schema . AnyOf . Count ( static x => ! x . IsSemanticallyMeaningful ( true ) ) == 1 && schema . AnyOf . Count ( static x => x . IsEnum ( ) ) == 1 ||
134
148
schema . OneOf . Count ( static x => ! x . IsSemanticallyMeaningful ( true ) ) == 1 && schema . OneOf . Count ( static x => x . IsEnum ( ) ) == 1 ;
135
149
}
150
+
136
151
public static bool IsSemanticallyMeaningful ( this OpenApiSchema schema , bool ignoreNullableObjects = false )
137
152
{
138
153
if ( schema is null ) return false ;
@@ -145,6 +160,7 @@ public static bool IsSemanticallyMeaningful(this OpenApiSchema schema, bool igno
145
160
! string . IsNullOrEmpty ( schema . Format ) ||
146
161
! string . IsNullOrEmpty ( schema . Reference ? . Id ) ;
147
162
}
163
+
148
164
public static IEnumerable < string > GetSchemaReferenceIds ( this OpenApiSchema schema , HashSet < OpenApiSchema > ? visitedSchemas = null )
149
165
{
150
166
visitedSchemas ??= new ( ) ;
@@ -173,6 +189,7 @@ public static IEnumerable<string> GetSchemaReferenceIds(this OpenApiSchema schem
173
189
174
190
return Enumerable . Empty < string > ( ) ;
175
191
}
192
+
176
193
private static IEnumerable < OpenApiSchema > FlattenEmptyEntries ( this IEnumerable < OpenApiSchema > schemas , Func < OpenApiSchema , IList < OpenApiSchema > > subsequentGetter , int ? maxDepth = default )
177
194
{
178
195
if ( schemas == null ) return Enumerable . Empty < OpenApiSchema > ( ) ;
@@ -205,6 +222,7 @@ private static IEnumerable<OpenApiSchema> FlattenEmptyEntries(this IEnumerable<O
205
222
}
206
223
return result ;
207
224
}
225
+
208
226
internal static string GetDiscriminatorPropertyName ( this OpenApiSchema schema )
209
227
{
210
228
if ( schema == null )
@@ -222,6 +240,7 @@ internal static string GetDiscriminatorPropertyName(this OpenApiSchema schema)
222
240
223
241
return string . Empty ;
224
242
}
243
+
225
244
internal static IEnumerable < KeyValuePair < string , string > > GetDiscriminatorMappings ( this OpenApiSchema schema , ConcurrentDictionary < string , ConcurrentDictionary < string , bool > > inheritanceIndex )
226
245
{
227
246
if ( schema == null )
@@ -245,7 +264,9 @@ internal static IEnumerable<KeyValuePair<string, string>> GetDiscriminatorMappin
245
264
return schema . Discriminator
246
265
. Mapping ;
247
266
}
267
+
248
268
private static readonly Func < OpenApiSchema , bool > allOfEvaluatorForMappings = static x => x . Discriminator ? . Mapping . Any ( ) ?? false ;
269
+
249
270
private static IEnumerable < string > GetAllInheritanceSchemaReferences ( string currentReferenceId , ConcurrentDictionary < string , ConcurrentDictionary < string , bool > > inheritanceIndex )
250
271
{
251
272
ArgumentException . ThrowIfNullOrEmpty ( currentReferenceId ) ;
@@ -255,4 +276,3 @@ private static IEnumerable<string> GetAllInheritanceSchemaReferences(string curr
255
276
return Enumerable . Empty < string > ( ) ;
256
277
}
257
278
}
258
-
0 commit comments