Skip to content

Commit

Permalink
fix: operation messages references should point to channels (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Dec 1, 2023
1 parent 3437140 commit b660c05
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/third-version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlainObject, createRefObject, isRefObject, sortObjectKeys, getValueByRef, getValueByPath, createRefPath } from './utils';
import { isPlainObject, createRefObject, isRefObject, sortObjectKeys, getValueByRef, getValueByPath, createRefPath, isRemoteRef } from './utils';
import type { AsyncAPIDocument, ConvertOptions, ConvertV2ToV3Options, ConvertFunction } from './interfaces';

export const converters: Record<string, ConvertFunction> = {
Expand Down Expand Up @@ -111,6 +111,7 @@ type ToStandaloneOperationData = {
options: any,
oldPath: string[]
}

/**
* Convert operation part of channels into standalone operations
*/
Expand Down Expand Up @@ -304,7 +305,7 @@ function applyMessageRefsToOperation(serializedMessages: Record<string, any>, ne
if (Object.keys(serializedMessages ?? {}).length > 0 ) {
const newOperationMessages: Array<any> = [];
Object.entries(serializedMessages).forEach(([messageId, messageValue]) => {
if (isRefObject(messageValue)) {
if (isRemoteRef(messageValue)) {
// shallow copy of JS reference
newOperationMessages.push({ ...messageValue });
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export function isRefObject(value: unknown): boolean {
return Boolean(value && '$ref' in (value as { $ref: any }));
}

export function isRemoteRef(value: any): boolean {
return isRefObject(value) && !value.$ref.startsWith('#')
}

export function getValueByRef(root: any, ref: string) {
if (!ref.startsWith('#')) {
return;
Expand Down
4 changes: 2 additions & 2 deletions test/input/2.6.0/for-3.0.0-with-deep-local-references.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ channels:
'turnOn':
publish:
message:
$ref: '#/components/messages/lightMeasured'
$ref: '#/components/messages/turnOnOff'
subscribe:
message:
oneOf:
Expand All @@ -38,7 +38,7 @@ components:
'someChannel':
publish:
message:
$ref: '#/components/messages/lightMeasured'
$ref: '#/components/messages/turnOnOff'
subscribe:
message:
oneOf:
Expand Down
14 changes: 7 additions & 7 deletions test/output/3.0.0/from-2.6.0-with-deep-local-references.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ channels:
address: turnOn
messages:
publish.message:
$ref: '#/components/messages/lightMeasured'
$ref: '#/components/messages/turnOnOff'
subscribe.message.0:
$ref: '#/components/messages/turnOnOff'
customMessageId:
Expand All @@ -42,21 +42,21 @@ operations:
channel:
$ref: '#/channels/turnOn'
messages:
- $ref: '#/components/messages/lightMeasured'
- $ref: '#/channels/turnOn/messages/publish.message'
turnOn.subscribe:
action: send
channel:
$ref: '#/channels/turnOn'
messages:
- $ref: '#/components/messages/turnOnOff'
- $ref: '#/channels/turnOn/messages/subscribe.message.0'
- $ref: '#/channels/turnOn/messages/customMessageId'
components:
channels:
someChannel:
address: someChannel
messages:
publish.message:
$ref: '#/components/messages/lightMeasured'
$ref: '#/components/messages/turnOnOff'
subscribe.message.0:
$ref: '#/components/messages/turnOnOff'
customMessageId:
Expand Down Expand Up @@ -98,11 +98,11 @@ components:
channel:
$ref: '#/components/channels/someChannel'
messages:
- $ref: '#/components/messages/lightMeasured'
- $ref: '#/components/channels/someChannel/messages/publish.message'
someChannel.subscribe:
action: send
channel:
$ref: '#/components/channels/someChannel'
messages:
- $ref: '#/components/messages/turnOnOff'
- $ref: '#/components/channels/someChannel/messages/customMessageId'
- $ref: '#/components/channels/someChannel/messages/subscribe.message.0'
- $ref: '#/components/channels/someChannel/messages/customMessageId'
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ components:
channel:
$ref: '#/components/channels/usedChannel'
messages:
- $ref: '#/components/messages/lightMeasured'
- $ref: '#/components/channels/usedChannel/messages/publish.message'
usedChannel.subscribe:
action: send
channel:
$ref: '#/components/channels/usedChannel'
messages:
- $ref: '#/components/messages/turnOnOff'
- $ref: '#/components/channels/usedChannel/messages/subscribe.message.0'
- $ref: '#/components/channels/usedChannel/messages/customMessageId'
- $ref: '#/components/channels/usedChannel/messages/subscribe.message.2'
- $ref: 'https://example.com/message'
Expand All @@ -182,4 +182,4 @@ components:
scopes:
- 'write:pets'
messages:
- $ref: '#/components/messages/dimLight'
- $ref: '#/components/channels/unusedChannel/messages/dimLight.message'
13 changes: 8 additions & 5 deletions test/output/3.0.0/from-2.6.0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ operations:
$ref: >-
#/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1on
messages:
- $ref: '#/components/messages/lightMeasured'
- $ref: >-
#/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1on/messages/publish.message
'smartylighting/streetlights/1/0/action/{streetlightId}/turn/on.subscribe':
action: send
channel:
$ref: >-
#/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1on
messages:
- $ref: '#/components/messages/turnOnOff'
- $ref: >-
#/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1on/messages/subscribe.message.0
- $ref: >-
#/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1turn~1on/messages/customMessageId
- $ref: >-
Expand All @@ -159,7 +161,7 @@ operations:
channel:
$ref: '#/channels/customChannelId'
messages:
- $ref: '#/components/messages/turnOnOff'
- $ref: '#/channels/customChannelId/messages/turnOnOff.message'
dimLight:
action: send
channel:
Expand All @@ -176,7 +178,8 @@ operations:
scopes:
- 'write:pets'
messages:
- $ref: '#/components/messages/dimLight'
- $ref: >-
#/channels/smartylighting~1streetlights~11~10~1action~1{streetlightId}~1dim/messages/dimLight.message
components:
messages:
lightMeasured:
Expand Down Expand Up @@ -254,4 +257,4 @@ components:
'read:pets': read your pets
parameters:
streetlightId:
description: The ID of the streetlight.
description: The ID of the streetlight.

0 comments on commit b660c05

Please sign in to comment.