From d68e03f33ea1da49d051ecef4106c101a492bfbb Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Thu, 24 Nov 2022 11:48:05 +0300 Subject: [PATCH 1/2] match urls based on url pattern --- src/app/utils/resources/resources-filter.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/app/utils/resources/resources-filter.ts b/src/app/utils/resources/resources-filter.ts index 9cd366593..3adaac5c3 100644 --- a/src/app/utils/resources/resources-filter.ts +++ b/src/app/utils/resources/resources-filter.ts @@ -1,4 +1,5 @@ import { IResource } from '../../../types/resources'; +import { hasPlaceHolders } from '../sample-url-generation'; function getResourcesSupportedByVersion( resources: IResource[], @@ -42,8 +43,25 @@ function searchResources(haystack: IResource[], needle: string): IResource[] { return foundResources; } +function getMatchingResourceForUrl(url: string, resources: IResource[]): IResource | undefined { + const parts = url.split('/').filter(k => k !== ''); + let matching = [...resources]; + let node; + for (const path of parts) { + if (hasPlaceHolders(path)) { + node = matching.find(k => hasPlaceHolders(k.segment)); + matching = node?.children || []; + } else { + node = matching.find(k => k.segment === path); + matching = node?.children || []; + } + } + return node; +} + export { searchResources, getResourcesSupportedByVersion, - versionExists + versionExists, + getMatchingResourceForUrl } From 6f36c0a0300846a7240ec6c2e7caabc4bb61cda2 Mon Sep 17 00:00:00 2001 From: Charles Wahome Date: Thu, 24 Nov 2022 11:49:21 +0300 Subject: [PATCH 2/2] use matching resources for the url --- src/modules/suggestions/suggestions.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/modules/suggestions/suggestions.ts b/src/modules/suggestions/suggestions.ts index 2ef3f97a9..707a43c6f 100644 --- a/src/modules/suggestions/suggestions.ts +++ b/src/modules/suggestions/suggestions.ts @@ -1,6 +1,7 @@ import { ISuggestions, SignContext } from '.'; import { parseOpenApiResponse } from '../../app/utils/open-api-parser'; import { + getMatchingResourceForUrl, getResourcesSupportedByVersion } from '../../app/utils/resources/resources-filter'; import { IOpenApiParseContent, IOpenApiResponse, IParsedOpenApiResponse } from '../../types/open-api'; @@ -44,13 +45,9 @@ class Suggestions implements ISuggestions { if (!url) { return this.createOpenApiResponse(versionedResources, url); } else { - const parts = url.split('/'); - let toSearch = [...versionedResources]; - for (const element of parts) { - toSearch = toSearch.find(k => k.segment === element)?.children || []; - } - if (toSearch.length > 0) { - return this.createOpenApiResponse(toSearch, url) + const matching = getMatchingResourceForUrl(url, versionedResources); + if (matching && matching.children.length > 0) { + return this.createOpenApiResponse(matching.children, url) } } return null;