public static string GetPrimitiveValue(IOpenApiAny value) { IOpenApiPrimitive primitive = (IOpenApiPrimitive)value; switch (primitive.PrimitiveType) { case PrimitiveType.String: OpenApiString stringValue = (OpenApiString)primitive; return(stringValue.Value); case PrimitiveType.Boolean: OpenApiBoolean booleanValue = (OpenApiBoolean)primitive; return(booleanValue.Value.ToString()); case PrimitiveType.Integer: OpenApiInteger integerValue = (OpenApiInteger)primitive; return(integerValue.Value.ToString()); case PrimitiveType.Long: OpenApiLong longValue = (OpenApiLong)primitive; return(longValue.Value.ToString()); case PrimitiveType.Float: OpenApiFloat floatValue = (OpenApiFloat)primitive; return(floatValue.Value.ToString(CultureInfo.InvariantCulture)); case PrimitiveType.Double: OpenApiDouble doubleValue = (OpenApiDouble)primitive; return(doubleValue.Value.ToString(CultureInfo.InvariantCulture)); case PrimitiveType.Byte: OpenApiByte byteValue = (OpenApiByte)primitive; return(Encoding.Default.GetString(byteValue.Value)); case PrimitiveType.Binary: OpenApiBinary binaryValue = (OpenApiBinary)primitive; StringBuilder builder = new StringBuilder(); foreach (byte byteVal in binaryValue.Value) { builder.Append(Convert.ToString(byteVal, 2).PadLeft(8, '0')); } return(builder.ToString()); case PrimitiveType.Date: OpenApiDate dateValue = (OpenApiDate)primitive; return(dateValue.Value.ToString(CultureInfo.InvariantCulture)); case PrimitiveType.DateTime: OpenApiDateTime dateTimeValue = (OpenApiDateTime)primitive; return(dateTimeValue.Value.ToString(CultureInfo.InvariantCulture)); case PrimitiveType.Password: OpenApiPassword passwordValue = (OpenApiPassword)primitive; return(passwordValue.Value); default: throw new NotImplementedException("This data example type is not supported yet!"); } }
public static bool TryCreateFor(OpenApiSchema schema, object value, out IOpenApiAny openApiAny) { openApiAny = null; if (schema.Type == "boolean" && TryCast(value, out bool boolValue)) { openApiAny = new OpenApiBoolean(boolValue); } else if (schema.Type == "integer" && schema.Format == "int16" && TryCast(value, out short shortValue)) { openApiAny = new OpenApiInteger(shortValue); // preliminary unboxing is required; simply casting to int won't suffice } else if (schema.Type == "integer" && schema.Format == "int32" && TryCast(value, out int intValue)) { openApiAny = new OpenApiInteger(intValue); } else if (schema.Type == "integer" && schema.Format == "int64" && TryCast(value, out long longValue)) { openApiAny = new OpenApiLong(longValue); } else if (schema.Type == "number" && schema.Format == "float" && TryCast(value, out float floatValue)) { openApiAny = new OpenApiFloat(floatValue); } else if (schema.Type == "number" && schema.Format == "double" && TryCast(value, out double doubleValue)) { openApiAny = new OpenApiDouble(doubleValue); } else if (schema.Type == "string" && value.GetType().IsEnum) { openApiAny = new OpenApiString(Enum.GetName(value.GetType(), value)); } else if (schema.Type == "string" && schema.Format == "date-time" && TryCast(value, out DateTime dateTimeValue)) { openApiAny = new OpenApiDate(dateTimeValue); } else if (schema.Type == "string") { openApiAny = new OpenApiString(value.ToString()); } return(openApiAny != null); }
private static IOpenApiPrimitive GetStructValue(Type type, object value) { var openValue = default(IOpenApiPrimitive); if (type == typeof(DateTime?) && ((DateTime?)value).HasValue) { openValue = new OpenApiDate(((DateTime?)value).Value.ToUniversalTime()); } else if (type == typeof(DateTime) && ((DateTime)value) != default(DateTime)) { openValue = new OpenApiDate(((DateTime)value).ToUniversalTime()); } else if (type == typeof(string)) { openValue = new OpenApiString((string)value); } else if (type == typeof(int) || type == typeof(int?)) { openValue = new OpenApiInteger((int)value); } else if (type == typeof(short) || type == typeof(short?)) { openValue = new OpenApiInteger((short)value); } else if (type == typeof(long) || type == typeof(long?)) { openValue = new OpenApiLong((long)value); } else if (type == typeof(float) || type == typeof(float?)) { openValue = new OpenApiFloat((float)value); } else if (type == typeof(decimal) || type == typeof(decimal?)) { openValue = new OpenApiDouble((double)(decimal)value); } else if (type == typeof(double) || type == typeof(double?)) { openValue = new OpenApiDouble((double)value); } else if (type == typeof(bool) || type == typeof(bool?)) { openValue = new OpenApiBoolean((bool)value); } else if (type == typeof(Guid) || type == typeof(Guid?)) { openValue = new OpenApiString($"{value}"); } else if (type == typeof(byte) || type == typeof(byte?)) { openValue = new OpenApiByte((byte)value); } else if ( #if NETSTANDARD14 type.GetTypeInfo().IsEnum || Nullable.GetUnderlyingType(type)?.GetTypeInfo().IsEnum == true) { #else type.IsEnum || Nullable.GetUnderlyingType(type)?.IsEnum == true) { #endif openValue = new OpenApiString($"{value}"); } else if (type.IsValueType && !type.IsPrimitive && !type.Namespace.StartsWith("System") && !type.IsEnum) { openValue = new OpenApiString($"{value}"); } return(openValue); }