Ejemplo n.º 1
0
        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!");
            }
        }
        private bool TryParse(JsonElement token, out IOpenApiAny?any)
        {
            any = null;

            switch (token.ValueKind)
            {
            case JsonValueKind.Array:
                var array = new OpenApiArray();

                foreach (var value in token.EnumerateArray())
                {
                    if (TryParse(value, out var child))
                    {
                        array.Add(child);
                    }
                }

                any = array;
                return(true);

            case JsonValueKind.False:
                any = new OpenApiBoolean(false);
                return(true);

            case JsonValueKind.True:
                any = new OpenApiBoolean(true);
                return(true);

            case JsonValueKind.Number:
                any = new OpenApiDouble(token.GetDouble());
                return(true);

            case JsonValueKind.String:
                any = new OpenApiString(token.GetString());
                return(true);

            case JsonValueKind.Object:
                var obj = new OpenApiObject();

                foreach (var child in token.EnumerateObject())
                {
                    if (TryParse(child.Value, out var value))
                    {
                        obj[child.Name] = value;
                    }
                }

                any = obj;
                return(true);

            case JsonValueKind.Null:
            case JsonValueKind.Undefined:
            default:
                return(false);
            }
        }
Ejemplo n.º 3
0
        public void WriteOpenApiDoubleAsJsonWorks(double input, bool produceTerseOutput)
        {
            // Arrange
            var doubleValue = new OpenApiDouble(input);

            var json = WriteAsJson(doubleValue, produceTerseOutput);

            // Assert
            json.Should().Be(input.ToString());
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new instance of <see cref="IOpenApiAny"/> based on the OpenAPI document format.
        /// </summary>
        /// <param name="instance">instance.</param>
        /// <param name="settings"><see cref="JsonSerializerSettings"/>settings.</param>
        /// <returns><see cref="IOpenApiAny"/> instance.</returns>
        public static IOpenApiAny CreateInstance <T>(T instance, JsonSerializerSettings settings)
        {
            Type type  = typeof(T);
            var  @enum = Type.GetTypeCode(type);
            var  openApiExampleValue = default(IOpenApiAny);

            switch (@enum)
            {
            case TypeCode.Int16:
                openApiExampleValue = new OpenApiInteger(Convert.ToInt16(instance));
                break;

            case TypeCode.Int32:
                openApiExampleValue = new OpenApiInteger(Convert.ToInt32(instance));
                break;

            case TypeCode.Int64:
                openApiExampleValue = new OpenApiLong(Convert.ToInt64(instance));
                break;

            case TypeCode.UInt16:
                openApiExampleValue = new OpenApiDouble(Convert.ToUInt16(instance));
                break;

            case TypeCode.UInt32:
                openApiExampleValue = new OpenApiDouble(Convert.ToUInt32(instance));
                break;

            case TypeCode.UInt64:
                openApiExampleValue = new OpenApiDouble(Convert.ToUInt64(instance));
                break;

            case TypeCode.Single:
                openApiExampleValue = new OpenApiFloat(Convert.ToSingle(instance));
                break;

            case TypeCode.Double:
                openApiExampleValue = new OpenApiDouble(Convert.ToDouble(instance));
                break;

            case TypeCode.Boolean:
                openApiExampleValue = new OpenApiBoolean(Convert.ToBoolean(instance));
                break;

            case TypeCode.String:
                openApiExampleValue = new OpenApiString(Convert.ToString(instance));
                break;

            case TypeCode.DateTime:
                openApiExampleValue = new OpenApiDateTime(Convert.ToDateTime(instance));
                break;

            case TypeCode.Object when type == typeof(DateTimeOffset):
                openApiExampleValue = new OpenApiDateTime((DateTimeOffset)(Convert.ChangeType(instance, type)));
                break;

            case TypeCode.Object when type == typeof(Guid):
                openApiExampleValue = new OpenApiString(Convert.ToString(instance));
                break;

            case TypeCode.Object when type == typeof(byte[]):
                openApiExampleValue = new OpenApiString(Convert.ToBase64String((byte[])Convert.ChangeType(instance, type)));
                break;

            case TypeCode.Object:
                openApiExampleValue = new OpenApiString(JsonConvert.SerializeObject(instance, settings));
                break;

            default:
                throw new InvalidOperationException("Invalid OpenAPI data Format");
            }

            return(openApiExampleValue);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 public QueryStringParameterAttribute(string name, string description, double example)
 {
     Initialise(name, description);
     Example = new OpenApiDouble(example);
 }