@@ -12,40 +12,81 @@ namespace Microsoft.OpenApi.Extensions
1212 /// </summary>
1313 public static class OpenApiTypeMapper
1414 {
15+ /// <summary>
16+ /// Maps a JsonSchema data type to an identifier.
17+ /// </summary>
18+ /// <param name="schemaType"></param>
19+ /// <returns></returns>
20+ public static string ToIdentifier ( JsonSchemaType ? schemaType )
21+ {
22+ return schemaType switch
23+ {
24+ JsonSchemaType . Null => "null" ,
25+ JsonSchemaType . Boolean => "boolean" ,
26+ JsonSchemaType . Integer => "integer" ,
27+ JsonSchemaType . Number => "number" ,
28+ JsonSchemaType . String => "string" ,
29+ JsonSchemaType . Array => "array" ,
30+ JsonSchemaType . Object => "object" ,
31+ _ => null ,
32+ } ;
33+ }
34+
35+ /// <summary>
36+ /// Converts a schema type's identifier into the enum equivalent
37+ /// </summary>
38+ /// <param name="identifier"></param>
39+ /// <returns></returns>
40+ public static JsonSchemaType IdentifierToEnumType ( string identifier )
41+ {
42+ return identifier switch
43+ {
44+ "null" => JsonSchemaType . Null ,
45+ "boolean" => JsonSchemaType . Boolean ,
46+ "integer" or "int" => JsonSchemaType . Integer ,
47+ "number" or "double" => JsonSchemaType . Number ,
48+ "string" => JsonSchemaType . String ,
49+ "array" => JsonSchemaType . Array ,
50+ "object" => JsonSchemaType . Object ,
51+ "file" => JsonSchemaType . String , // File is treated as string
52+ _ => JsonSchemaType . Any ,
53+ } ;
54+ }
55+
1556 private static readonly Dictionary < Type , Func < OpenApiSchema > > _simpleTypeToOpenApiSchema = new ( )
1657 {
17- [ typeof ( bool ) ] = ( ) => new ( ) { Type = "boolean" } ,
18- [ typeof ( byte ) ] = ( ) => new ( ) { Type = "string" , Format = "byte" } ,
19- [ typeof ( int ) ] = ( ) => new ( ) { Type = "integer" , Format = "int32" } ,
20- [ typeof ( uint ) ] = ( ) => new ( ) { Type = "integer" , Format = "int32" } ,
21- [ typeof ( long ) ] = ( ) => new ( ) { Type = "integer" , Format = "int64" } ,
22- [ typeof ( ulong ) ] = ( ) => new ( ) { Type = "integer" , Format = "int64" } ,
23- [ typeof ( float ) ] = ( ) => new ( ) { Type = "number" , Format = "float" } ,
24- [ typeof ( double ) ] = ( ) => new ( ) { Type = "number" , Format = "double" } ,
25- [ typeof ( decimal ) ] = ( ) => new ( ) { Type = "number" , Format = "double" } ,
26- [ typeof ( DateTime ) ] = ( ) => new ( ) { Type = "string" , Format = "date-time" } ,
27- [ typeof ( DateTimeOffset ) ] = ( ) => new ( ) { Type = "string" , Format = "date-time" } ,
28- [ typeof ( Guid ) ] = ( ) => new ( ) { Type = "string" , Format = "uuid" } ,
29- [ typeof ( char ) ] = ( ) => new ( ) { Type = "string" } ,
58+ [ typeof ( bool ) ] = ( ) => new ( ) { Type = JsonSchemaType . Boolean } ,
59+ [ typeof ( byte ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "byte" } ,
60+ [ typeof ( int ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int32" } ,
61+ [ typeof ( uint ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int32" } ,
62+ [ typeof ( long ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int64" } ,
63+ [ typeof ( ulong ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int64" } ,
64+ [ typeof ( float ) ] = ( ) => new ( ) { Type = JsonSchemaType . Number , Format = "float" } ,
65+ [ typeof ( double ) ] = ( ) => new ( ) { Type = JsonSchemaType . Number , Format = "double" } ,
66+ [ typeof ( decimal ) ] = ( ) => new ( ) { Type = JsonSchemaType . Number , Format = "double" } ,
67+ [ typeof ( DateTime ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "date-time" } ,
68+ [ typeof ( DateTimeOffset ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "date-time" } ,
69+ [ typeof ( Guid ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "uuid" } ,
70+ [ typeof ( char ) ] = ( ) => new ( ) { Type = JsonSchemaType . String } ,
3071
3172 // Nullable types
32- [ typeof ( bool ? ) ] = ( ) => new ( ) { Type = "boolean" , Nullable = true } ,
33- [ typeof ( byte ? ) ] = ( ) => new ( ) { Type = "string" , Format = "byte" , Nullable = true } ,
34- [ typeof ( int ? ) ] = ( ) => new ( ) { Type = "integer" , Format = "int32" , Nullable = true } ,
35- [ typeof ( uint ? ) ] = ( ) => new ( ) { Type = "integer" , Format = "int32" , Nullable = true } ,
36- [ typeof ( long ? ) ] = ( ) => new ( ) { Type = "integer" , Format = "int64" , Nullable = true } ,
37- [ typeof ( ulong ? ) ] = ( ) => new ( ) { Type = "integer" , Format = "int64" , Nullable = true } ,
38- [ typeof ( float ? ) ] = ( ) => new ( ) { Type = "number" , Format = "float" , Nullable = true } ,
39- [ typeof ( double ? ) ] = ( ) => new ( ) { Type = "number" , Format = "double" , Nullable = true } ,
40- [ typeof ( decimal ? ) ] = ( ) => new ( ) { Type = "number" , Format = "double" , Nullable = true } ,
41- [ typeof ( DateTime ? ) ] = ( ) => new ( ) { Type = "string" , Format = "date-time" , Nullable = true } ,
42- [ typeof ( DateTimeOffset ? ) ] = ( ) => new ( ) { Type = "string" , Format = "date-time" , Nullable = true } ,
43- [ typeof ( Guid ? ) ] = ( ) => new ( ) { Type = "string" , Format = "uuid" , Nullable = true } ,
44- [ typeof ( char ? ) ] = ( ) => new ( ) { Type = "string" , Nullable = true } ,
73+ [ typeof ( bool ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Boolean , Nullable = true } ,
74+ [ typeof ( byte ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "byte" , Nullable = true } ,
75+ [ typeof ( int ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int32" , Nullable = true } ,
76+ [ typeof ( uint ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int32" , Nullable = true } ,
77+ [ typeof ( long ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int64" , Nullable = true } ,
78+ [ typeof ( ulong ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Integer , Format = "int64" , Nullable = true } ,
79+ [ typeof ( float ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Number , Format = "float" , Nullable = true } ,
80+ [ typeof ( double ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Number , Format = "double" , Nullable = true } ,
81+ [ typeof ( decimal ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . Number , Format = "double" , Nullable = true } ,
82+ [ typeof ( DateTime ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "date-time" , Nullable = true } ,
83+ [ typeof ( DateTimeOffset ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "date-time" , Nullable = true } ,
84+ [ typeof ( Guid ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "uuid" , Nullable = true } ,
85+ [ typeof ( char ? ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Nullable = true } ,
4586
46- [ typeof ( Uri ) ] = ( ) => new ( ) { Type = "string" , Format = "uri" } , // Uri is treated as simple string
47- [ typeof ( string ) ] = ( ) => new ( ) { Type = "string" } ,
48- [ typeof ( object ) ] = ( ) => new ( ) { Type = "object" }
87+ [ typeof ( Uri ) ] = ( ) => new ( ) { Type = JsonSchemaType . String , Format = "uri" } , // Uri is treated as simple string
88+ [ typeof ( string ) ] = ( ) => new ( ) { Type = JsonSchemaType . String } ,
89+ [ typeof ( object ) ] = ( ) => new ( ) { Type = JsonSchemaType . Object }
4990 } ;
5091
5192 /// <summary>
@@ -79,7 +120,7 @@ public static OpenApiSchema MapTypeToOpenApiPrimitiveType(this Type type)
79120
80121 return _simpleTypeToOpenApiSchema . TryGetValue ( type , out var result )
81122 ? result ( )
82- : new ( ) { Type = "string" } ;
123+ : new ( ) { Type = JsonSchemaType . String } ;
83124 }
84125
85126 /// <summary>
@@ -95,7 +136,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
95136 throw new ArgumentNullException ( nameof ( schema ) ) ;
96137 }
97138
98- var type = ( schema . Type ? . ToString ( ) . ToLowerInvariant ( ) , schema . Format ? . ToLowerInvariant ( ) , schema . Nullable ) switch
139+ var type = ( ToIdentifier ( schema . Type ) , schema . Format ? . ToLowerInvariant ( ) , schema . Nullable ) switch
99140 {
100141 ( "boolean" , null , false ) => typeof ( bool ) ,
101142 ( "integer" , "int32" , false ) => typeof ( int ) ,
0 commit comments