Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amplify-velocity-template): expression not equals works improperly #5571

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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