33
44using System ;
55using System . Collections . Generic ;
6+ using System . Linq ;
67using Microsoft . OpenApi . Exceptions ;
78using Microsoft . OpenApi . Models ;
89
@@ -19,7 +20,7 @@ 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 [ ] ? ToIdentifier ( this JsonSchemaType ? schemaType )
2324 {
2425 if ( schemaType is null )
2526 {
@@ -33,20 +34,21 @@ public static class OpenApiTypeMapper
3334 /// </summary>
3435 /// <param name="schemaType"></param>
3536 /// <returns></returns>
36- public static string ? ToIdentifier ( this JsonSchemaType schemaType )
37+ public static string [ ] ToIdentifier ( 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+ var types = new List < string > ( ) ;
40+
41+ if ( schemaType . HasFlag ( JsonSchemaType . Boolean ) ) types . Add ( "boolean" ) ;
42+ if ( schemaType . HasFlag ( JsonSchemaType . Integer ) ) types . Add ( "integer" ) ;
43+ if ( schemaType . HasFlag ( JsonSchemaType . Number ) ) types . Add ( "number" ) ;
44+ if ( schemaType . HasFlag ( JsonSchemaType . String ) ) types . Add ( "string" ) ;
45+ if ( schemaType . HasFlag ( JsonSchemaType . Object ) ) types . Add ( "object" ) ;
46+ if ( schemaType . HasFlag ( JsonSchemaType . Array ) ) types . Add ( "array" ) ;
47+ if ( schemaType . HasFlag ( JsonSchemaType . Null ) ) types . Add ( "null" ) ;
48+
49+ return types . ToArray ( ) ;
4950 }
51+
5052#nullable restore
5153
5254 /// <summary>
@@ -141,7 +143,7 @@ public static OpenApiSchema MapTypeToOpenApiPrimitiveType(this Type type)
141143 }
142144
143145 /// <summary>
144- /// Maps an JsonSchema data type and format to a simple type.
146+ /// Maps a JsonSchema data type and format to a simple type.
145147 /// </summary>
146148 /// <param name="schema">The OpenApi data type</param>
147149 /// <returns>The simple type</returns>
@@ -152,21 +154,24 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
152154 {
153155 throw new ArgumentNullException ( nameof ( schema ) ) ;
154156 }
157+ var typeIdentifier = schema . Type . ToIdentifier ( ) ;
158+ var isNullable = typeIdentifier . Contains ( "null" ) ;
159+ var nonNullable = typeIdentifier . FirstOrDefault ( t => t != "null" ) ;
155160
156- var type = ( ( schema . Type & ~ JsonSchemaType . Null ) . ToIdentifier ( ) , schema . Format ? . ToLowerInvariant ( ) , schema . Type & JsonSchemaType . Null ) switch
161+ var type = ( nonNullable , schema . Format ? . ToLowerInvariant ( ) , isNullable ) switch
157162 {
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 ? ) ,
163+ ( "integer" or "number" , "int32" , true ) => typeof ( int ? ) ,
164+ ( "integer" or "number" , "int64" , true ) => typeof ( long ? ) ,
165+ ( "integer" , null , true ) => typeof ( long ? ) ,
166+ ( "number" , "float" , true ) => typeof ( float ? ) ,
167+ ( "number" , "double" , true ) => typeof ( double ? ) ,
168+ ( "number" , null , true ) => typeof ( double ? ) ,
169+ ( "number" , "decimal" , true ) => typeof ( decimal ? ) ,
170+ ( "string" , "byte" , true ) => typeof ( byte ? ) ,
171+ ( "string" , "date-time" , true ) => typeof ( DateTimeOffset ? ) ,
172+ ( "string" , "uuid" , true ) => typeof ( Guid ? ) ,
173+ ( "string" , "char" , true ) => typeof ( char ? ) ,
174+ ( "boolean" , null , true ) => typeof ( bool ? ) ,
170175 ( "boolean" , null , _ ) => typeof ( bool ) ,
171176 // integer is technically not valid with format, but we must provide some compatibility
172177 ( "integer" or "number" , "int32" , _) => typeof ( int ) ,
0 commit comments