Skip to content

Commit

Permalink
fix: support customized code snippets in case of endpoint with query …
Browse files Browse the repository at this point in the history
…params (#3665)

* fix issue with custom snippets

Signed-off-by: Andrea Tabone <[email protected]>

* add test

Signed-off-by: Andrea Tabone <[email protected]>

* fix sonarcloud

Signed-off-by: Andrea Tabone <[email protected]>

---------

Signed-off-by: Andrea Tabone <[email protected]>
  • Loading branch information
taban03 authored Jul 24, 2024
1 parent ce9053d commit 5e024f4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api-catalog-ui/frontend/src/utils/generateSnippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export function getSnippetContent(req, target, codeSnippet) {
// get extended info about request
const { spec, oasPathMethod } = req.toJS();
const { path, method } = oasPathMethod;
// run OpenAPISnippet for target node
const targets = [target];
// Include query parameters in the path for comparison
const query = req?.get('query');
const queryString = query ? `?${new URLSearchParams(query).toString()}` : '';
const fullPath = path + queryString;
let snippet;
try {
// set request snippet content
Expand All @@ -43,13 +45,13 @@ export function getSnippetContent(req, target, codeSnippet) {
codeSnippet.codeBlock !== undefined &&
codeSnippet.codeBlock !== null
) {
if (codeSnippet.endpoint === path) {
if (codeSnippet.endpoint === fullPath) {
snippet = codeSnippet.codeBlock;
} else {
snippet = null;
}
} else {
snippet = OpenAPISnippet.getEndpointSnippets(spec, path, method, targets).snippets[0].content;
snippet = OpenAPISnippet.getEndpointSnippets(spec, path, method, [target]).snippets[0].content;
}
} catch (err) {
snippet = JSON.stringify(snippet);
Expand Down
73 changes: 73 additions & 0 deletions api-catalog-ui/frontend/src/utils/generateSnippets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('>>> Code snippet generator', () => {
};

const req = {
get: jest.fn(),
toJS: () => ({
spec,
oasPathMethod,
Expand All @@ -80,6 +81,73 @@ describe('>>> Code snippet generator', () => {
expect(result).toEqual(expectedResult);
});

it('generate a snippet for endpoint with query parameter', () => {
const system = {
Im: {
fromJS: (obj) => obj,
},
};
const title = 'Java Unirest';
const syntax = 'java';
const target = 'java_unirest';

const spec = {
paths: {
'/path/to/api': {
get: {
parameters: [
{
name: 'parameter',
in: 'query',
required: true,
type: 'string',
},
],
responses: {
200: {
description: 'Response description',
schema: {
$ref: '#/definitions/SomeSchema',
},
},
},
},
},
},
};

const oasPathMethod = {
path: '/path/to/api',
method: 'get',
};

const query = { parameter: 'value' };

const req = {
get: jest.fn((key) => {
if (key === 'query') {
return query;
}
return undefined;
}),
toJS: () => ({
spec,
oasPathMethod,
}),
};

const codeSnippet = {
endpoint: '/path/to/api?parameter=value',
language: 'java',
codeBlock: 'Some java code;',
};

const result = generateSnippet(system, title, syntax, target, codeSnippet).fn(req);
const expectedResult = 'Some java code;';

expect(result).toEqual(expectedResult);
});

it('should call mutatedRequestFor with path and method', () => {
const ori = jest.fn();
const requestFor = wrapSelectors.spec.wrapSelectors.requestFor(ori);
Expand Down Expand Up @@ -176,6 +244,7 @@ describe('>>> Code snippet generator', () => {
};

const req = {
get: jest.fn(),
toJS: () => ({
spec,
oasPathMethod,
Expand Down Expand Up @@ -210,6 +279,7 @@ describe('>>> Code snippet generator', () => {
};

const req = {
get: jest.fn(),
toJS: () => ({
spec,
oasPathMethod,
Expand Down Expand Up @@ -245,6 +315,7 @@ describe('>>> Code snippet generator', () => {
};

const req = {
get: jest.fn(),
toJS: () => ({
spec,
oasPathMethod,
Expand Down Expand Up @@ -276,7 +347,9 @@ describe('>>> Code snippet generator', () => {
path: '/path/to/api',
method: 'get',
};

const req = {
get: jest.fn(),
toJS: () => ({
spec,
oasPathMethod,
Expand Down

0 comments on commit 5e024f4

Please sign in to comment.