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: parser will throw an error when address is null/no param but param is present #1011

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/ruleset/v2/functions/channelParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export const channelParameters = createRulesetFunction<{ parameters: Record<stri
const results: IFunctionResult[] = [];

const parameters = parseUrlVariables(path);
if (parameters.length === 0) return;

const hasParameters = Object.keys(targetVal.parameters).length > 0;
if (hasParameters && (path === null || parameters.length === 0)) {
results.push({
message: `Channel has parameters defined, but the address is null or does not contain any parameters.`,
path: ctx.path.slice(0, -1),
});
return results;
}

const missingParameters = getMissingProps(parameters, targetVal.parameters);
if (missingParameters.length) {
Expand Down
19 changes: 19 additions & 0 deletions test/ruleset/rules/v2/asyncapi2-channel-parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ testRule('asyncapi2-channel-parameters', [
errors: [],
},

{
name: 'address has no parameters but parameters are defined',
document: {
asyncapi: '2.0.0',
channels: {
'users/signedUp': {
parameters: {
test: {},
},
},
},
},
errors: [{
message: 'Channel has parameters defined, but the address is null or does not contain any parameters',
path: ['channels', 'users/signedUp'],
severity: DiagnosticSeverity.Error,
},],
},

{
name: 'channel has not defined definition for one of the parameters',
document: {
Expand Down
Loading