Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add check that $ref value is a string (#92) #94

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
aeworxet marked this conversation as resolved.
Show resolved Hide resolved