Skip to content

Commit 1b8844c

Browse files
authored
fix: Swagger server url transform (#1934)
* Fix transformation of swagger servers urls Signed-off-by: Carson Cook <carson.cook@ibm.com> * Fix bug in tests with undefined swagger version Signed-off-by: Carson Cook <carson.cook@ibm.com> * Add test for swagger server url transform Signed-off-by: Carson Cook <carson.cook@ibm.com>
1 parent 5dcf55e commit 1b8844c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

api-catalog-ui/frontend/src/components/Swagger/Swagger.jsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ function transformSwaggerToCurrentHost(swagger) {
99

1010
if (swagger.servers !== null && swagger.servers !== undefined) {
1111
for (let i = 0; i < swagger.servers.length; i += 1) {
12-
const url = `${window.location.protocol}//${window.location.host}/${swagger.servers[i].url}`;
13-
swagger.servers[i].url = url;
12+
const location = `${window.location.protocol}//${window.location.host}`;
13+
try {
14+
const swaggerUrl = new URL(swagger.servers[i].url);
15+
swagger.servers[i].url = location + swaggerUrl.pathname;
16+
} catch (e) {
17+
// not a proper url, assume it is an endpoint
18+
swagger.servers[i].url = location + swagger.servers[i];
19+
}
1420
}
1521
}
1622

@@ -71,7 +77,7 @@ export default class SwaggerUI extends Component {
7177
try {
7278
// If no version selected use the default apiDoc
7379
if (
74-
selectedVersion === null &&
80+
(selectedVersion === null || selectedVersion === undefined) &&
7581
selectedService.apiDoc !== null &&
7682
selectedService.apiDoc !== undefined &&
7783
selectedService.apiDoc.length !== 0
@@ -85,7 +91,7 @@ export default class SwaggerUI extends Component {
8591
plugins: [this.customPlugins],
8692
});
8793
}
88-
if (selectedVersion !== null) {
94+
if (selectedVersion !== null && selectedVersion !== undefined) {
8995
const url = `${process.env.REACT_APP_GATEWAY_URL +
9096
process.env.REACT_APP_CATALOG_HOME +
9197
process.env.REACT_APP_APIDOC_UPDATE}/${selectedService.serviceId}/${selectedVersion}`;

api-catalog-ui/frontend/src/components/Swagger/Swagger.test.jsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable no-undef */
22
import { shallow } from 'enzyme';
33
import SwaggerUI from './Swagger';
4+
import { act } from "react-dom/test-utils";
5+
import { render } from 'react-dom';
46

57
describe('>>> Swagger component tests', () => {
68
it('should not render swagger if apiDoc is null', () => {
@@ -16,11 +18,37 @@ describe('>>> Swagger component tests', () => {
1618
};
1719
const wrapper = shallow(
1820
<div>
19-
<SwaggerUI service={service} />
21+
<SwaggerUI selectedService={service} />
2022
</div>
2123
);
2224
const swaggerDiv = wrapper.find('#swaggerContainer');
2325

2426
expect(swaggerDiv.length).toEqual(0);
2527
});
28+
29+
it('should transform swagger server url', async () => {
30+
const endpoint = '/enabler/api/v1';
31+
const service = {
32+
serviceId: 'testservice',
33+
title: 'Spring Boot Enabler Service',
34+
description: 'Dummy Service for enabling others',
35+
status: 'UP',
36+
secured: false,
37+
homePageUrl: 'http://localhost:10013/enabler/',
38+
basePath: '/enabler/api/v1',
39+
apiDoc: JSON.stringify({
40+
openapi: "3.0.0",
41+
servers: [{url: 'https://bad.com' + endpoint}],
42+
}),
43+
apiId: {
44+
default: 'enabler'
45+
}
46+
};
47+
48+
const container = document.createElement('div');
49+
document.body.appendChild(container);
50+
51+
await act(async () => render(<SwaggerUI selectedService={service}/>, container));
52+
expect(container.textContent).toContain('Servershttp://localhost' + endpoint);
53+
});
2654
});

0 commit comments

Comments
 (0)