@@ -12,40 +12,81 @@ namespace Microsoft.OpenApi.Extensions
12
12
/// </summary>
13
13
public static class OpenApiTypeMapper
14
14
{
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
+
15
56
private static readonly Dictionary < Type , Func < OpenApiSchema > > _simpleTypeToOpenApiSchema = new ( )
16
57
{
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 } ,
30
71
31
72
// 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 } ,
45
86
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 }
49
90
} ;
50
91
51
92
/// <summary>
@@ -79,7 +120,7 @@ public static OpenApiSchema MapTypeToOpenApiPrimitiveType(this Type type)
79
120
80
121
return _simpleTypeToOpenApiSchema . TryGetValue ( type , out var result )
81
122
? result ( )
82
- : new ( ) { Type = "string" } ;
123
+ : new ( ) { Type = JsonSchemaType . String } ;
83
124
}
84
125
85
126
/// <summary>
@@ -95,7 +136,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
95
136
throw new ArgumentNullException ( nameof ( schema ) ) ;
96
137
}
97
138
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
99
140
{
100
141
( "boolean" , null , false ) => typeof ( bool ) ,
101
142
( "integer" , "int32" , false ) => typeof ( int ) ,
0 commit comments