diff --git a/app/javascript/src/ActiveDocs/ThreeScaleApiDocs.ts b/app/javascript/src/ActiveDocs/ThreeScaleApiDocs.ts index 5a474ccc57..afce646ae2 100644 --- a/app/javascript/src/ActiveDocs/ThreeScaleApiDocs.ts +++ b/app/javascript/src/ActiveDocs/ThreeScaleApiDocs.ts @@ -9,6 +9,8 @@ import { autocompleteRequestInterceptor } from 'ActiveDocs/OAS3Autocomplete' import type { AccountDataResponse, ApiDocsServices, BackendApiReportBody, BackendApiTransaction, BodyValue, BodyValueObject, FormData } from 'Types/SwaggerTypes' import type { ExecuteData } from 'swagger-client/es/execute' import type { SwaggerUIPlugin } from 'swagger-ui' +import type { Component } from 'react' +import type { SwaggerUIContext, ParameterIncludeEmptyProperties } from 'swagger-ui-utils' const getApiSpecUrl = (baseUrl: string, specPath: string): string => { return `${baseUrl.replace(/\/$/, '')}${specPath}` @@ -118,6 +120,18 @@ const RequestBodyTransformerPlugin: SwaggerUIPlugin = () => { } } +const UncheckSendEmptyValuePlugin: SwaggerUIPlugin = () => { + return { + wrapComponents: { + // eslint-disable-next-line @typescript-eslint/naming-convention + ParameterIncludeEmpty: (originalComponent: Component, { React }: SwaggerUIContext ) => function ParameterIncludeEmptyWrapped (props: ParameterIncludeEmptyProperties) { + props.isIncludedOptions.defaultValue = false + return React.createElement(originalComponent, props) + } + } + } +} + export const renderSwaggerUI = async (container: HTMLElement, apiDocsPath: string, baseUrl: string, accountDataUrl: string): Promise => { const apiSpecs: ApiDocsServices = await fetchData(apiDocsPath) @@ -134,7 +148,7 @@ export const renderSwaggerUI = async (container: HTMLElement, apiDocsPath: strin requestInterceptor: (request) => autocompleteRequestInterceptor(request, accountData, ''), tryItOutEnabled: true, plugins: [ - RequestBodyTransformerPlugin + RequestBodyTransformerPlugin, UncheckSendEmptyValuePlugin ] }) }) diff --git a/app/javascript/src/Types/swagger.d.ts b/app/javascript/src/Types/swagger.d.ts index 94677ce9be..e6e7ffe07e 100644 --- a/app/javascript/src/Types/swagger.d.ts +++ b/app/javascript/src/Types/swagger.d.ts @@ -25,3 +25,22 @@ declare module 'swagger-client/es/execute' { function execute (req: ExecuteData): unknown export { execute } } + +declare module 'swagger-ui-utils' { + import type { Component } from 'react' + + export interface ReactType { + createElement: (Original: Component, props: ParameterIncludeEmptyProperties) => Component; + } + + export interface SwaggerUIContext { + // eslint-disable-next-line @typescript-eslint/naming-convention + React: ReactType; + } + + export interface ParameterIncludeEmptyProperties { + isIncludedOptions: { + defaultValue: boolean; + }; + } +}