Skip to content

Commit

Permalink
fix(amplify-velocity-template): expression not equals works unproperly (
Browse files Browse the repository at this point in the history
#5571)

When used with amplify-appsync-simulator the not equals (!=) expression doesn't work properly. Two
strings are considered inequal when should be considered equals.

fix #5570

Co-authored-by: Fernando Chucre <fernando@oraoncology.com>
  • Loading branch information
chucre and Fernando Chucre authored Oct 14, 2020
1 parent 35af053 commit 7bc5051
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { GraphQLResolveInfo } from 'graphql';
import { AmplifyAppSyncSimulator } from '../..';
import { AmplifyAppSyncSimulatorAuthenticationType } from '../../type-definition';
import { VelocityTemplate } from '../../velocity';
import { mockInfo } from './util/general-utils.test';

describe('VelocityTemplate', () => {
let content;
let simulator;
beforeAll(() => {
content = `"$ctx.error.message, $ctx.error.type"`;
simulator = new AmplifyAppSyncSimulator();
});

describe('#render', () => {
it('should handle string comparison', () => {
const template = new VelocityTemplate(
{
path: 'INLINE_TEMPLATE',
content: `
#if( $ctx.stash.get("current_user_id")!=$ctx.prev.result.current_user_id)
"not the same value"
#else
"the same value"
#end`,
},
simulator,
);
const info = mockInfo;
const result = template.render(
{
arguments: {},
source: {},
stash: {
current_user_id: 'someString',
},
prevResult: {
current_user_id: 'someString',
},
},
{
headers: { Key: 'value' },
requestAuthorizationMode: AmplifyAppSyncSimulatorAuthenticationType.API_KEY,
},
info,
);
expect(result.errors).toEqual([]);
expect(result.result).toEqual('the same value');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const stubInfo = {
},
},
} as unknown;
const mockInfo = stubInfo as GraphQLResolveInfo;
export const mockInfo = stubInfo as GraphQLResolveInfo;
const stubJavaMap: JavaMap = new JavaMap({ field1: 'field1Value', field2: 'field2Value', field3: 'field3Value' }, x => x);
var util;

Expand Down
16 changes: 10 additions & 6 deletions packages/amplify-velocity-template/src/compile/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ module.exports = function(Velocity, utils) {
* 基本数据类型,使用 getLiteral求值,getLiteral遇到是引用的时候,使用
* getReferences求值
*/
compareEqualExpressions: function(exp0, exp1) {
const val0 = this.getExpression(exp0);
const val1 = this.getExpression(exp1);
const val0Json = val0 && val0.toJSON ? val0.toJSON() : val0;
const val1Json = val1 && val1.toJSON ? val1.toJSON() : val1;
return _.isEqual(val0Json, val1Json);
},

getExpression: function(ast) {
var exp = ast.expression;
var ret;
Expand Down Expand Up @@ -51,11 +59,7 @@ module.exports = function(Velocity, utils) {
break;

case '==':
const val0 = this.getExpression(exp[0]);
const val1 = this.getExpression(exp[1]);
const val0Json = val0 && val0.toJSON ? val0.toJSON() : val0;
const val1Json = val1 && val1.toJSON ? val1.toJSON() : val1;
ret = _.isEqual(val0Json, val1Json);
ret = this.compareEqualExpressions(exp[0], exp[1]);
break;

case '>=':
Expand All @@ -67,7 +71,7 @@ module.exports = function(Velocity, utils) {
break;

case '!=':
ret = this.getExpression(exp[0]) != this.getExpression(exp[1]);
ret = !this.compareEqualExpressions(exp[0], exp[1]);
break;

case 'minus':
Expand Down

0 comments on commit 7bc5051

Please sign in to comment.