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

Commit 8078cc6

Browse files
Shuai Wangmunozemilio
andauthored
Add check for reserved prebuilt entities name (#1250)
* add check for reserved prebuilt names * fix failed tests * fix failed tests * retrigger * use set instead of list * fix set constructor error Co-authored-by: Emilio Munoz <[email protected]>
1 parent 0247e05 commit 8078cc6

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ class LUParser {
143143

144144
try {
145145
let newEntitySections = this.extractNewEntitiesSections(fileContent);
146+
const prebuilts = new Set(['age', 'datetimeV2', 'dimension', 'email', 'geographyV2', 'keyPhrase', 'money', 'number', 'ordinal', 'ordinalV2',
147+
'percentage', 'personName', 'phonenumber', 'temperature', 'url', 'datetime']);
148+
newEntitySections.forEach(section =>{
149+
if (prebuilts.has(section.Name) && section.Type && section.Type !== 'prebuilt') {
150+
section.Errors.push(BuildDiagnostic({
151+
message: `The model name ${section.Name} is reserved.`
152+
}))
153+
}
154+
});
155+
146156
newEntitySections.forEach(section => errors = errors.concat(section.Errors));
147157
sections = sections.concat(newEntitySections);
148158
} catch (err) {

packages/lu/test/parser/lufile/parseFileContents.featureToModels.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ describe('Model as feature definitions', function () {
9797

9898
it('list entity can be added as a feature to an intent', function(done) {
9999
let luFile = `
100-
@ intent abc usesFeature number
101-
@ list number
100+
@ intent abc usesFeature number2
101+
@ list number2
102102
# abc
103103
- test
104104
`;
@@ -108,18 +108,18 @@ describe('Model as feature definitions', function () {
108108
assert.equal(res.LUISJsonStructure.intents.length, 1);
109109
assert.equal(res.LUISJsonStructure.intents[0].name, 'abc');
110110
assert.equal(res.LUISJsonStructure.intents[0].features.length, 1);
111-
assert.equal(res.LUISJsonStructure.intents[0].features[0].modelName, 'number');
111+
assert.equal(res.LUISJsonStructure.intents[0].features[0].modelName, 'number2');
112112
assert.equal(res.LUISJsonStructure.closedLists.length, 1);
113-
assert.equal(res.LUISJsonStructure.closedLists[0].name, 'number');
113+
assert.equal(res.LUISJsonStructure.closedLists[0].name, 'number2');
114114
done();
115115
})
116116
.catch(err => done(err))
117117
});
118118

119119
it('Composite entity can be added as a feature to an intent', function(done) {
120120
let luFile = `
121-
@ intent abc usesFeature number
122-
@ composite number
121+
@ intent abc usesFeature number2
122+
@ composite number2
123123
# abc
124124
- test
125125
`;
@@ -129,18 +129,18 @@ describe('Model as feature definitions', function () {
129129
assert.equal(res.LUISJsonStructure.intents.length, 1);
130130
assert.equal(res.LUISJsonStructure.intents[0].name, 'abc');
131131
assert.equal(res.LUISJsonStructure.intents[0].features.length, 1);
132-
assert.equal(res.LUISJsonStructure.intents[0].features[0].modelName, 'number');
132+
assert.equal(res.LUISJsonStructure.intents[0].features[0].modelName, 'number2');
133133
assert.equal(res.LUISJsonStructure.composites.length, 1);
134-
assert.equal(res.LUISJsonStructure.composites[0].name, 'number');
134+
assert.equal(res.LUISJsonStructure.composites[0].name, 'number2');
135135
done();
136136
})
137137
.catch(err => done(err))
138138
});
139139

140140
it('Regex entity can be added as a feature to an intent', function(done) {
141141
let luFile = `
142-
@ intent abc usesFeature number
143-
@ regex number
142+
@ intent abc usesFeature number2
143+
@ regex number2
144144
# abc
145145
- test
146146
`;
@@ -150,9 +150,9 @@ describe('Model as feature definitions', function () {
150150
assert.equal(res.LUISJsonStructure.intents.length, 1);
151151
assert.equal(res.LUISJsonStructure.intents[0].name, 'abc');
152152
assert.equal(res.LUISJsonStructure.intents[0].features.length, 1);
153-
assert.equal(res.LUISJsonStructure.intents[0].features[0].modelName, 'number');
153+
assert.equal(res.LUISJsonStructure.intents[0].features[0].modelName, 'number2');
154154
assert.equal(res.LUISJsonStructure.regex_entities.length, 1);
155-
assert.equal(res.LUISJsonStructure.regex_entities[0].name, 'number');
155+
assert.equal(res.LUISJsonStructure.regex_entities[0].name, 'number2');
156156
done();
157157
})
158158
.catch(err => done(err))

packages/lu/test/parser/lufile/parseFileContents.modelAsFeature.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ describe('Model as feature definitions', function () {
301301
- portland
302302
- PDX
303303
304-
@ ml number hasRoles r1 usesFeature city
305-
@ number usesFeature city
304+
@ ml number2 hasRoles r1 usesFeature city
305+
@ number2 usesFeature city
306306
`;
307307

308308
parseFile.parseFile(luFile)
309309
.then(res => {
310310
assert.equal(res.LUISJsonStructure.entities.length, 1);
311-
assert.equal(res.LUISJsonStructure.entities[0].name, 'number');
311+
assert.equal(res.LUISJsonStructure.entities[0].name, 'number2');
312312
assert.equal(res.LUISJsonStructure.entities[0].features.length, 2);
313313
assert.equal(res.LUISJsonStructure.entities[0].features[0].featureName, 'city');
314314
assert.equal(res.LUISJsonStructure.entities[0].roles.length, 1);
@@ -341,14 +341,14 @@ describe('Model as feature definitions', function () {
341341
- portland
342342
- PDX
343343
344-
@ ml number hasRoles r1, r2 usesFeatures city, city2
345-
@ number usesFeature city
344+
@ ml number2 hasRoles r1, r2 usesFeatures city, city2
345+
@ number2 usesFeature city
346346
`;
347347

348348
parseFile.parseFile(luFile)
349349
.then(res => {
350350
assert.equal(res.LUISJsonStructure.entities.length, 1);
351-
assert.equal(res.LUISJsonStructure.entities[0].name, 'number');
351+
assert.equal(res.LUISJsonStructure.entities[0].name, 'number2');
352352
assert.equal(res.LUISJsonStructure.entities[0].features.length, 2);
353353
assert.equal(res.LUISJsonStructure.entities[0].features[0].featureName, 'city');
354354
assert.equal(res.LUISJsonStructure.entities[0].features[1].featureName, 'city2');

packages/lu/test/parser/lufile/sectionapi.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ const SectionOperator = require('./../../../src/parser/lufile/sectionOperator');
99
const LUSectionTypes = require('./../../../src/parser/utils/enums/lusectiontypes');
1010
const NEWLINE = require('os').EOL;
1111

12+
describe('luParser parse test', () => {
13+
let luresource = undefined;
14+
it('new Entity name should not use reserved keywords', () => {
15+
let fileContent =
16+
`@ ml age
17+
@ prebuilt number`;
18+
19+
luresource = luparser.parse(fileContent);
20+
assert.equal(luresource.Sections[0].Errors.length, 1);
21+
assert.equal(luresource.Sections[0].Errors[0].Message, 'The model name age is reserved.');
22+
})
23+
});
24+
1225
describe('Section CRUD tests for intent', () => {
1326
let luresource = undefined;
1427

0 commit comments

Comments
 (0)