From 1466130070922a6cc55c2a5b724311780a394a13 Mon Sep 17 00:00:00 2001 From: Arno Rehn Date: Thu, 9 May 2019 23:26:13 +0200 Subject: Fix generic conversion of list-like values to JSON arrays Previously, QVariant::toList() was used to convert a QVariant to a QVariantList. This only works for actual QVariantLists and QStringList, however. This patch uses QVariant::value() which works in all cases. A better approach would be to extract a QSequentialIterable, but QTBUG-42016 currently prevents this. Change-Id: I732cc88a6db2ec5d990760364a9db98a52521f6b Reviewed-by: Milian Wolff --- src/webchannel/qmetaobjectpublisher.cpp | 5 ++++- tests/auto/webchannel/tst_webchannel.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index 3b2c016..917bbf7 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -621,7 +621,10 @@ QJsonValue QMetaObjectPublisher::wrapResult(const QVariant &result, QWebChannelA #endif } else if (result.canConvert()) { // recurse and potentially wrap contents of the array - return wrapList(result.toList(), transport); + // *don't* use result.toList() as that *only* works for QVariantList and QStringList! + // Also, don't use QSequentialIterable (yet), since that seems to trigger QTBUG-42016 + // in certain cases. + return wrapList(result.value(), transport); } return QJsonValue::fromVariant(result); diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index 57aab53..27ef81c 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -797,6 +797,13 @@ void TestWebChannel::testWrapValues() QVERIFY(value.isDouble()); QCOMPARE(value.toInt(), (int) flags); } + { + QVector vec{1, 2, 3}; + QVariant variant = QVariant::fromValue(vec); + QJsonValue value = channel.d_func()->publisher->wrapResult(variant, m_dummyTransport); + QVERIFY(value.isArray()); + QCOMPARE(value.toArray(), QJsonArray({1, 2, 3})); + } } void TestWebChannel::testWrapObjectWithMultipleTransports() -- cgit v1.2.1