Skip to content

Commit 01563e6

Browse files
authoredMay 18, 2020
[Expression] Do not transform NULL integer as 0 when concat
Fixes #36112 : don't concat null values
1 parent dc8b8e9 commit 01563e6

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,8 @@ static QVariant fcnConcat( const QVariantList &values, const QgsExpressionContex
19231923
QString concat;
19241924
for ( const QVariant &value : values )
19251925
{
1926-
concat += QgsExpressionUtils::getStringValue( value, parent );
1926+
if ( !value.isNull() )
1927+
concat += QgsExpressionUtils::getStringValue( value, parent );
19271928
}
19281929
return concat;
19291930
}

‎tests/src/core/testqgsexpression.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,6 +3891,31 @@ class TestQgsExpression: public QObject
38913891
QCOMPARE( QgsExpression::replaceExpressionText( input, &context ), expected );
38923892
}
38933893

3894+
void testConcatNULLAttributeValue()
3895+
{
3896+
// Test that null integer values coming from provider are not transformed as 0
3897+
// https://github.com/qgis/QGIS/issues/36112
3898+
3899+
QgsFields fields;
3900+
fields.append( QgsField( QStringLiteral( "foo" ), QVariant::Int ) );
3901+
3902+
QgsFeature f;
3903+
f.initAttributes( 1 );
3904+
f.setAttribute( 0, QVariant( QVariant::Int ) );
3905+
3906+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
3907+
QgsExpression exp( QStringLiteral( "concat('test', foo)" ) );
3908+
QVariant res = exp.evaluate( &context );
3909+
QCOMPARE( res.type(), QVariant::String );
3910+
QCOMPARE( res.toString(), QStringLiteral( "test" ) );
3911+
3912+
f.setAttribute( 0, QVariant() );
3913+
context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
3914+
res = exp.evaluate( &context );
3915+
QCOMPARE( res.type(), QVariant::String );
3916+
QCOMPARE( res.toString(), QStringLiteral( "test" ) );
3917+
}
3918+
38943919
};
38953920

38963921
QGSTEST_MAIN( TestQgsExpression )

0 commit comments

Comments
 (0)
Please sign in to comment.