Last active
November 23, 2025 17:53
-
-
Save adamijak/0ab78a4334f4a8c40b1f971079f3120a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| namespace _; | |
| public class Parameter<T> //: IParameter - unnecesary | |
| { | |
| private string? stringValue; | |
| private T value; | |
| public bool IsValid { get; private set; } | |
| public static bool TryParse(string value, out Parameter<T> result) | |
| { | |
| return TryParse(value, null!, out result); | |
| } | |
| public static bool TryParse(string value, IFormatProvider provider, out Parameter<T> result) | |
| { | |
| result = new Parameter<T> | |
| { | |
| stringValue = value | |
| }; | |
| var success = TryParseParameter(value, provider, out var resultValue); | |
| if (!success) | |
| { | |
| return true; | |
| } | |
| result.value = resultValue; | |
| result.IsValid = true; | |
| return true; | |
| } | |
| private static bool TryParseParameter(string value, IFormatProvider provider, out T result) | |
| { | |
| var success = false; | |
| result = default; | |
| if (typeof(T) == typeof(bool)) | |
| { | |
| success = bool.TryParse(value, out var parsedValue); | |
| result = (T)(object)parsedValue; | |
| return success; | |
| } | |
| if (typeof(T).IsEnum) | |
| { | |
| success = Enum.TryParse(typeof(T), value, true, out var parsedValue); | |
| success = success && Enum.IsDefined(typeof(T), parsedValue!); | |
| if (success) | |
| { | |
| result = (T)parsedValue; | |
| } | |
| return success; | |
| } | |
| if (typeof(T) == typeof(DateTime)) | |
| { | |
| success = DateTime.TryParse(value, provider, out var parsedValue); | |
| result = (T)(object)parsedValue; | |
| return success; | |
| } | |
| return success; | |
| } | |
| public static implicit operator T(Parameter<T> p) | |
| { | |
| return p.value; | |
| } | |
| public override string? ToString() | |
| { | |
| return stringValue; | |
| } | |
| } |
This file contains hidden or 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
| namespace _; | |
| public class ParameterSchemaTransformer : IOpenApiSchemaTransformer | |
| { | |
| public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, | |
| CancellationToken cancellationToken) | |
| { | |
| var type = context.JsonTypeInfo.Type; | |
| if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Parameter<>)) | |
| { | |
| return Task.CompletedTask; | |
| } | |
| var parameterType = type.GetGenericArguments().FirstOrDefault(); | |
| if (parameterType != null && parameterType.IsEnum) | |
| { | |
| schema.Type = "string"; | |
| schema.Enum = Enum.GetNames(parameterType).Select(IOpenApiAny (i) => new OpenApiString(i)).ToList(); | |
| } | |
| if (parameterType == typeof(bool)) | |
| { | |
| schema.Type = "boolean"; | |
| } | |
| if (parameterType == typeof(DateTime)) | |
| { | |
| schema.Type = "string"; | |
| schema.Format = "date-time"; | |
| } | |
| return Task.CompletedTask; | |
| } | |
| } |
Author
Where does the IParameter interface come from?
From former implementation - shouldn't be necessary
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where does the IParameter interface come from?