Skip to content

Commit

Permalink
Improve checking for API spec calls for autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed Mar 27, 2023
1 parent 1cbd7ea commit b858333
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
7 changes: 1 addition & 6 deletions app/controllers/api_docs/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def initialize(name, system_name)

def json
parsed_content = JSON.parse(file_content)
parsed_content['servers'] = [{ 'url' => backend_api? ? backend_base_host : '/' }]
parsed_content['servers'] = [{ 'url' => backend_base_host }] if backend_api?

parsed_content
end
Expand Down Expand Up @@ -115,7 +115,6 @@ def index
def show
system_name = params[:id].to_sym
api_file = (api_files.fetch(system_name) { raise ActiveRecord::RecordNotFound }).dup
update_base_url(api_file)
api_file['paths'] = exclude_forbidden_endpoints(api_file['paths']) if master_on_premises?

render json: api_file
Expand All @@ -137,8 +136,4 @@ def allowed_api?(api)
true
end
end

def update_base_url(api_spec)
api_spec['servers'] = [{ 'url' => helpers.base_url.gsub(%r{/$}, '') }] if api_spec['servers'][0]['url'] == '/'
end
end
6 changes: 3 additions & 3 deletions app/javascript/packs/active_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import SwaggerUI from 'swagger-ui'
import 'swagger-ui/dist/swagger-ui.css'

import { autocompleteInterceptor } from 'ActiveDocs/OAS3Autocomplete'
import { autocompleteRequestInterceptor } from 'ActiveDocs/OAS3Autocomplete'

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) => autocompleteInterceptor(response, accountDataUrl, serviceEndpoint, args.url)
const requestInterceptor = (request: SwaggerUI.Request) => autocompleteRequestInterceptor(request, accountDataUrl, serviceEndpoint)

SwaggerUI({
...args,
responseInterceptor
requestInterceptor
} as SwaggerUI.SwaggerUIOptions)
}
6 changes: 3 additions & 3 deletions app/javascript/packs/service_active_docs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwaggerUI from 'swagger-ui'
import 'swagger-ui/dist/swagger-ui.css'

import { autocompleteInterceptor } from 'ActiveDocs/OAS3Autocomplete'
import { autocompleteRequestInterceptor } from 'ActiveDocs/OAS3Autocomplete'

import 'ActiveDocs/swagger-ui-3-provider-patch.scss'

Expand All @@ -16,12 +16,12 @@ document.addEventListener('DOMContentLoaded', () => {
const { url = '', baseUrl = '', serviceEndpoint = '' } = container.dataset
const accountDataUrl = `${baseUrl}${DATA_URL}`

const responseInterceptor: SwaggerUI.SwaggerUIOptions['responseInterceptor'] = (response) => autocompleteInterceptor(response, accountDataUrl, serviceEndpoint, url)
const requestInterceptor: SwaggerUI.SwaggerUIOptions['requestInterceptor'] = (request) => autocompleteRequestInterceptor(request, accountDataUrl, serviceEndpoint)

SwaggerUI({
url,
// eslint-disable-next-line @typescript-eslint/naming-convention -- SwaggerUI API
dom_id: `#${containerId}`,
responseInterceptor
requestInterceptor
})
})
19 changes: 9 additions & 10 deletions app/javascript/src/ActiveDocs/OAS3Autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { fetchData } from 'utilities/fetchData'

import type { Response as SwaggerUIResponse } from 'swagger-ui'
import type { Request as SwaggerUIRequest, Response as SwaggerUIResponse } from 'swagger-ui'
import type { AccountData } from 'Types/SwaggerTypes'

const X_DATA_ATTRIBUTE = 'x-data-threescale-name'
Expand Down Expand Up @@ -136,17 +136,16 @@ const autocompleteOAS3 = async (response: SwaggerUIResponse, accountDataUrl: str
}

/**
* Intercept and process the response made by Swagger UI
* Apply transformations (inject servers list and autocomplete data) to the response for OpenAPI spec requests, and
* Intercept the request, and if it fetches the OpenAPI specification, apply a custom responseInterceptor that applies
* transformations (inject servers list and autocomplete data) to the response
* keep the responses to the actual API calls (made through 'Try it out') untouched
* @param response response to the request made through Swagger UI
* @param specUrl URL of the OpenAPI specification
* @param request request made through Swagger UI
* @param accountDataUrl URL of the data for autocompletion
* @param serviceEndpoint Public Base URL of the gateway, that will replace the URL in the "servers" object
*/
export const autocompleteInterceptor = (response: SwaggerUIResponse, accountDataUrl: string, serviceEndpoint: string, specUrl?: string): Promise<Response> | SwaggerUIResponse => {
if (specUrl !== response.url) {
return response
export const autocompleteRequestInterceptor = (request: SwaggerUIRequest, accountDataUrl: string, serviceEndpoint: string): Promise<SwaggerUIRequest> | SwaggerUIRequest => {
if (request.loadSpec) {
request.responseInterceptor = (response: SwaggerUIResponse) => autocompleteOAS3(response, accountDataUrl, serviceEndpoint)
}
return autocompleteOAS3(response, accountDataUrl, serviceEndpoint)
}
return request
}

This comment has been minimized.

Copy link
@jlledom

jlledom Mar 29, 2023

Contributor

No new line!

4 changes: 2 additions & 2 deletions app/javascript/src/ActiveDocs/ThreeScaleApiDocs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwaggerUI from 'swagger-ui'

import { fetchData } from 'utilities/fetchData'
import { autocompleteInterceptor } from 'ActiveDocs/OAS3Autocomplete'
import { autocompleteRequestInterceptor } from 'ActiveDocs/OAS3Autocomplete'

import type { ApiDocsServices } from 'Types/SwaggerTypes'

Expand All @@ -27,7 +27,7 @@ export const renderApiDocs = async (container: HTMLElement, apiDocsPath: string,
url,
// eslint-disable-next-line @typescript-eslint/naming-convention -- Swagger UI
dom_id: `#${domId}`,
responseInterceptor: (response) => autocompleteInterceptor(response, apiDocsAccountDataPath, '', url),
requestInterceptor: (request) => autocompleteRequestInterceptor(request, apiDocsAccountDataPath, ''),
tryItOutEnabled: true
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/integration/api_docs/services_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ def test_show_service_management
assert_equal "https://#{backend_config[:host]}", json["servers"][0]["url"]
end

test 'show the correct url for system APIs' do
test 'show the relative "/" path for system APIs' do
system_apis = ApiDocs::ServicesController::API_SYSTEM_NAMES - [:service_management_api]
system_apis.each do |system_name|
get api_docs_service_path(format: :json, id: system_name)
assert_response :success
json = JSON.parse(response.body)
assert_equal request.base_url, json["servers"][0]["url"]
servers = JSON.parse(response.body)["servers"]
assert_equal [{ 'url' => '/' }], servers
end
end

Expand Down

0 comments on commit b858333

Please sign in to comment.