/// <summary>
        /// Matches the type, format and item (if array) to the schema specified on the parameter.
        /// </summary>
        /// <param name="type">The type defined for the parameter, check: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#dataTypeFormat for guidelines</param>
        /// <param name="isArray">a boolean that determines the location of the properties on the structure.</param>
        /// <returns></returns>
        private static SchemaRef GetSchemaByType(string type, bool isArray)
        {
            SchemaRef schema;
            string    format = null;

            switch (type.ToLowerInvariant()) //formats as defined by OAS:
            {
            case "string":
                type   = "string";
                format = null;
                break;

            case "int":
            case "integer":
                type   = "integer";
                format = "int32";
                break;

            case "long":
                type   = "integer";
                format = "int64";
                break;

            case "float":
                type   = "number";
                format = "float";
                break;

            case "double":
                type   = "number";
                format = "double";
                break;

            case "byte":
                type   = "string";
                format = "byte";
                break;

            case "binary":
                type   = "string";
                format = "binary";
                break;

            case "bool":
            case "boolean":
                type   = "boolean";
                format = null;
                break;

            case "date":
                type   = "string";
                format = "date";
                break;

            case "datetime":
                type   = "string";
                format = "date-time";
                break;

            case "password":
                type   = "string";
                format = "password";
                break;
            }

            if (isArray)
            {
                schema = new SchemaRef
                {
                    Item = new Item {
                        Type = type, Format = format
                    },
                    Type = "array"
                };
            }
            else
            {
                schema = new SchemaRef
                {
                    Type   = type,
                    Format = format
                };
            }

            return(schema);
        }
Beispiel #2
0
        /// <summary>
        /// Matches the type, format and item (if array) to the schema specified on the parameter.
        /// </summary>
        /// <param name="type">The type defined for the parameter, check: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#dataTypeFormat for guidelines</param>
        /// <returns></returns>
        internal static SchemaRef GetSchemaByType(Type type)
        {
            bool isCollection = false;

            if (type.IsArray)
            {
                type         = type.GetElementType();
                isCollection = true;
            }

            if (typeof(IEnumerable).IsAssignableFrom(type) && (type != typeof(string)))
            {
                type         = type.GetGenericArguments()[0];
                isCollection = true;
            }

            SchemaRef schema;
            string    schemaType = null, format = null;

            switch (Type.GetTypeCode(type)) //formats as defined by OAS:
            {
            case TypeCode.String:
                schemaType = "string";
                format     = null;
                break;

            case TypeCode.Int16:
            case TypeCode.Int32:      //single, integer
                schemaType = "integer";
                format     = "int32";
                break;

            case TypeCode.Int64:
                schemaType = "integer";
                format     = "int64";
                break;

            case TypeCode.Decimal:
                schemaType = "number";
                format     = "float";
                break;

            case TypeCode.Double:
                schemaType = "number";
                format     = "double";
                break;

            case TypeCode.Byte:
                schemaType = "string";
                format     = "byte";
                break;

            case TypeCode.Boolean:
                schemaType = "boolean";
                format     = null;
                break;

            case TypeCode.DateTime:
                schemaType = "string";
                format     = "date-time";
                break;

            default:
                schemaType = "string";
                format     = null;
                break;
            }

            if (isCollection)
            {
                schema = new SchemaRef()
                {
                    Item = new Item()
                    {
                        Type = schemaType, Format = format
                    }, Type = "array"
                };
            }
            else
            {
                schema = new SchemaRef()
                {
                    Type = schemaType, Format = format
                };
            }

            return(schema);
        }