-
-
Notifications
You must be signed in to change notification settings - Fork 381
refactor: update getQueryParams to support multiple channels #2026
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
Open
SushanthMusham
wants to merge
3
commits into
asyncapi:master
Choose a base branch
from
SushanthMusham:fix-getqueryparams-all-channels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
895f788
refactor: update getQueryParams to support multiple channels and fix …
SushanthMusham 56957ea
refactor: introduce getFirstChannelQueryParams helper to DRY up compa…
SushanthMusham 0c2900e
chore: use null-prototype objects for security as suggested by CodeRa…
SushanthMusham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,83 @@ | ||
| /** | ||
| * Extracts default query parameters from a channel’s WebSocket binding. | ||
| * Extracts default query parameters from all channels' WebSocket bindings. | ||
| * | ||
| * @param {Map<string,string>} channels - A Map representing all AsyncAPI channels. | ||
| * @returns {Map<string,string>} A Map whose keys are parameter names and whose values are their defaults (or `''`). | ||
| * @param {Object} channels - An object representing all AsyncAPI channels. It assumes this object provides `.isEmpty()` and `.all()` methods. | ||
| * @returns {Object|null} An object whose keys are channel names and whose values are objects of their defaults (or `''`), or null if none exist. | ||
| * | ||
| * @example | ||
| * // Suppose channel.bindings().get("ws").values() returns: | ||
| * // Suppose your AsyncAPI document has a 'chat' channel with a WS binding: | ||
| * // { query: { properties: { foo: { default: 'bar' }, baz: {} } } } | ||
| * const params = getQueryParams(channel); | ||
| * console.log(params.get('foo')); // → 'bar' | ||
| * console.log(params.get('baz')); // → '' | ||
| * const params = getQueryParams(channels); | ||
| * console.log(params.chat.foo); // → 'bar' | ||
| * console.log(params.chat.baz); // → '' | ||
| */ | ||
| function getQueryParams(channels) { | ||
| // current implementation assumes there is always one channel | ||
| // at the moment only WebSocket binding support query params and the use case for WebSocket is that there is always one channel per AsyncAPI document | ||
| const channel = !channels.isEmpty() && channels.all().entries().next().value[1]; | ||
|
|
||
| const queryMap = new Map(); | ||
|
|
||
| const bindings = channel?.bindings?.(); | ||
| const hasWsBinding = bindings?.has('ws'); | ||
|
|
||
| if (!hasWsBinding) { | ||
| if (!channels || channels.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| const wsBinding = bindings.get('ws'); | ||
| const query = wsBinding.value()?.query; | ||
| //we do not throw error, as user do not have to use query params, we just exit with null as it doesn't make sense to continue with query building | ||
| if (!query) { | ||
| return null; | ||
| const result = Object.create(null); | ||
|
|
||
| // Loop through every single channel | ||
| for (const [channelName, channel] of channels.all().entries()) { | ||
| const bindings = channel?.bindings?.(); | ||
| const hasWsBinding = bindings?.has('ws'); | ||
|
|
||
| // If this specific channel doesn't have a ws binding, skip it and move to the next | ||
| if (!hasWsBinding) { | ||
| continue; | ||
| } | ||
|
|
||
| const wsBinding = bindings.get('ws'); | ||
| const query = wsBinding.value()?.query; | ||
|
|
||
| // If no query, skip to the next channel | ||
| if (!query) { | ||
| continue; | ||
| } | ||
|
|
||
| // the JSON Schema properties | ||
| const properties = query.properties; | ||
| if (!properties || typeof properties !== 'object') { | ||
| continue; | ||
| } | ||
|
|
||
| // Populate the parameters for this specific channel | ||
| const channelParams = Object.create(null); | ||
| for (const [key, schema] of Object.entries(properties)) { | ||
| const value = schema.default ?? ''; | ||
| channelParams[key] = String(value); | ||
| } | ||
|
|
||
| // Add the populated object to our main result object under the channel's name | ||
| if (Object.keys(channelParams).length > 0) { | ||
| result[channelName] = channelParams; | ||
| } | ||
| } | ||
|
|
||
| // If we didn't find any ws query params across any channels, return null to preserve the old behavior | ||
| return Object.keys(result).length > 0 ? result : null; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the query parameters for the first available WebSocket channel. | ||
| * This acts as a backward-compatibility helper for templates expecting a single Map of parameters. | ||
| * | ||
| * @param {Object} channels - The AsyncAPI channels collection object. | ||
| * @returns {Map|null} A Map of query parameters for the first channel, or null if none exist. | ||
| */ | ||
| function getFirstChannelQueryParams(channels) { | ||
| const allQueryParams = getQueryParams(channels); | ||
|
|
||
| // Drill into the JSON Schema properties | ||
| const properties = query.properties; | ||
| if (!properties || typeof properties !== 'object') { | ||
| return null; | ||
| if (allQueryParams && Object.keys(allQueryParams).length > 0) { | ||
| const firstChannelName = Object.keys(allQueryParams)[0]; | ||
| return new Map(Object.entries(allQueryParams[firstChannelName])); | ||
| } | ||
|
|
||
| // Populate the map, preserving defaults | ||
| for (const [key, schema] of Object.entries(properties)) { | ||
| const value = schema.default ?? ''; | ||
| queryMap.set(key, String(value)); | ||
| } | ||
|
|
||
| return queryMap; | ||
| return null; | ||
| } | ||
|
|
||
| module.exports = { | ||
| getQueryParams | ||
| getQueryParams, | ||
| getFirstChannelQueryParams | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.