Navigation Menu

Skip to content

Commit

Permalink
[Expression] Do not transform NULL integer as 0 when concat
Browse files Browse the repository at this point in the history
Fixes #36112 : don't concat null values
  • Loading branch information
troopa81 committed May 18, 2020
1 parent dc8b8e9 commit 01563e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -1923,7 +1923,8 @@ static QVariant fcnConcat( const QVariantList &values, const QgsExpressionContex
QString concat;
for ( const QVariant &value : values )
{
concat += QgsExpressionUtils::getStringValue( value, parent );
if ( !value.isNull() )
concat += QgsExpressionUtils::getStringValue( value, parent );
}
return concat;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -3891,6 +3891,31 @@ class TestQgsExpression: public QObject
QCOMPARE( QgsExpression::replaceExpressionText( input, &context ), expected );
}

void testConcatNULLAttributeValue()
{
// Test that null integer values coming from provider are not transformed as 0
// https://github.com/qgis/QGIS/issues/36112

QgsFields fields;
fields.append( QgsField( QStringLiteral( "foo" ), QVariant::Int ) );

QgsFeature f;
f.initAttributes( 1 );
f.setAttribute( 0, QVariant( QVariant::Int ) );

QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
QgsExpression exp( QStringLiteral( "concat('test', foo)" ) );
QVariant res = exp.evaluate( &context );
QCOMPARE( res.type(), QVariant::String );
QCOMPARE( res.toString(), QStringLiteral( "test" ) );

f.setAttribute( 0, QVariant() );
context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
res = exp.evaluate( &context );
QCOMPARE( res.type(), QVariant::String );
QCOMPARE( res.toString(), QStringLiteral( "test" ) );
}

};

QGSTEST_MAIN( TestQgsExpression )
Expand Down

0 comments on commit 01563e6

Please sign in to comment.