From 0360dcee5aca970979a2c13b1f41bc6323013199 Mon Sep 17 00:00:00 2001 From: Antherkiv Date: Mon, 30 Sep 2019 02:39:19 -0500 Subject: [PATCH] feat: new option expandDefaultServerVariables (#1014) Recover server expanding default variables This reverts commit 7849f7f6b7517275f2b283085ac8174f83dc055d. --- src/components/Endpoint/Endpoint.tsx | 11 ++++++--- src/components/Schema/Schema.tsx | 8 +++--- src/services/RedocNormalizedOptions.ts | 7 ++++++ src/utils/__tests__/openapi.test.ts | 34 ++++++++++++++++++++++++++ src/utils/openapi.ts | 7 ++++++ 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/components/Endpoint/Endpoint.tsx b/src/components/Endpoint/Endpoint.tsx index 21bffa52..280a8582 100644 --- a/src/components/Endpoint/Endpoint.tsx +++ b/src/components/Endpoint/Endpoint.tsx @@ -5,7 +5,7 @@ import { Markdown } from '../Markdown/Markdown'; import { OptionsContext } from '../OptionsProvider'; import { SelectOnClick } from '../SelectOnClick/SelectOnClick'; -import { getBasePath } from '../../utils'; +import { expandDefaultServerVariables, getBasePath } from '../../utils'; import { EndpointInfo, HttpVerb, @@ -61,15 +61,18 @@ export class Endpoint extends React.Component { {operation.servers.map(server => { + const normalizedUrl = options.expandDefaultServerVariables + ? expandDefaultServerVariables(server.url, server.variables) + : server.url; return ( - + {hideHostname || options.hideHostname - ? getBasePath(server.url) - : server.url} + ? getBasePath(normalizedUrl) + : normalizedUrl} {operation.path} diff --git a/src/components/Schema/Schema.tsx b/src/components/Schema/Schema.tsx index f71c33a2..2a533190 100644 --- a/src/components/Schema/Schema.tsx +++ b/src/components/Schema/Schema.tsx @@ -44,9 +44,7 @@ export class Schema extends React.Component> { if (discriminatorProp !== undefined) { if (!oneOf || !oneOf.length) { throw new Error( - `Looks like you are using discriminator wrong: you don't have any definition inherited from the ${ - schema.title - }`, + `Looks like you are using discriminator wrong: you don't have any definition inherited from the ${schema.title}`, ); } return ( @@ -66,9 +64,9 @@ export class Schema extends React.Component> { switch (type) { case 'object': - return ; + return ; case 'array': - return ; + return ; } // TODO: maybe adjust FieldDetails to accept schema diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 1bcffb27..67fcd808 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -30,7 +30,10 @@ export interface RedocRawOptions { allowedMdComponents?: Dict; labels?: LabelsConfigRaw; + enumSkipQuotes?: boolean | string; + + expandDefaultServerVariables?: boolean; } function argValueToBoolean(val?: string | boolean): boolean { @@ -146,6 +149,8 @@ export class RedocNormalizedOptions { unstable_ignoreMimeParameters: boolean; allowedMdComponents: Dict; + expandDefaultServerVariables: boolean; + constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) { raw = { ...defaults, ...raw }; const hook = raw.theme && raw.theme.extensionsHook; @@ -181,5 +186,7 @@ export class RedocNormalizedOptions { this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters); this.allowedMdComponents = raw.allowedMdComponents || {}; + + this.expandDefaultServerVariables = argValueToBoolean(raw.expandDefaultServerVariables); } } diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index 37d5680c..92057583 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -13,6 +13,7 @@ import { import { FieldModel, OpenAPIParser, RedocNormalizedOptions } from '../../services'; import { OpenAPIParameter, OpenAPIParameterLocation, OpenAPIParameterStyle } from '../../types'; +import { expandDefaultServerVariables } from '../openapi'; describe('Utils', () => { describe('openapi getStatusCode', () => { @@ -293,6 +294,39 @@ describe('Utils', () => { ]); expect(res).toEqual([{ url: 'https://base.com/sandbox/test', description: 'test' }]); }); + + it('should expand variables', () => { + const servers = normalizeServers('', [ + { + url: 'http://{host}{basePath}', + variables: { + host: { + default: '127.0.0.1', + }, + basePath: { + default: '/path/to/endpoint', + }, + }, + }, + { + url: 'http://127.0.0.2:{port}', + variables: {}, + }, + { + url: 'http://127.0.0.3', + }, + ]); + + expect(expandDefaultServerVariables(servers[0].url, servers[0].variables)).toEqual( + 'http://127.0.0.1/path/to/endpoint', + ); + expect(expandDefaultServerVariables(servers[1].url, servers[1].variables)).toEqual( + 'http://127.0.0.2:{port}', + ); + expect(expandDefaultServerVariables(servers[2].url, servers[2].variables)).toEqual( + 'http://127.0.0.3', + ); + }); }); describe('openapi humanizeConstraints', () => { diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 0b928257..7eefb30d 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -487,6 +487,13 @@ export function mergeSimilarMediaTypes(types: Dict): Dict (variables[name] && variables[name].default) || match, + ); +} + export function normalizeServers( specUrl: string | undefined, servers: OpenAPIServer[],