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

Commit f3767ca

Browse files
vishwacsenaVishwac Sena Kannanmunozemilio
authored
fix (#803)
Co-authored-by: Vishwac Sena Kannan <[email protected]> Co-authored-by: Emilio Munoz <[email protected]>
1 parent 432d1bd commit f3767ca

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

packages/lu/src/parser/utils/helpers.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ const updateToV7 = function(finalLUISJSON) {
215215
const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, entityParentTree)
216216
{
217217
if (finalLUISJSON.patterns === undefined || !Array.isArray(finalLUISJSON.patterns) || finalLUISJSON.patterns.length === 0) return;
218+
// update entityParentTree with all entity types.
219+
updateEntityParentTreeWithAllEntityTypes(finalLUISJSON, entityParentTree);
218220
(finalLUISJSON.patterns || []).forEach(pattern => {
219221
// detect if pattern has an entity definition
220222
let entitiesRegExp = /{(?<entity>[^{,}]+)}/gmi;
@@ -225,7 +227,9 @@ const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, ent
225227
entity = entity.replace(/[{}]/g, '');
226228
let entityInTree = entityParentTree[entity]
227229
if (entityInTree !== undefined) {
228-
if (entityInTree[0] != "$root$") {
230+
// at least one of these need to be a root entity.
231+
let isEntityAlsoRoot = entityInTree.find(item => item[0] === "$root$");
232+
if (isEntityAlsoRoot === undefined) {
229233
throw (new exception(retCode.errorCode.INVALID_INPUT, `Patterns cannot contain references to child entities. Pattern: "${pattern.pattern}" has reference to "{${entity}}".`));
230234
}
231235
}
@@ -234,6 +238,26 @@ const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, ent
234238
})
235239
}
236240

241+
const updateEntityParentTreeWithAllEntityTypes = function(finalLUISJSON, entityParentTree)
242+
{
243+
(finalLUISJSON.prebuiltEntities || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
244+
(finalLUISJSON.patternAnyEntities || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
245+
(finalLUISJSON.model_features || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
246+
(finalLUISJSON.phraselists || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
247+
(finalLUISJSON.regex_entities || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
248+
(finalLUISJSON.closedLists || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
249+
(finalLUISJSON.composites || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
250+
}
251+
252+
const addEntityToParentTree = function(entityParentTree, entityName)
253+
{
254+
if (entityParentTree[entityName] === undefined) {
255+
entityParentTree[entityName] = [["$root$"]];
256+
} else {
257+
entityParentTree[entityName].push(["$root$"]);
258+
}
259+
}
260+
237261
const constructEntityParentTree = function(entityCollection, entityParentTree, curPath)
238262
{
239263
entityCollection.forEach(entity => {

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -754,21 +754,6 @@ describe('V2 NDepth definitions using @ notation', function () {
754754
.catch(err => done())
755755
})
756756

757-
it('[V7 upgrade test] Patterns cannot have refernece to child entities', function(done) {
758-
let luFile = `
759-
@ ml userProfile =
760-
- @ personName firstName
761-
- @ personName lastName
762-
763-
@ prebuilt personName
764-
765-
# test
766-
- [my] name is vishwac
767-
- my first name is {@firstName} and last name is {@lastName}`;
768-
769-
parseFile.parseFile(luFile)
770-
.then(res => done(res))
771-
.catch(err => done())
772-
})
757+
773758

774759
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ const validateLUISBlob = require('./../../../src/parser/luis/luisValidator')
77
var chai = require('chai');
88
var assert = chai.assert;
99
describe('With helper functions', function () {
10+
it('Parsefile correctly handles non nDepth entity references in patterns', function(done) {
11+
let luFile = `@ list foo=
12+
@ ml operation=
13+
- @foo foo
14+
15+
# Test
16+
- Pattern {foo}`;
17+
parseFile.parseFile(luFile)
18+
.then(res => {
19+
assert.equal(res.LUISJsonStructure.patterns.length, 1)
20+
assert.equal(res.LUISJsonStructure.patterns[0].pattern, "Pattern {foo}");
21+
done()
22+
})
23+
})
24+
1025
it('parseFile includes defaults for LUIS app', function(done) {
1126
let luFile = `@ simple entity1`;
1227
parseFile.parseFile(luFile)

0 commit comments

Comments
 (0)