Skip to content

Commit

Permalink
Don't create aliases with unsupported characters
Browse files Browse the repository at this point in the history
- Replace any unsupported characters with '-'
- Include SUSHI's new warning when processing aliases with
  unsupported characters
  • Loading branch information
jafeltra committed Feb 28, 2024
1 parent 9a83c90 commit 50584b3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/optimizer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
ExportableAlias
} from '../exportable';

const unsupportedAliasCharRegex = /[^a-zA-z0-9_\-\.]/g;

export function optimizeURL(
url: string,
aliases: ExportableAlias[],
Expand Down Expand Up @@ -102,6 +104,9 @@ export function resolveAliasFromURL(url: string, aliases: ExportableAlias[]): st
alias = `${aliasPart}_${counterPart}`;
}

// Replace unsupported characters, but allow $ at the start
alias = `$${alias.substring(1).replace(unsupportedAliasCharRegex, '-')}`;

aliases.push(new ExportableAlias(alias, url));
return alias;
}
Expand Down
6 changes: 6 additions & 0 deletions src/processor/AliasProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { InputStream, CommonTokenStream } from 'antlr4';
import FSHLexer from 'fsh-sushi/dist/import/generated/FSHLexer';
import FSHParser from 'fsh-sushi/dist/import/generated/FSHParser';

const aliasRegex = /^\$?[a-zA-z0-9_\-\.]*$/;

export class AliasProcessor {
static process(aliasFile: string): ExportableAlias[] {
// Load aliases from alias-file option.
Expand Down Expand Up @@ -61,6 +63,10 @@ export class AliasProcessor {
`Alias ${name} cannot include "|" since the "|" character is reserved for indicating a version`
);
return;
} else if (!aliasRegex.test(name)) {
logger.warn(
`Alias ${name} includes unsupported characters. Alias names can only contain letters, numbers, underscores ("_"), hyphens ("-"), and dots (".").`
);
}
if (aliases.has(name) && aliases.get(name) !== value) {
logger.error(
Expand Down
12 changes: 12 additions & 0 deletions test/optimizer/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ describe('optimizer', () => {
url: 'http://example.org/bar/foo'
});
});

it('should ensure newly created aliases only use allowed characters', () => {
const originalLength = aliases.length;
expect(resolveAliasFromURL('http://example.org/foo-bar_3.1~te$t', aliases)).toBe(
'$foo-bar_3.1-te-t'
);
expect(aliases).toHaveLength(originalLength + 1);
expect(aliases[aliases.length - 1]).toEqual({
alias: '$foo-bar_3.1-te-t',
url: 'http://example.org/foo-bar_3.1~te$t'
});
});
});
});
});
7 changes: 6 additions & 1 deletion test/processor/AliasProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ describe('AliasProcessor', () => {
it('should log errors for invalid aliases', () => {
const aliasFile = path.join(__dirname, 'fixtures', 'invalid-alias.fsh');
const result = AliasProcessor.process(aliasFile);
expect(result).toHaveLength(2);
expect(result).toHaveLength(3);
expect(loggerSpy.getAllMessages('error')).toHaveLength(2);
expect(loggerSpy.getMessageAtIndex(0, 'error')).toMatch(
/Alias \$not\|valid cannot include "\|"/s
);
expect(loggerSpy.getMessageAtIndex(1, 'error')).toMatch(/Alias \$valid cannot be redefined/s);
expect(loggerSpy.getAllMessages('warn')).toHaveLength(1);
expect(loggerSpy.getLastMessage('warn')).toMatch(
/Alias \$valid~ish includes unsupported characters/
);
});
});
});
4 changes: 3 additions & 1 deletion test/processor/fixtures/invalid-alias.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ Alias: $not|valid = http://example.org/CodeSystem/not-valid
// this alias is valid
Alias: somethingGood = http://example.org/ValueSet/something-good
// this alias is invalid, since it is already defined
Alias: $valid = http://example.org/already-defined
Alias: $valid = http://example.org/already-defined
// this alias is valid, but it has unsupported characters
Alias: $valid~ish = http://example.org/sort-of-valid

0 comments on commit 50584b3

Please sign in to comment.