Skip to content

Commit

Permalink
fix: Fix bug in the swagger (#2571)
Browse files Browse the repository at this point in the history
* test - revert

Signed-off-by: at670475 <[email protected]>

* fix

Signed-off-by: at670475 <[email protected]>

* attempt 2

Signed-off-by: at670475 <[email protected]>

* attempt 3

Signed-off-by: at670475 <[email protected]>

* Change way to check for the object property

Signed-off-by: at670475 <[email protected]>

* Use in operator rather than undefined check

Signed-off-by: at670475 <[email protected]>

Signed-off-by: at670475 <[email protected]>
  • Loading branch information
taban03 authored Sep 7, 2022
1 parent 0956738 commit 36997c6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
32 changes: 20 additions & 12 deletions api-catalog-ui/frontend/src/components/Swagger/SwaggerUI.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,26 @@ export default class SwaggerUI extends Component {
setSwaggerState = () => {
const { selectedService, selectedVersion } = this.props;
let codeSnippets = null;
if (
selectedService.apis[selectedVersion || selectedService.defaultApiVersion] !== null &&
selectedService.apis[selectedVersion || selectedService.defaultApiVersion] !== undefined &&
Object.hasOwn(selectedService.apis[selectedVersion || selectedService.defaultApiVersion], 'codeSnippet')
) {
codeSnippets = selectedService.apis[selectedVersion || selectedService.defaultApiVersion].codeSnippet;
} else if (
selectedService.apis.default !== null &&
selectedService.apis.default !== undefined &&
Object.hasOwn(selectedService.apis.default, 'codeSnippet')
) {
codeSnippets = selectedService.apis.default.codeSnippet;
if (selectedService.apis.length !== 0) {
if (
selectedService.apis[selectedVersion] !== null &&
selectedService.apis[selectedVersion] !== undefined &&
'codeSnippet' in selectedService.apis[selectedVersion]
) {
codeSnippets = selectedService.apis[selectedVersion].codeSnippet;
} else if (
selectedService.apis[selectedService.defaultApiVersion] !== null &&
selectedService.apis[selectedService.defaultApiVersion] !== undefined &&
'codeSnippet' in selectedService.apis[selectedService.defaultApiVersion]
) {
codeSnippets = selectedService.apis[selectedService.defaultApiVersion].codeSnippet;
} else if (
selectedService.apis.default !== null &&
selectedService.apis.default !== undefined &&
'codeSnippet' in selectedService.apis.default
) {
codeSnippets = selectedService.apis.default.codeSnippet;
}
}
try {
// If no version selected use the default apiDoc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ describe('>>> Swagger component tests', () => {
afterEach(() => {
document.body.innerHTML = '';
});
beforeAll(() => {
// eslint-disable-next-line no-extend-native
Object.defineProperty(Object.prototype, 'hasOwn', {
value(prop) {
return Object.prototype.hasOwnProperty.call(this, prop);
},
});
});

it('should not render swagger if apiDoc is null', () => {
const service = {
Expand Down
23 changes: 13 additions & 10 deletions api-catalog-ui/frontend/src/utils/generateSnippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function getSnippetContent(req, target, codeSnippet) {
* @param title the code snippet title
* @param syntax the syntax used for indentation
* @param target the language target
* @param codeSnippet the code snippet
* @returns codeSnippet the code snippet
*/
export function generateSnippet(system, title, syntax, target, codeSnippet) {
Expand All @@ -88,16 +89,18 @@ export function CustomizedSnippedGenerator(codeSnippets) {
(ori, system) =>
(state, ...args) => {
let useSet = ori(state, ...args);
// eslint-disable-next-line no-restricted-syntax
for (const codeSnippet of codeSnippets) {
const newSnippet = generateSnippet(
system,
`Customized Snippet - ${codeSnippet.language}`,
codeSnippet.language,
'target',
codeSnippet
);
useSet = useSet.set(codeSnippet.endpoint + codeSnippet.language, newSnippet);
if (codeSnippets !== null && codeSnippets !== undefined) {
// eslint-disable-next-line no-restricted-syntax
for (const codeSnippet of codeSnippets) {
const newSnippet = generateSnippet(
system,
`Customized Snippet - ${codeSnippet.language}`,
codeSnippet.language,
'target',
codeSnippet
);
useSet = useSet.set(codeSnippet.endpoint + codeSnippet.language, newSnippet);
}
}
useSet = useSet
.set(
Expand Down

0 comments on commit 36997c6

Please sign in to comment.