@@ -42,7 +42,7 @@ public static class OpenApiTypeMapper
4242 [ typeof ( DateTimeOffset ? ) ] = ( ) => new OpenApiSchema { Type = "string" , Format = "date-time" , Nullable = true } ,
4343 [ typeof ( Guid ? ) ] = ( ) => new OpenApiSchema { Type = "string" , Format = "uuid" , Nullable = true } ,
4444 [ typeof ( char ? ) ] = ( ) => new OpenApiSchema { Type = "string" , Nullable = true } ,
45-
45+
4646 [ typeof ( Uri ) ] = ( ) => new OpenApiSchema { Type = "string" } , // Uri is treated as simple string
4747 [ typeof ( string ) ] = ( ) => new OpenApiSchema { Type = "string" } ,
4848 [ typeof ( object ) ] = ( ) => new OpenApiSchema { Type = "object" }
@@ -81,5 +81,50 @@ public static OpenApiSchema MapTypeToOpenApiPrimitiveType(this Type type)
8181 ? result ( )
8282 : new OpenApiSchema { Type = "string" } ;
8383 }
84+
85+ /// <summary>
86+ /// Maps an OpenAPI data type and format to a simple type.
87+ /// </summary>
88+ /// <param name="schema">The OpenApi data type</param>
89+ /// <returns>The simple type</returns>
90+ /// <exception cref="ArgumentNullException"></exception>
91+ public static Type MapOpenApiPrimitiveTypeToSimpleType ( this OpenApiSchema schema )
92+ {
93+ if ( schema == null )
94+ {
95+ throw new ArgumentNullException ( nameof ( schema ) ) ;
96+ }
97+
98+ var type = ( schema . Type , schema . Format , schema . Nullable ) switch
99+ {
100+ ( "boolean" , null , false ) => typeof ( bool ) ,
101+ ( "integer" , "int32" , false ) => typeof ( int ) ,
102+ ( "integer" , "int64" , false ) => typeof ( long ) ,
103+ ( "number" , "float" , false ) => typeof ( float ) ,
104+ ( "number" , "double" , false ) => typeof ( double ) ,
105+ ( "number" , "decimal" , false ) => typeof ( decimal ) ,
106+ ( "string" , "byte" , false ) => typeof ( byte ) ,
107+ ( "string" , "date-time" , false ) => typeof ( DateTimeOffset ) ,
108+ ( "string" , "uuid" , false ) => typeof ( Guid ) ,
109+ ( "string" , "duration" , false ) => typeof ( TimeSpan ) ,
110+ ( "string" , "char" , false ) => typeof ( char ) ,
111+ ( "string" , null , false ) => typeof ( string ) ,
112+ ( "object" , null , false ) => typeof ( object ) ,
113+ ( "string" , "uri" , false ) => typeof ( Uri ) ,
114+ ( "integer" , "int32" , true ) => typeof ( int ? ) ,
115+ ( "integer" , "int64" , true ) => typeof ( long ? ) ,
116+ ( "number" , "float" , true ) => typeof ( float ? ) ,
117+ ( "number" , "double" , true ) => typeof ( double ? ) ,
118+ ( "number" , "decimal" , true ) => typeof ( decimal ? ) ,
119+ ( "string" , "byte" , true ) => typeof ( byte ? ) ,
120+ ( "string" , "date-time" , true ) => typeof ( DateTimeOffset ? ) ,
121+ ( "string" , "uuid" , true ) => typeof ( Guid ? ) ,
122+ ( "string" , "char" , true ) => typeof ( char ? ) ,
123+ ( "boolean" , null , true ) => typeof ( bool ? ) ,
124+ _ => typeof ( string ) ,
125+ } ;
126+
127+ return type ;
128+ }
84129 }
85130}
0 commit comments