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

Commit

Permalink
[bf-lu] Fix throwing exception bug in validateResponse when utterance…
Browse files Browse the repository at this point in the history
… reference is invalid (#1079)

* support output to file for kb:export command

* fix exception issue in validateResponse

* fix named groups not supported in composer
  • Loading branch information
feich-ms authored Jan 15, 2021
1 parent e06f70f commit 2b00408
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
7 changes: 2 additions & 5 deletions packages/lu/src/parser/lufile/parseFileContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ const parseFileContentsModule = {
// parse entity section
parseAndHandleEntitySection(parsedContent, resource, false, undefined, config);

// parse entity section
parseAndHandleEntitySection(parsedContent, resource, false, undefined, config);

// validate simple intent section
parseAndHandleSimpleIntentSection(parsedContent, resource, config)

Expand All @@ -139,11 +136,11 @@ const parseFileContentsModule = {
}

} catch(e) {
if (e instanceof exception) {
if (e instanceof exception && e.diagnostics) {
errors.push(...e.diagnostics)
} else {
errors.push(BuildDiagnostic({
message: e.message
message: e.message || e.text
}))
}
}
Expand Down
26 changes: 22 additions & 4 deletions packages/lu/src/parser/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,18 @@ const helpers = {
let linkValue = linkValueList[0].replace('(', '').replace(')', '');
if (linkValue === '') throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}"`));
// reference can either be #<Intent-Name> or #? or /*#? or /**#? or #*utterance* or #<Intent-Name>*patterns*
let splitRegExp = new RegExp(/^(?<fileName>.*?)(?<segment>#|\*+)(?<path>.*?)$/gim);
let splitRegExp = new RegExp(/^(.*?)(#|\*+)(.*?)$/gim);
let splitReference = splitRegExp.exec(linkValue);
if (!splitReference) throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}".\n Reference needs a qualifier - either a #Intent-Name or #? or *#? or **#? or #*utterances* etc.`));
if (!splitReference) {
throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}".\n Reference needs a qualifier - either a #Intent-Name or #? or *#? or **#? or #*utterances* etc.`));
} else {
splitReference.groups = {
fileName: splitReference[1],
segment: splitReference[2],
path: splitReference[3]
}
}

if (splitReference.groups.fileName && srcId && luSearchFn) {
let luObjects = await luSearchFn(srcId, [{filePath: splitReference.groups.fileName, includeInCollate: false}])
if (luObjects && luObjects.length > 0) splitReference.groups.fileName = luObjects[0].id
Expand All @@ -103,9 +112,18 @@ const helpers = {
let linkValue = linkValueList[0].replace('(', '').replace(')', '');
if (linkValue === '') throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}"`));
// reference can either be #<Intent-Name> or #? or /*#? or /**#? or #*utterance* or #<Intent-Name>*patterns*
let splitRegExp = new RegExp(/^(?<fileName>.*?)(?<segment>#|\*+)(?<path>.*?)$/gim);
let splitRegExp = new RegExp(/^(.*?)(#|\*+)(.*?)$/gim);
let splitReference = splitRegExp.exec(linkValue);
if (!splitReference) throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}".\n Reference needs a qualifier - either a #Intent-Name or #? or *#? or **#? or #*utterances* etc.`));
if (!splitReference) {
throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}".\n Reference needs a qualifier - either a #Intent-Name or #? or *#? or **#? or #*utterances* etc.`));
} else {
splitReference.groups = {
fileName: splitReference[1],
segment: splitReference[2],
path: splitReference[3]
}
}

if (splitReference.groups.segment.includes('*')) {
if (splitReference.groups.path === '') {
throw (new exception(retCode.errorCode.INVALID_LU_FILE_REF, `[ERROR]: Invalid LU File Ref: "${utterance}".\n '*' and '**' can only be used with QnA qualitifier. e.g. *#? and **#?`));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ describe('ValidateResource don\'t change resource in memory', () => {
assert.equal(luresource.Sections.length, 2);
});
});

describe('Invalid utterance references', () => {
it('Throw exception when utterance reference is invalid', function () {
let fileContent = `# Cancel
- [Cancel](./general)`;
let luresource = luparser.parse(fileContent);
let errors = validateResource(luresource);
assert.include(errors[0].Message, '[ERROR]: Invalid LU File Ref: "[Cancel](./general)".')
});
});

0 comments on commit 2b00408

Please sign in to comment.