Skip to content

Commit

Permalink
fix: add check that $ref value is a string (#92) (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Nov 9, 2022
1 parent 67e5c61 commit 5491120
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parser.ts
Expand Up @@ -41,8 +41,8 @@ function crawlChannelPropertiesForRefs(JSONSchema: AsyncAPIObject) {
* @returns {boolean}
* @private
*/
function isExternalReference(ref: string) {
return !ref.startsWith('#');
function isExternalReference(ref: string): boolean {
return typeof ref === 'string' && !ref.startsWith('#');
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/index.spec.ts
Expand Up @@ -40,4 +40,40 @@ describe('bundler should ', () => {

expect(message.$ref).toMatch('#/components/messages/UserSignedUp');
});

test('should not throw if value of `$ref` is not a string', async () => {
const files = ['./tests/wrong-ref-not-string.yaml'];

// If async function `bundle()` resolved Promise successfully, that means it
// did not throw exception during process of execution, which is the
// objective of testing.
expect(
await bundle(
files.map(file =>
fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')
),
{
referenceIntoComponents: true,
}
)
).resolves;
});

test('should not throw if value of `$ref` is absent', async () => {
const files = ['./tests/wrong-ref-absent.yaml'];

// If async function `bundle()` resolved Promise successfully, that means it
// did not throw exception during process of execution, which is the
// objective of testing.
expect(
await bundle(
files.map(file =>
fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')
),
{
referenceIntoComponents: true,
}
)
).resolves;
});
});
10 changes: 10 additions & 0 deletions tests/wrong-ref-absent.yaml
@@ -0,0 +1,10 @@
asyncapi: '2.2.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup:
subscribe:
message:
$ref:
10 changes: 10 additions & 0 deletions tests/wrong-ref-not-string.yaml
@@ -0,0 +1,10 @@
asyncapi: '2.2.0'
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup:
subscribe:
message:
$ref: 5

0 comments on commit 5491120

Please sign in to comment.