diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2019-12-13 16:38:59 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-04-15 15:57:41 +0200 |
commit | bdec52548ca739e5533792c3bf656b8e2cf9fcb6 (patch) | |
tree | bbec422c569f18ba9f4b898c2fe392c07360f0a2 | |
parent | 1f793c887773f9b7477a73467aa1e2497ffef25c (diff) | |
download | qtwebchannel-bdec52548ca739e5533792c3bf656b8e2cf9fcb6.tar.gz |
Restore compatibility with custom QVariantList/QVariantMap converters
QVariant::toValue<QVariantList> does not honor explicit custom converters
for a sequentially iterable container to a QVariantList. This breaks
compatibility with Qt 5.12.3 and before, where the old code path using
QVariant::toList() used the custom converter. Do the same for
QVariantMap.
This patch restores the compatibility with old code. The real fix will
target the QVariantList converter code in QtCore - this is just a hotfix
within Qt WebChannel for now.
Task-number: QTBUG-80751
Change-Id: Ic70c6a353aad43ddbaefbc6626a0af87bd0d024f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.cpp | 13 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.cpp | 25 | ||||
-rw-r--r-- | tests/auto/webchannel/tst_webchannel.h | 13 |
3 files changed, 49 insertions, 2 deletions
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index e23921d..ea7ddc9 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -788,10 +788,19 @@ QJsonValue QMetaObjectPublisher::wrapResult(const QVariant &result, QWebChannelA // *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<QVariantList>(), transport); + // additionally, when there's a direct converter to QVariantList, use that one via convert + // but recover when conversion fails and fall back to the .value<QVariantList> conversion + // see also: https://bugreports.qt.io/browse/QTBUG-80751 + auto list = result; + if (!list.convert(qMetaTypeId<QVariantList>())) + list = result; + return wrapList(list.value<QVariantList>(), transport); } else if (result.canConvert<QVariantMap>()) { // recurse and potentially wrap contents of the map - return wrapMap(result.toMap(), transport); + auto map = result; + if (!map.convert(qMetaTypeId<QVariantMap>())) + map = result; + return wrapMap(map.value<QVariantMap>(), transport); } return QJsonValue::fromVariant(result); diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index c47cd6b..f9719cf 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -183,6 +183,20 @@ void TestJSEngine::initWebChannelJS() #endif // WEBCHANNEL_TESTS_CAN_USE_JS_ENGINE +namespace { +QVariantList convert_to_js(const TestStructVector &list) +{ + QVariantList ret; + ret.reserve(list.size()); + std::transform(list.begin(), list.end(), std::back_inserter(ret), [](const TestStruct &value) -> QVariant { + QVariantMap map; + map["foo"] = value.foo; + map["bar"] = value.bar; + return map; + }); + return ret; +} +} TestWebChannel::TestWebChannel(QObject *parent) : QObject(parent) @@ -191,6 +205,9 @@ TestWebChannel::TestWebChannel(QObject *parent) , m_lastBool(false) , m_lastDouble(0) { + qRegisterMetaType<TestStruct>(); + qRegisterMetaType<TestStructVector>(); + QMetaType::registerConverter<TestStructVector, QVariantList>(convert_to_js); } TestWebChannel::~TestWebChannel() @@ -864,6 +881,14 @@ void TestWebChannel::testWrapValues() QVERIFY(value.isArray()); QCOMPARE(value.toArray(), QJsonArray({1, 2, 3})); } + { + TestStructVector vec{{1, 2}, {3, 4}}; + QVariant variant = QVariant::fromValue(vec); + QJsonValue value = channel.d_func()->publisher->wrapResult(variant, m_dummyTransport); + QVERIFY(value.isArray()); + QCOMPARE(value.toArray(), QJsonArray({QJsonObject{{"foo", 1}, {"bar", 2}}, + QJsonObject{{"foo", 3}, {"bar", 4}}})); + } } void TestWebChannel::testWrapObjectWithMultipleTransports() diff --git a/tests/auto/webchannel/tst_webchannel.h b/tests/auto/webchannel/tst_webchannel.h index 7cfce06..eae21f4 100644 --- a/tests/auto/webchannel/tst_webchannel.h +++ b/tests/auto/webchannel/tst_webchannel.h @@ -39,6 +39,19 @@ #include <QtWebChannel/QWebChannelAbstractTransport> +struct TestStruct +{ + TestStruct(int foo = 0, int bar = 0) + : foo(foo) + , bar(bar) + {} + int foo; + int bar; +}; +Q_DECLARE_METATYPE(TestStruct) +using TestStructVector = std::vector<TestStruct>; +Q_DECLARE_METATYPE(TestStructVector) + QT_BEGIN_NAMESPACE class DummyTransport : public QWebChannelAbstractTransport |