Skip to content

Commit

Permalink
Fix: pattern matching urls (#2259)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome authored Nov 25, 2022
1 parent b1daf54 commit 48697c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/app/utils/resources/resources-filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IResource } from '../../../types/resources';
import { hasPlaceHolders } from '../sample-url-generation';

function getResourcesSupportedByVersion(
resources: IResource[],
Expand Down Expand Up @@ -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
}
11 changes: 4 additions & 7 deletions src/modules/suggestions/suggestions.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 48697c2

Please sign in to comment.