Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 2d78bb6

Browse files
authored
throw exception when entity name contains invalid char (#837)
1 parent 7ded18e commit 2d78bb6

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

packages/lu/src/parser/lufile/entitySection.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const EntitySectionContext = require('./generated/LUFileParser').LUFileParser.En
22
const DiagnosticSeverity = require('./diagnostic').DiagnosticSeverity;
33
const BuildDiagnostic = require('./diagnostic').BuildDiagnostic;
44
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
5+
const InvalidCharsInIntentOrEntityName = require('./../utils/enums/invalidchars').InvalidCharsInIntentOrEntityName;
56

67
class EntitySection {
78
/**
@@ -19,13 +20,23 @@ class EntitySection {
1920
}
2021

2122
ExtractName(parseTree) {
23+
let entityName;
2224
if (parseTree.entityDefinition().entityLine().entityName()) {
23-
return parseTree.entityDefinition().entityLine().entityName().getText().trim();
25+
entityName = parseTree.entityDefinition().entityLine().entityName().getText().trim();
2426
} else {
2527
this.Errors.push(BuildDiagnostic({
2628
message: "Invalid entity line, did you miss entity name after $",
2729
context: parseTree.entityDefinition().entityLine()
28-
}))
30+
}));
31+
}
32+
33+
if (entityName && InvalidCharsInIntentOrEntityName.some(x => entityName.includes(x))) {
34+
this.Errors.push(BuildDiagnostic({
35+
message: `Invalid entity line, entity name ${entityName} cannot contain any of the following characters: [<, >, *, %, &, :, \\, $]`,
36+
context: parseTree.newEntityDefinition().newEntityLine()
37+
}));
38+
} else {
39+
return entityName;
2940
}
3041
}
3142

packages/lu/src/parser/lufile/newEntitySection.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const NewEntitySectionContext = require('./generated/LUFileParser').LUFileParser
22
const DiagnosticSeverity = require('./diagnostic').DiagnosticSeverity;
33
const BuildDiagnostic = require('./diagnostic').BuildDiagnostic;
44
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
5+
const InvalidCharsInIntentOrEntityName = require('./../utils/enums/invalidchars').InvalidCharsInIntentOrEntityName;
56

67
class NewEntitySection {
78
/**
@@ -23,16 +24,26 @@ class NewEntitySection {
2324
}
2425

2526
ExtractName(parseTree) {
27+
let entityName
2628
if (parseTree.newEntityDefinition().newEntityLine().newEntityName()) {
27-
return parseTree.newEntityDefinition().newEntityLine().newEntityName().getText().trim();
29+
entityName = parseTree.newEntityDefinition().newEntityLine().newEntityName().getText().trim();
2830
} else if (parseTree.newEntityDefinition().newEntityLine().newEntityNameWithWS()) {
29-
return parseTree.newEntityDefinition().newEntityLine().newEntityNameWithWS().getText().trim();
31+
entityName = parseTree.newEntityDefinition().newEntityLine().newEntityNameWithWS().getText().trim();
3032
} else {
3133
this.Errors.push(BuildDiagnostic({
3234
message: "Invalid entity line, did you miss entity name after @",
3335
context: parseTree.newEntityDefinition().newEntityLine()
3436
}))
3537
}
38+
39+
if (entityName && InvalidCharsInIntentOrEntityName.some(x => entityName.includes(x))) {
40+
this.Errors.push(BuildDiagnostic({
41+
message: `Invalid entity line, entity name ${entityName} cannot contain any of the following characters: [<, >, *, %, &, :, \\, $]`,
42+
context: parseTree.newEntityDefinition().newEntityLine()
43+
}));
44+
} else {
45+
return entityName;
46+
}
3647
}
3748

3849
ExtractType(parseTree) {

packages/lu/src/parser/lufile/visitor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const lp = require('./generated/LUFileParser').LUFileParser;
22
const LUISObjNameEnum = require('./../utils/enums/luisobjenum');
3+
const InvalidCharsInIntentOrEntityName = require('./../utils/enums/invalidchars').InvalidCharsInIntentOrEntityName;
34

45
class Visitor {
56
/**
@@ -38,6 +39,12 @@ class Visitor {
3839
static recurselyResolveTokenizedUtterance(tokUtt, entities, errorMsgs, srcUtterance) {
3940
for (const item of tokUtt) {
4041
if (item === Object(item)) {
42+
let entityName = item.entityName.trim()
43+
if (entityName && InvalidCharsInIntentOrEntityName.some(x => entityName.includes(x))) {
44+
errorMsgs.push(`Invalid utterance line, entity name ${entityName} cannot contain any of the following characters: [<, >, *, %, &, :, \\, $]`);
45+
continue;
46+
}
47+
4148
if (item.entityValue === undefined) {
4249
// we have a pattern.any entity
4350
const patternStr = item.role ? `{${item.entityName}:${item.role}}` : `{${item.entityName}}`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
// Invalid chars in intent or entity name
6+
module.exports = {
7+
InvalidCharsInIntentOrEntityName: ['<', '>', '*', '%', '&', ':', '\\', '$']
8+
};

packages/lu/test/commands/luis/convert.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ describe('luis:convert negative tests', () => {
233233
})
234234

235235
})
236+
237+
it('luis:convert should show ERR message when entity name contains invalid char', (done) => {
238+
loadLuFile('./../../fixtures/testcases/bad5.lu')
239+
.then(res => {
240+
LuisBuilder.fromLUAsync(res)
241+
.then(res => done(res))
242+
.catch(err => {
243+
assert.isTrue(err.text.includes('[ERROR] line 2:0 - line 2:26: Invalid utterance line, entity name @addto*Property cannot contain any of the following characters: [<, >, *, %, &, :, \\, $]'))
244+
assert.isTrue(err.text.includes('[ERROR] line 4:0 - line 4:20: Invalid entity line, entity name delete$Property cannot contain any of the following characters: [<, >, *, %, &, :, \\, $]'))
245+
done()
246+
})
247+
})
248+
})
236249
})
237250

238251
describe('luis:convert new entity format', () => {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# greeting
2+
- hi {@addto*Property=foo}
3+
4+
@ ml delete$Property

0 commit comments

Comments
 (0)