feat: add new experimental option unstable_ignoreMimeParameters

This commit is contained in:
Roman Hotsiy 2018-05-29 17:41:50 +03:00
parent 720c304484
commit d162babe19
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 36 additions and 3 deletions

View File

@ -14,6 +14,8 @@ export interface RedocRawOptions {
untrustedSpec?: boolean | string; untrustedSpec?: boolean | string;
hideLoading?: boolean | string; hideLoading?: boolean | string;
hideDownloadButton?: boolean | string; hideDownloadButton?: boolean | string;
unstable_ignoreMimeParameters?: boolean;
} }
function argValueToBoolean(val?: string | boolean): boolean { function argValueToBoolean(val?: string | boolean): boolean {
@ -92,6 +94,9 @@ export class RedocNormalizedOptions {
untrustedSpec: boolean; untrustedSpec: boolean;
hideDownloadButton: boolean; hideDownloadButton: boolean;
/* tslint:disable-next-line */
unstable_ignoreMimeParameters: boolean;
constructor(raw: RedocRawOptions) { constructor(raw: RedocRawOptions) {
this.theme = resolveTheme(mergeObjects({} as any, defaultTheme, raw.theme || {})); this.theme = resolveTheme(mergeObjects({} as any, defaultTheme, raw.theme || {}));
this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset); this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
@ -103,5 +108,7 @@ export class RedocNormalizedOptions {
this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel); this.pathInMiddlePanel = argValueToBoolean(raw.pathInMiddlePanel);
this.untrustedSpec = argValueToBoolean(raw.untrustedSpec); this.untrustedSpec = argValueToBoolean(raw.untrustedSpec);
this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton); this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton);
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);
} }
} }

View File

@ -3,12 +3,13 @@ import { action, computed, observable } from 'mobx';
import { OpenAPIMediaType } from '../../types'; import { OpenAPIMediaType } from '../../types';
import { MediaTypeModel } from './MediaType'; import { MediaTypeModel } from './MediaType';
import { mergeSimilarMediaTypes } from '../../utils';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
/** /**
* MediaContent model ready to be sued by React components * MediaContent model ready to be sued by React components
* Contains multiple MediaTypes and keeps track of the currently active on * Contains multiple MediaTypes and keeps track of the currently active one
*/ */
export class MediaContentModel { export class MediaContentModel {
mediaTypes: MediaTypeModel[]; mediaTypes: MediaTypeModel[];
@ -20,10 +21,13 @@ export class MediaContentModel {
*/ */
constructor( constructor(
public parser: OpenAPIParser, public parser: OpenAPIParser,
info: { [mime: string]: OpenAPIMediaType }, info: Dict<OpenAPIMediaType>,
public isRequestType: boolean, public isRequestType: boolean,
options: RedocNormalizedOptions, options: RedocNormalizedOptions,
) { ) {
if (options.unstable_ignoreMimeParameters) {
info = mergeSimilarMediaTypes(info);
}
this.mediaTypes = Object.keys(info).map(name => { this.mediaTypes = Object.keys(info).map(name => {
const mime = info[name]; const mime = info[name];
// reset deref cache just in case something is left there // reset deref cache just in case something is left there

View File

@ -1,5 +1,11 @@
import { OpenAPIParser } from '../services/OpenAPIParser'; import { OpenAPIParser } from '../services/OpenAPIParser';
import { OpenAPIOperation, OpenAPIParameter, OpenAPISchema, Referenced } from '../types'; import {
OpenAPIMediaType,
OpenAPIOperation,
OpenAPIParameter,
OpenAPISchema,
Referenced,
} from '../types';
export function getStatusCodeType(statusCode: string | number, defaultAsError = false): string { export function getStatusCodeType(statusCode: string | number, defaultAsError = false): string {
if (statusCode === 'default') { if (statusCode === 'default') {
@ -199,4 +205,20 @@ export function mergeParams(
return pathParams.concat(operationParams); return pathParams.concat(operationParams);
} }
export function mergeSimilarMediaTypes(types: Dict<OpenAPIMediaType>): Dict<OpenAPIMediaType> {
const mergedTypes = {};
Object.keys(types).forEach(name => {
const mime = types[name];
// ignore content type parameters (e.g. charset) and merge
const normalizedMimeName = name.split(';')[0].trim();
if (!mergedTypes[normalizedMimeName]) {
mergedTypes[normalizedMimeName] = mime;
return;
}
mergedTypes[normalizedMimeName] = { ...mergedTypes[normalizedMimeName], ...mime };
});
return mergedTypes;
}
export const SECURITY_SCHEMES_SECTION = 'section/Authentication/'; export const SECURITY_SCHEMES_SECTION = 'section/Authentication/';