@@ -20,13 +20,13 @@ public static class OpenApiTypeMapper
2020 /// </summary>
2121 /// <param name="schemaType"></param>
2222 /// <returns></returns>
23- public static string [ ] ? ToIdentifier ( this JsonSchemaType ? schemaType )
23+ public static string [ ] ? ToIdentifiers ( this JsonSchemaType ? schemaType )
2424 {
2525 if ( schemaType is null )
2626 {
2727 return null ;
2828 }
29- return schemaType . Value . ToIdentifier ( ) ;
29+ return schemaType . Value . ToIdentifiers ( ) ;
3030 }
3131
3232 /// <summary>
@@ -56,10 +56,24 @@ public static string[] ToIdentifiers(this JsonSchemaType schemaType)
5656 /// <returns></returns>
5757 internal static string ToFirstIdentifier ( this JsonSchemaType schemaType )
5858 {
59- var identifier = schemaType . ToIdentifier ( ) ;
59+ var identifier = schemaType . ToIdentifiers ( ) ;
6060 return identifier [ 0 ] ;
6161 }
6262
63+ /// <summary>
64+ /// Returns a single identifier from an array with only one item.
65+ /// </summary>
66+ /// <param name="schemaType"></param>
67+ /// <returns></returns>
68+ public static string ToSingleIdentifier ( this JsonSchemaType schemaType )
69+ {
70+ if ( schemaType . ToIdentifiers ( ) . Length != 1 )
71+ {
72+ throw new OpenApiException ( $ "Schema type { schemaType } must have exactly one identifier.") ;
73+ }
74+ return schemaType . ToFirstIdentifier ( ) ;
75+ }
76+
6377#nullable restore
6478
6579 /// <summary>
@@ -185,40 +199,37 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
185199 {
186200 throw new ArgumentNullException ( nameof ( schema ) ) ;
187201 }
188- var isNullable = ( schema . Type & JsonSchemaType . Null ) == JsonSchemaType . Null ;
189- var nonNullable = ( schema . Type & ~ JsonSchemaType . Null ) ? . FirstIdentifier ( ) ;
190202
191- var type = ( nonNullable , schema . Format ? . ToLowerInvariant ( ) , isNullable ) switch
203+ var type = ( schema . Type , schema . Format ? . ToLowerInvariant ( ) ) switch
192204 {
193- ( "integer" or "number" , "int32" , true ) => typeof ( int ? ) ,
194- ( "integer" or "number" , "int64" , true ) => typeof ( long ? ) ,
195- ( "integer" , null , true ) => typeof ( long ? ) ,
196- ( "number" , "float" , true ) => typeof ( float ? ) ,
197- ( "number" , "double" , true ) => typeof ( double ? ) ,
198- ( "number" , null , true ) => typeof ( double ? ) ,
199- ( "number" , "decimal" , true ) => typeof ( decimal ? ) ,
200- ( "string" , "byte" , true ) => typeof ( byte ? ) ,
201- ( "string" , "date-time" , true ) => typeof ( DateTimeOffset ? ) ,
202- ( "string" , "uuid" , true ) => typeof ( Guid ? ) ,
203- ( "string" , "char" , true ) => typeof ( char ? ) ,
204- ( "boolean" , null , true ) => typeof ( bool ? ) ,
205- ( "boolean" , null , _ ) => typeof ( bool ) ,
205+ ( JsonSchemaType . Integer | JsonSchemaType . Null or JsonSchemaType . Number | JsonSchemaType . Null , "int32" ) => typeof ( int ? ) ,
206+ ( JsonSchemaType . Integer | JsonSchemaType . Null or JsonSchemaType . Number | JsonSchemaType . Null , "int64" ) => typeof ( long ? ) ,
207+ ( JsonSchemaType . Integer | JsonSchemaType . Null , null ) => typeof ( long ? ) ,
208+ ( JsonSchemaType . Number | JsonSchemaType . Null , "float" ) => typeof ( float ? ) ,
209+ ( JsonSchemaType . Number | JsonSchemaType . Null , "double" ) => typeof ( double ? ) ,
210+ ( JsonSchemaType . Number | JsonSchemaType . Null , null ) => typeof ( double ? ) ,
211+ ( JsonSchemaType . Number | JsonSchemaType . Null , "decimal" ) => typeof ( decimal ? ) ,
212+ ( JsonSchemaType . String | JsonSchemaType . Null , "byte" ) => typeof ( byte ? ) ,
213+ ( JsonSchemaType . String | JsonSchemaType . Null , "date-time" ) => typeof ( DateTimeOffset ? ) ,
214+ ( JsonSchemaType . String | JsonSchemaType . Null , "uuid" ) => typeof ( Guid ? ) ,
215+ ( JsonSchemaType . String | JsonSchemaType . Null , "char" ) => typeof ( char ? ) ,
216+ ( JsonSchemaType . Boolean | JsonSchemaType . Null , null ) => typeof ( bool ? ) ,
217+ ( JsonSchemaType . Boolean , null ) => typeof ( bool ) ,
206218 // integer is technically not valid with format, but we must provide some compatibility
207- ( "integer" or "number" , "int32" , _) => typeof ( int ) ,
208- ( "integer" or "number" , "int64" , _) => typeof ( long ) ,
209- ( "integer" , null , _ ) => typeof ( long ) ,
210- ( "number" , "float" , _ ) => typeof ( float ) ,
211- ( "number" , "double" , _ ) => typeof ( double ) ,
212- ( "number" , "decimal" , _ ) => typeof ( decimal ) ,
213- ( "number" , null , _ ) => typeof ( double ) ,
214- ( "string" , "byte" , _ ) => typeof ( byte ) ,
215- ( "string" , "date-time" , _ ) => typeof ( DateTimeOffset ) ,
216- ( "string" , "uuid" , _ ) => typeof ( Guid ) ,
217- ( "string" , "duration" , _ ) => typeof ( TimeSpan ) ,
218- ( "string" , "char" , _ ) => typeof ( char ) ,
219- ( "string" , null , _ ) => typeof ( string ) ,
220- ( "object" , null , _ ) => typeof ( object ) ,
221- ( "string" , "uri" , _ ) => typeof ( Uri ) ,
219+ ( JsonSchemaType . Integer or JsonSchemaType . Number , "int32" ) => typeof ( int ) ,
220+ ( JsonSchemaType . Integer or JsonSchemaType . Number , "int64" ) => typeof ( long ) ,
221+ ( JsonSchemaType . Integer , null ) => typeof ( long ) ,
222+ ( JsonSchemaType . Number , "float" ) => typeof ( float ) ,
223+ ( JsonSchemaType . Number , "double" ) => typeof ( double ) ,
224+ ( JsonSchemaType . Number , "decimal" ) => typeof ( decimal ) ,
225+ ( JsonSchemaType . Number , null ) => typeof ( double ) ,
226+ ( JsonSchemaType . String , "byte" ) => typeof ( byte ) ,
227+ ( JsonSchemaType . String , "date-time" ) => typeof ( DateTimeOffset ) ,
228+ ( JsonSchemaType . String , "uuid" ) => typeof ( Guid ) ,
229+ ( JsonSchemaType . String , "char" ) => typeof ( char ) ,
230+ ( JsonSchemaType . String , null ) => typeof ( string ) ,
231+ ( JsonSchemaType . Object , null ) => typeof ( object ) ,
232+ ( JsonSchemaType . String , "uri" ) => typeof ( Uri ) ,
222233 _ => typeof ( string ) ,
223234 } ;
224235
0 commit comments