Skip to content

Commit

Permalink
THREESCALE-3927 removed servers from response body
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-soni1104 committed Mar 6, 2023
1 parent 2cc5d0b commit dd43896
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/javascript/packs/active_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'ActiveDocs/swagger-ui-3-patch.scss'

const accountDataUrl = '/api_docs/account_data.json'

window.SwaggerUI = (args: SwaggerUI.SwaggerUIOptions, serviceEndpoint: string) => {
const responseInterceptor = (response: SwaggerUI.Response) => autocompleteOAS3(response, accountDataUrl, serviceEndpoint)
window.SwaggerUI = (args: SwaggerUI.SwaggerUIOptions) => {
const responseInterceptor = (response: SwaggerUI.Response) => autocompleteOAS3(response, accountDataUrl)

SwaggerUI({
...args,
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/packs/provider_active_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'ActiveDocs/swagger-ui-3-provider-patch.scss'

const accountDataUrl = '/p/admin/api_docs/account_data.json'

window.SwaggerUI = (args: SwaggerUI.SwaggerUIOptions, serviceEndpoint: string) => {
const responseInterceptor = (response: SwaggerUI.Response) => autocompleteOAS3(response, accountDataUrl, serviceEndpoint)
window.SwaggerUI = (args: SwaggerUI.SwaggerUIOptions) => {
const responseInterceptor = (response: SwaggerUI.Response) => autocompleteOAS3(response, accountDataUrl)

SwaggerUI({
...args,
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/packs/service_active_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ document.addEventListener('DOMContentLoaded', () => {
if (!container) {
throw new Error('The target ID was not found: ' + containerId)
}
const { url, baseUrl, serviceEndpoint = '' } = container.dataset
const { url, baseUrl } = container.dataset
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- FIXME
const accountDataUrl = `${baseUrl}${DATA_URL}`

const responseInterceptor: SwaggerUI.SwaggerUIOptions['responseInterceptor'] = (response) => autocompleteOAS3(response, accountDataUrl, serviceEndpoint)
const responseInterceptor: SwaggerUI.SwaggerUIOptions['responseInterceptor'] = (response) => autocompleteOAS3(response, accountDataUrl)

SwaggerUI({
url,
Expand Down
23 changes: 4 additions & 19 deletions app/javascript/src/ActiveDocs/OAS3Autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,6 @@ const injectAutocompleteToResponseBody = (responseBody: string | { paths?: Recor
return res
}

const injectServerToResponseBody = (responseBody: any | string, serviceEndpoint: string): any | string => {
if (typeof responseBody === 'string') {
return responseBody
}

const originalServers = responseBody.servers || []
const servers = serviceEndpoint ? [{ url: serviceEndpoint }] : originalServers

return {
...responseBody,
servers
}
}

export interface Response extends SwaggerUIResponse {
body: {
servers: unknown;
Expand All @@ -99,18 +85,17 @@ export interface Response extends SwaggerUIResponse {
text: string;
}

export const autocompleteOAS3 = async (response: SwaggerUIResponse, accountDataUrl: string, serviceEndpoint: string): Promise<Response> => {
const bodyWithServer = injectServerToResponseBody(response.body, serviceEndpoint)
export const autocompleteOAS3 = async (response: SwaggerUIResponse, accountDataUrl: string): Promise<Response> => {
const data = await fetchData<{ results: AccountData }>(accountDataUrl)

let body = undefined
try {
body = data.results
? injectAutocompleteToResponseBody(bodyWithServer, data.results)
: bodyWithServer
? injectAutocompleteToResponseBody(response.body, data.results)
: response.body
} catch (error: unknown) {
console.error(error)
body = bodyWithServer
body = response.body
}

return {
Expand Down
8 changes: 1 addition & 7 deletions spec/javascripts/ActiveDocs/OAS3Autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,8 @@ const accountData = {
const fetchDataSpy = jest.spyOn(utils, 'fetchData')
fetchDataSpy.mockResolvedValue(accountData)

it('should inject servers to response body', () => {
return autocompleteOAS3(response, accountDataUrl, serviceEndpoint).then(res => {
expect(res.body.servers).toEqual([{ 'url': 'foo/bar/serviceEndpoint' }])
})
})

it('should autocomplete fields with x-data-threescale-name property', () => {
return autocompleteOAS3(response, accountDataUrl, serviceEndpoint).then(res => {
return autocompleteOAS3(response, accountDataUrl).then(res => {
const examplesFirstParam = res.body.paths['/'].get.parameters[0].examples
const examplesSecondParam = res.body.paths['/'].get.parameters[1].examples

Expand Down

0 comments on commit dd43896

Please sign in to comment.