33
44using  System ; 
55using  System . Collections . Generic ; 
6+ using  System . Linq ; 
67using  Microsoft . OpenApi . Exceptions ; 
78using  Microsoft . OpenApi . Models ; 
89
@@ -19,34 +20,61 @@ public static class OpenApiTypeMapper
1920        /// </summary> 
2021        /// <param name="schemaType"></param> 
2122        /// <returns></returns> 
22-         public  static string ?   ToIdentifier ( this  JsonSchemaType ?  schemaType ) 
23+         public  static string [ ] ?   ToIdentifiers ( this  JsonSchemaType ?  schemaType ) 
2324        { 
2425            if  ( schemaType  is  null ) 
2526            { 
2627                return  null ; 
2728            } 
28-             return  schemaType . Value . ToIdentifier ( ) ; 
29+             return  schemaType . Value . ToIdentifiers ( ) ; 
2930        } 
3031
3132        /// <summary> 
3233        /// Maps a JsonSchema data type to an identifier. 
3334        /// </summary> 
3435        /// <param name="schemaType"></param> 
3536        /// <returns></returns> 
36-         public  static string ?   ToIdentifier ( this  JsonSchemaType  schemaType ) 
37+         public  static string [ ]   ToIdentifiers ( this  JsonSchemaType  schemaType ) 
3738        { 
38-             return  schemaType  switch 
39-             { 
40-                 JsonSchemaType . Null  =>  "null" , 
41-                 JsonSchemaType . Boolean  =>  "boolean" , 
42-                 JsonSchemaType . Integer  =>  "integer" , 
43-                 JsonSchemaType . Number  =>  "number" , 
44-                 JsonSchemaType . String  =>  "string" , 
45-                 JsonSchemaType . Array  =>  "array" , 
46-                 JsonSchemaType . Object  =>  "object" , 
47-                 _ =>  null , 
48-             } ; 
39+             return  schemaType . ToIdentifiersInternal ( ) . ToArray ( ) ; 
40+         } 
41+ 
42+         private  static readonly  Dictionary < JsonSchemaType ,  string >  allSchemaTypes  =  new ( ) 
43+         { 
44+             {  JsonSchemaType . Boolean ,  "boolean"  } , 
45+             {  JsonSchemaType . Integer ,  "integer"  } , 
46+             {  JsonSchemaType . Number ,  "number"  } , 
47+             {  JsonSchemaType . String ,  "string"  } , 
48+             {  JsonSchemaType . Object ,  "object"  } , 
49+             {  JsonSchemaType . Array ,  "array"  } , 
50+             {  JsonSchemaType . Null ,  "null"  } 
51+         } ; 
52+ 
53+         private  static IEnumerable < string >  ToIdentifiersInternal ( this  JsonSchemaType  schemaType ) 
54+         { 
55+             return  allSchemaTypes . Where ( kvp =>  schemaType . HasFlag ( kvp . Key ) ) . Select ( static  kvp =>  kvp . Value ) ; 
56+         } 
57+ 
58+         /// <summary> 
59+         /// Returns the first identifier from a string array. 
60+         /// </summary> 
61+         /// <param name="schemaType"></param> 
62+         /// <returns></returns> 
63+         internal  static string  ToFirstIdentifier ( this  JsonSchemaType  schemaType ) 
64+         { 
65+             return  schemaType . ToIdentifiersInternal ( ) . First ( ) ; 
66+         } 
67+ 
68+         /// <summary> 
69+         /// Returns a single identifier from an array with only one item. 
70+         /// </summary> 
71+         /// <param name="schemaType"></param> 
72+         /// <returns></returns> 
73+         internal  static string  ToSingleIdentifier ( this  JsonSchemaType  schemaType ) 
74+         { 
75+             return  schemaType . ToIdentifiersInternal ( ) . Single ( ) ; 
4976        } 
77+ 
5078#nullable restore
5179
5280        /// <summary> 
@@ -70,6 +98,26 @@ public static JsonSchemaType ToJsonSchemaType(this string identifier)
7098            } ; 
7199        } 
72100
101+         /// <summary> 
102+         /// Converts a schema type's identifier into the enum equivalent 
103+         /// </summary> 
104+         /// <param name="identifier"></param> 
105+         /// <returns></returns> 
106+         public  static JsonSchemaType ?  ToJsonSchemaType ( this  string [ ]  identifier ) 
107+         { 
108+             if  ( identifier  ==  null ) 
109+             { 
110+                 return  null ; 
111+             } 
112+ 
113+             JsonSchemaType  type  =  0 ; 
114+             foreach  ( var  id  in  identifier ) 
115+             { 
116+                 type  |=  id . ToJsonSchemaType ( ) ; 
117+             } 
118+             return  type ; 
119+         } 
120+ 
73121        private  static readonly  Dictionary < Type ,  Func < OpenApiSchema > >  _simpleTypeToOpenApiSchema  =  new ( ) 
74122        { 
75123            [ typeof ( bool ) ]  =  ( )  =>  new ( )  {  Type  =  JsonSchemaType . Boolean  } , 
@@ -141,7 +189,7 @@ public static OpenApiSchema MapTypeToOpenApiPrimitiveType(this Type type)
141189        } 
142190
143191        /// <summary> 
144-         /// Maps an  JsonSchema data type and format to a simple type. 
192+         /// Maps a  JsonSchema data type and format to a simple type. 
145193        /// </summary> 
146194        /// <param name="schema">The OpenApi data type</param> 
147195        /// <returns>The simple type</returns> 
@@ -153,37 +201,36 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
153201                throw  new  ArgumentNullException ( nameof ( schema ) ) ; 
154202            } 
155203
156-             var  type  =  ( ( schema . Type   &   ~ JsonSchemaType . Null ) . ToIdentifier ( ) ,  schema . Format ? . ToLowerInvariant ( ) ,   schema . Type   &   JsonSchemaType . Null )  switch 
204+             var  type  =  ( schema . Type ,  schema . Format ? . ToLowerInvariant ( ) )  switch 
157205            { 
158-                 ( "integer"   or "number" ,   "int32" ,  JsonSchemaType . Null )  =>  typeof ( int ? ) , 
159-                 ( "integer"   or "number" ,   "int64" ,  JsonSchemaType . Null )  =>  typeof ( long ? ) , 
160-                 ( "integer" ,   null ,  JsonSchemaType . Null )  =>  typeof ( long ? ) , 
161-                 ( "number" ,   "float" ,  JsonSchemaType . Null )  =>  typeof ( float ? ) , 
162-                 ( "number" ,   "double" ,  JsonSchemaType . Null )  =>  typeof ( double ? ) , 
163-                 ( "number" ,   null ,  JsonSchemaType . Null )  =>  typeof ( double ? ) , 
164-                 ( "number" ,   "decimal" ,  JsonSchemaType . Null )  =>  typeof ( decimal ? ) , 
165-                 ( "string" ,   "byte" ,  JsonSchemaType . Null )  =>  typeof ( byte ? ) , 
166-                 ( "string" ,  "date-time" ,   JsonSchemaType . Null )  =>  typeof ( DateTimeOffset ? ) , 
167-                 ( "string" ,   "uuid" ,  JsonSchemaType . Null )  =>  typeof ( Guid ? ) , 
168-                 ( "string" ,   "char" ,  JsonSchemaType . Null )  =>  typeof ( char ? ) , 
169-                 ( "boolean" ,   null ,  JsonSchemaType . Null )  =>  typeof ( bool ? ) , 
170-                 ( "boolean" ,  null ,   _ )  =>  typeof ( bool ) , 
206+                 ( JsonSchemaType . Integer   |   JsonSchemaType . Null   or JsonSchemaType . Number   |  JsonSchemaType . Null ,   "int32" )  =>  typeof ( int ? ) , 
207+                 ( JsonSchemaType . Integer   |   JsonSchemaType . Null   or JsonSchemaType . Number   |  JsonSchemaType . Null ,   "int64" )  =>  typeof ( long ? ) , 
208+                 ( JsonSchemaType . Integer   |  JsonSchemaType . Null ,   null )  =>  typeof ( long ? ) , 
209+                 ( JsonSchemaType . Number   |  JsonSchemaType . Null ,   "float" )  =>  typeof ( float ? ) , 
210+                 ( JsonSchemaType . Number   |  JsonSchemaType . Null ,   "double" )  =>  typeof ( double ? ) , 
211+                 ( JsonSchemaType . Number   |  JsonSchemaType . Null ,   null )  =>  typeof ( double ? ) , 
212+                 ( JsonSchemaType . Number   |  JsonSchemaType . Null ,   "decimal" )  =>  typeof ( decimal ? ) , 
213+                 ( JsonSchemaType . String   |  JsonSchemaType . Null ,   "byte" )  =>  typeof ( byte ? ) , 
214+                 ( JsonSchemaType . String   |   JsonSchemaType . Null ,  "date-time" )  =>  typeof ( DateTimeOffset ? ) , 
215+                 ( JsonSchemaType . String   |  JsonSchemaType . Null ,   "uuid" )  =>  typeof ( Guid ? ) , 
216+                 ( JsonSchemaType . String   |  JsonSchemaType . Null ,   "char" )  =>  typeof ( char ? ) , 
217+                 ( JsonSchemaType . Boolean   |  JsonSchemaType . Null ,   null )  =>  typeof ( bool ? ) , 
218+                 ( JsonSchemaType . Boolean ,  null )  =>  typeof ( bool ) , 
171219                // integer is technically not valid with format, but we must provide some compatibility 
172-                 ( "integer"  or "number" ,  "int32" ,  _)  =>  typeof ( int ) , 
173-                 ( "integer"  or "number" ,  "int64" ,  _)  =>  typeof ( long ) , 
174-                 ( "integer" ,  null ,  _ )  =>  typeof ( long ) , 
175-                 ( "number" ,  "float" ,  _ )  =>  typeof ( float ) , 
176-                 ( "number" ,  "double" ,  _ )  =>  typeof ( double ) , 
177-                 ( "number" ,  "decimal" ,  _ )  =>  typeof ( decimal ) , 
178-                 ( "number" ,  null ,  _ )  =>  typeof ( double ) , 
179-                 ( "string" ,  "byte" ,  _ )  =>  typeof ( byte ) , 
180-                 ( "string" ,  "date-time" ,  _ )  =>  typeof ( DateTimeOffset ) , 
181-                 ( "string" ,  "uuid" ,  _ )  =>  typeof ( Guid ) , 
182-                 ( "string" ,  "duration" ,  _ )  =>  typeof ( TimeSpan ) , 
183-                 ( "string" ,  "char" ,  _ )  =>  typeof ( char ) , 
184-                 ( "string" ,  null ,  _ )  =>  typeof ( string ) , 
185-                 ( "object" ,  null ,  _ )  =>  typeof ( object ) , 
186-                 ( "string" ,  "uri" ,  _ )  =>  typeof ( Uri ) , 
220+                 ( JsonSchemaType . Integer  or JsonSchemaType . Number ,  "int32" )  =>  typeof ( int ) , 
221+                 ( JsonSchemaType . Integer  or JsonSchemaType . Number ,  "int64" )  =>  typeof ( long ) , 
222+                 ( JsonSchemaType . Integer ,  null )  =>  typeof ( long ) , 
223+                 ( JsonSchemaType . Number ,  "float" )  =>  typeof ( float ) , 
224+                 ( JsonSchemaType . Number ,  "double" )  =>  typeof ( double ) , 
225+                 ( JsonSchemaType . Number ,  "decimal" )  =>  typeof ( decimal ) , 
226+                 ( JsonSchemaType . Number ,  null )  =>  typeof ( double ) , 
227+                 ( JsonSchemaType . String ,  "byte" )  =>  typeof ( byte ) , 
228+                 ( JsonSchemaType . String ,  "date-time" )  =>  typeof ( DateTimeOffset ) , 
229+                 ( JsonSchemaType . String ,  "uuid" )  =>  typeof ( Guid ) , 
230+                 ( JsonSchemaType . String ,  "char" )  =>  typeof ( char ) , 
231+                 ( JsonSchemaType . String ,  null )  =>  typeof ( string ) , 
232+                 ( JsonSchemaType . Object ,  null )  =>  typeof ( object ) , 
233+                 ( JsonSchemaType . String ,  "uri" )  =>  typeof ( Uri ) , 
187234                _ =>  typeof ( string ) , 
188235            } ; 
189236
0 commit comments