Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
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
26 changes: 25 additions & 1 deletion packages/lu/src/parser/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ const updateToV7 = function(finalLUISJSON) {
const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, entityParentTree)
{
if (finalLUISJSON.patterns === undefined || !Array.isArray(finalLUISJSON.patterns) || finalLUISJSON.patterns.length === 0) return;
// update entityParentTree with all entity types.
updateEntityParentTreeWithAllEntityTypes(finalLUISJSON, entityParentTree);
(finalLUISJSON.patterns || []).forEach(pattern => {
// detect if pattern has an entity definition
let entitiesRegExp = /{(?<entity>[^{,}]+)}/gmi;
Expand All @@ -225,7 +227,9 @@ const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, ent
entity = entity.replace(/[{}]/g, '');
let entityInTree = entityParentTree[entity]
if (entityInTree !== undefined) {
if (entityInTree[0] != "$root$") {
// at least one of these need to be a root entity.
let isEntityAlsoRoot = entityInTree.find(item => item[0] === "$root$");
if (isEntityAlsoRoot === undefined) {
throw (new exception(retCode.errorCode.INVALID_INPUT, `Patterns cannot contain references to child entities. Pattern: "${pattern.pattern}" has reference to "{${entity}}".`));
}
}
Expand All @@ -234,6 +238,26 @@ const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, ent
})
}

const updateEntityParentTreeWithAllEntityTypes = function(finalLUISJSON, entityParentTree)
{
(finalLUISJSON.prebuiltEntities || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same operation is done for all properties. This could be replaced by a for going over an array

(finalLUISJSON.patternAnyEntities || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
(finalLUISJSON.model_features || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
(finalLUISJSON.phraselists || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
(finalLUISJSON.regex_entities || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
(finalLUISJSON.closedLists || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
(finalLUISJSON.composites || []).forEach(entity => addEntityToParentTree(entityParentTree, entity.name));
}

const addEntityToParentTree = function(entityParentTree, entityName)
{
if (entityParentTree[entityName] === undefined) {
entityParentTree[entityName] = [["$root$"]];
} else {
entityParentTree[entityName].push(["$root$"]);
}
}

const constructEntityParentTree = function(entityCollection, entityParentTree, curPath)
{
entityCollection.forEach(entity => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,21 +754,6 @@ describe('V2 NDepth definitions using @ notation', function () {
.catch(err => done())
})

it('[V7 upgrade test] Patterns cannot have refernece to child entities', function(done) {
let luFile = `
@ ml userProfile =
- @ personName firstName
- @ personName lastName

@ prebuilt personName

# test
- [my] name is vishwac
- my first name is {@firstName} and last name is {@lastName}`;

parseFile.parseFile(luFile)
.then(res => done(res))
.catch(err => done())
})


});
15 changes: 15 additions & 0 deletions packages/lu/test/parser/lufile/parseFileContents.parseFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ const validateLUISBlob = require('./../../../src/parser/luis/luisValidator')
var chai = require('chai');
var assert = chai.assert;
describe('With helper functions', function () {
it('Parsefile correctly handles non nDepth entity references in patterns', function(done) {
let luFile = `@ list foo=
@ ml operation=
- @foo foo

# Test
- Pattern {foo}`;
parseFile.parseFile(luFile)
.then(res => {
assert.equal(res.LUISJsonStructure.patterns.length, 1)
assert.equal(res.LUISJsonStructure.patterns[0].pattern, "Pattern {foo}");
done()
})
})

it('parseFile includes defaults for LUIS app', function(done) {
let luFile = `@ simple entity1`;
parseFile.parseFile(luFile)
Expand Down