Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5900861

Browse files
nirvnnyalldawson
authored andcommittedJun 9, 2021
[expression] Allow use of (and return null to) index operator against null node/value
1 parent 752b414 commit 5900861

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
 

‎src/core/expression/qgsexpressionnodeimpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,8 @@ QVariant QgsExpressionNodeIndexOperator::evalNode( QgsExpression *parent, const
16691669
}
16701670

16711671
default:
1672-
parent->setEvalErrorString( tr( "[] can only be used with map or array values, not %1" ).arg( QMetaType::typeName( container.type() ) ) );
1672+
if ( !container.isNull() )
1673+
parent->setEvalErrorString( tr( "[] can only be used with map or array values, not %1" ).arg( QMetaType::typeName( container.type() ) ) );
16731674
return QVariant();
16741675
}
16751676
}

‎tests/src/core/testqgsexpression.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,9 @@ class TestQgsExpression: public QObject
39023902
e = QgsExpression( QStringLiteral( "array(1,2,3)[-4]" ) );
39033903
QVERIFY( !e.evaluate( &context ).isValid() );
39043904
QVERIFY( !e.hasEvalError() ); // no eval error - we are tolerant to this
3905+
e = QgsExpression( QStringLiteral( "@null_variable[0]" ) );
3906+
QVERIFY( !e.evaluate( &context ).isValid() );
3907+
QVERIFY( !e.hasEvalError() ); // no eval error - we are tolerant to this
39053908

39063909
// maps
39073910
e = QgsExpression( QStringLiteral( "map('a',1,'b',2,'c',3)[0]" ) );
@@ -3918,6 +3921,9 @@ class TestQgsExpression: public QObject
39183921
QCOMPARE( e.evaluate( &context ).toInt(), 3 );
39193922
e = QgsExpression( QStringLiteral( "map('a',1,'bbb',2,'c',3)['b'||'b'||'b']" ) );
39203923
QCOMPARE( e.evaluate( &context ).toInt(), 2 );
3924+
e = QgsExpression( QStringLiteral( "@null_variable['key']" ) );
3925+
QVERIFY( !e.evaluate( &context ).isValid() );
3926+
QVERIFY( !e.hasEvalError() ); // no eval error - we are tolerant to this
39213927
}
39223928

39233929

0 commit comments

Comments
 (0)
Please sign in to comment.