-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON columns with Date and/or Bool fail decoding #163
Comments
cc @gwynne |
For the first issue, replace For the let query = """
SELECT JSON_OBJECT(
'isEnabled', true,
'due', UNIX_TIMESTAMP() - 978307200.0
) options
""" as SQLQueryString
(Note: I moved the The arguably "correct" way is to configure the underlying JSON coders in use by MySQLKIt appropriately, which requires doing this when you configure the database connection, assuming you're using Fluent to actually connect to the database: let iso8601JsonDecoder = JSONDecoder(), iso8601JsonEncoder = JSONEncoder()
iso8601JsonDecoder.dateDecodingStrategy = .iso8601
iso8601JsonEncoder.dateEncodingStrategy = .iso8601
app.databases.use(.mysql(
/* url: or hostname:port:username: etc. here */,
encoder: .init(json: iso8601JsonEncoder),
decoder: .init(json: iso8601JsonDecoder)
)) If you're connecting directly via SQLKit/MySQLKIt, you can pass the same (P.S.: As a matter of completionism, I would be remiss not to mention that your query can be more formally written this way: try await db.select()
.column(SQLFunction("json_object", args:
SQLLiteral.string("isEnabled"), SQLLiteral.boolean(true),
SQLLiteral.string("due"), SQLFunction("now")
), as: "options")
.first(decoding: MyContent.self) I'm the first to agree this is rather clumsy and awkward compared to the raw query syntax. I will be doing something about that when I get a chance 😅) |
Problem Description
When trying to decode a query that has a JSON column with booleans and/or date values, it fails with an error. Consider the following minimal example:
Running this code will produce:
if we change the
Bool
toInt
, we now get an error because of theDate
value:The second error can be omitted by changing the type in the
due
property, fromDate
toString
Expected Behavior
Not sure if this is a bug report, or a feature request. But ideally, Fluent should be able to decode
Bool
andDate
types. Or maybe I'm missing something obvious of why this is not the case?Workaround
In the meantime, I'm implementing
Econdable.init(from decoder: Decoder)
to workaround the problem:This workaround now produces proper JSON content:
Environment
mysql server: 8.0.27
framework: 4.67.4
toolbox: 18.6.0
The text was updated successfully, but these errors were encountered: