Skip to content
Closed
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
54 changes: 54 additions & 0 deletions src/ruleset/v3/functions/operationReplyAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createRulesetFunction } from '@stoplight/spectral-core';

import { hasRef, isObject, retrieveDeepData, toJSONPathArray } from '../../../utils';

type OperationReply = {
address?: unknown;
channel?: unknown;
};

function resolveLocalReference(document: Record<string, unknown>, value: unknown): unknown {
const visited = new Set<string>();

while (hasRef(value) && value.$ref.startsWith('#/') && !visited.has(value.$ref)) {
visited.add(value.$ref);
value = retrieveDeepData(document, toJSONPathArray(value.$ref));
}

return value;
}

export const operationReplyAddress = createRulesetFunction<OperationReply, null>(
{
input: {
type: 'object',
properties: {
address: {
type: 'object',
},
channel: {
type: 'object',
},
},
},
options: null,
},
(targetVal, _, ctx) => {
if (targetVal.address === undefined || !hasRef(targetVal.channel)) {
return;
}

const channel = resolveLocalReference(
ctx.document.data as Record<string, unknown>,
targetVal.channel,
);
if (!isObject(channel) || channel.address === undefined || channel.address === null) {
return;
}

return [{
message: 'A channel referenced by a reply with a dynamic address must have a null or undefined "address".',
path: [...ctx.path, 'channel'],
}];
},
);
15 changes: 15 additions & 0 deletions src/ruleset/v3/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { AsyncAPIFormats } from '../formats';
import { operationMessagesUnambiguity } from './functions/operationMessagesUnambiguity';
import { operationReplyAddress } from './functions/operationReplyAddress';
import { pattern } from '@stoplight/spectral-functions';

export const v3CoreRuleset = {
Expand Down Expand Up @@ -39,6 +40,20 @@ export const v3CoreRuleset = {
},
},
},
'asyncapi3-operation-reply-address': {
description: 'A channel referenced by an operation reply with a dynamic address must not define a concrete address.',
message: '{{error}}',
severity: 'error',
recommended: true,
resolved: false,
given: [
'$.operations.*.reply',
'$.components.operations.*.reply',
],
then: {
function: operationReplyAddress,
},
},

/**
* Channel Object rules
Expand Down
111 changes: 111 additions & 0 deletions test/ruleset/rules/v3/asyncapi3-operation-reply-address.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { testRule, DiagnosticSeverity } from '../../tester';

const baseDocument = {
asyncapi: '3.0.0',
info: {
title: 'Account Service',
version: '1.0.0',
},
};

function operation(channel: string, withDynamicAddress = true) {
return {
action: 'send',
channel: { $ref: '#/channels/request' },
reply: {
channel: { $ref: channel },
...(withDynamicAddress && {
address: { location: '$message.header#/REPLY_TOPIC' },
}),
},
};
}

const error = {
message: 'A channel referenced by a reply with a dynamic address must have a null or undefined "address".',
severity: DiagnosticSeverity.Error,
};

testRule('asyncapi3-operation-reply-address', [
{
name: 'rejects a concrete address on a root reply channel',
document: {
...baseDocument,
channels: {
request: { address: 'requests' },
reply: { address: 'replies' },
},
operations: {
sendRequest: operation('#/channels/reply'),
},
},
errors: [{
...error,
path: ['operations', 'sendRequest', 'reply', 'channel'],
}],
},
{
name: 'allows a null address on a root reply channel',
document: {
...baseDocument,
channels: {
request: { address: 'requests' },
reply: { address: null },
},
operations: {
sendRequest: operation('#/channels/reply'),
},
},
errors: [],
},
{
name: 'allows an omitted address on a root reply channel',
document: {
...baseDocument,
channels: {
request: { address: 'requests' },
reply: {},
},
operations: {
sendRequest: operation('#/channels/reply'),
},
},
errors: [],
},
{
name: 'allows a concrete channel address when the reply address is omitted',
document: {
...baseDocument,
channels: {
request: { address: 'requests' },
reply: { address: 'replies' },
},
operations: {
sendRequest: operation('#/channels/reply', false),
},
},
errors: [],
},
{
name: 'rejects a concrete address reached through component references',
document: {
...baseDocument,
channels: {
request: { address: 'requests' },
},
components: {
channels: {
replyAlias: { $ref: '#/components/channels/reply' },
reply: { address: 'replies' },
},
operations: {
sendRequest: operation('#/components/channels/replyAlias'),
},
},
},
errors: [{
...error,
path: ['components', 'operations', 'sendRequest', 'reply', 'channel'],
}],
},
]);
Loading