-
-
Save INTERNALINTERFERENCE/ba70db64bc0810008cec94d485225af0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static public class CustomTypeMapper | |
{ | |
static private readonly Dictionary<Type, Func<OpenApiSchema>> _typeToOpenApiSchema = new() | |
{ | |
[typeof(bool)] = () => new OpenApiSchema { Type = "boolean" }, | |
[typeof(byte)] = () => new OpenApiSchema { Type = "string", Format = "byte" }, | |
[typeof(int)] = () => new OpenApiSchema { Type = "integer", Format = "int32" }, | |
[typeof(uint)] = () => new OpenApiSchema { Type = "integer", Format = "int32" }, | |
[typeof(long)] = () => new OpenApiSchema { Type = "integer", Format = "int64" }, | |
[typeof(ulong)] = () => new OpenApiSchema { Type = "integer", Format = "int64" }, | |
[typeof(float)] = () => new OpenApiSchema { Type = "number", Format = "float" }, | |
[typeof(double)] = () => new OpenApiSchema { Type = "number", Format = "double" }, | |
[typeof(decimal)] = () => new OpenApiSchema { Type = "number", Format = "double" }, | |
[typeof(LocalDateTime)] = () => new OpenApiSchema{Type = "string", Format = "date-time"}, | |
[typeof(DateTime)] = () => new OpenApiSchema { Type = "string", Format = "date-time" }, | |
[typeof(DateTimeOffset)] = () => new OpenApiSchema { Type = "string", Format = "date-time" }, | |
[typeof(LocalDateTimeInterval)] = () => new OpenApiSchema { Type = "string", Example = new OpenApiString("2023-06-23T00:00:00/2023-06-30T00:00:00") }, | |
[typeof(Guid)] = () => new OpenApiSchema { Type = "string", Format = "uuid" }, | |
[typeof(char)] = () => new OpenApiSchema { Type = "string" }, | |
[typeof(Uri)] = () => new OpenApiSchema { Type = "string", Format = "uri" }, | |
[typeof(string)] = () => new OpenApiSchema { Type = "string" }, | |
[typeof(object)] = () => new OpenApiSchema { Type = "object" }, | |
[typeof(JObject)] = () => new OpenApiSchema { Type = "object" }, | |
[typeof(TimeSpan)] = () => new OpenApiSchema {Type = "string", Example = new OpenApiString( "00:00:00" )}, | |
[typeof(Version)] = () => new OpenApiSchema{Type = "string", Example = new OpenApiString( "1.0" )}, | |
[typeof(IPAddress)] = () => new OpenApiSchema{Type = "string", Example = new OpenApiString( "198.51.100.1" )}, | |
[typeof(IdentifierCode)] = () => new OpenApiSchema{ | |
Type = "array", | |
Items = new OpenApiSchema | |
{ | |
Type = "string", | |
Format = "byte" | |
}}, | |
[typeof(DateTimeOffsetInterval)] = () => new OpenApiSchema { Type = "string", Example = new OpenApiString("2023-06-23T00:00:00/2023-06-30T00:00:00")}, | |
// Nullable types | |
[typeof(bool?)] = () => new OpenApiSchema { Type = "boolean", Nullable = true }, | |
[typeof(byte?)] = () => new OpenApiSchema { Type = "string", Format = "byte", Nullable = true }, | |
[typeof(int?)] = () => new OpenApiSchema { Type = "integer", Format = "int32", Nullable = true }, | |
[typeof(uint?)] = () => new OpenApiSchema { Type = "integer", Format = "int32", Nullable = true }, | |
[typeof(long?)] = () => new OpenApiSchema { Type = "integer", Format = "int64", Nullable = true }, | |
[typeof(ulong?)] = () => new OpenApiSchema { Type = "integer", Format = "int64", Nullable = true }, | |
[typeof(float?)] = () => new OpenApiSchema { Type = "number", Format = "float", Nullable = true }, | |
[typeof(double?)] = () => new OpenApiSchema { Type = "number", Format = "double", Nullable = true }, | |
[typeof(decimal?)] = () => new OpenApiSchema { Type = "number", Format = "double", Nullable = true }, | |
[typeof(DateTime?)] = () => new OpenApiSchema { Type = "string", Format = "date-time", Nullable = true }, | |
[typeof(DateTimeOffset?)] = () => new OpenApiSchema { Type = "string", Format = "date-time", Nullable = true }, | |
[typeof(LocalDateTime?)] = () => new OpenApiSchema{Type = "string", Format = "date-time", Nullable = true}, | |
[typeof(Guid?)] = () => new OpenApiSchema { Type = "string", Format = "uuid", Nullable = true }, | |
[typeof(char?)] = () => new OpenApiSchema { Type = "string", Nullable = true }, | |
[typeof(TimeSpan?)] = () => new OpenApiSchema {Type = "string", Example = new OpenApiString( "00:00:00" ), Nullable = true}, | |
}; | |
static public OpenApiSchema MapTypeToOpenApiType( | |
this Type type, | |
OpenApiSchema openApiSchema) | |
{ | |
if (_typeToOpenApiSchema.TryGetValue(type, out var result)) | |
return result(); | |
openApiSchema.Type = "object"; | |
return openApiSchema; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment