feat: new option simpleOneOfTypeLabel

This commit is contained in:
Roman Hotsiy 2020-08-04 12:19:49 +03:00
parent 2ce71895bc
commit 7af2efe731
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 43 additions and 13 deletions

View File

@ -232,6 +232,8 @@ You can use all of the following options with standalone version on <redoc> tag
* `hideSingleRequestSampleTab` - do not show the request sample tab for requests with only one sample.
* `expandSingleSchemaField` - automatically expand single field in a schema
* `jsonSampleExpandLevel` - set the default expand level for JSON payload samples (responses and request body). Special value 'all' expands all levels. The default value is `2`.
* `hideSchemaTitles` - do not display schema `title` next to to the type
* `simpleOneOfTypeLabel` - show only unique oneOf types in the label without titles
* `lazyRendering` - _Not implemented yet_ ~~if set, enables lazy rendering mode in ReDoc. This mode is useful for APIs with big number of operations (e.g. > 50). In this mode ReDoc shows initial screen ASAP and then renders the rest operations asynchronously while showing progress bar on the top. Check out the [demo](\\redocly.github.io/redoc) for the example.~~
* `menuToggle` - if true clicking second time on expanded menu item will collapse it, default `false`.
* `nativeScrollbars` - use native scrollbar for sidemenu instead of perfect-scroll (scrolling performance optimization for big specs).

View File

@ -26,6 +26,7 @@ export interface RedocRawOptions {
menuToggle?: boolean | string;
jsonSampleExpandLevel?: number | string | 'all';
hideSchemaTitles?: boolean | string;
simpleOneOfTypeLabel?: boolean | string;
payloadSampleIdx?: number;
expandSingleSchemaField?: boolean | string;
@ -180,6 +181,7 @@ export class RedocNormalizedOptions {
jsonSampleExpandLevel: number;
enumSkipQuotes: boolean;
hideSchemaTitles: boolean;
simpleOneOfTypeLabel: boolean;
payloadSampleIdx: number;
expandSingleSchemaField: boolean;
@ -235,6 +237,7 @@ export class RedocNormalizedOptions {
);
this.enumSkipQuotes = argValueToBoolean(raw.enumSkipQuotes);
this.hideSchemaTitles = argValueToBoolean(raw.hideSchemaTitles);
this.simpleOneOfTypeLabel = argValueToBoolean(raw.simpleOneOfTypeLabel);
this.payloadSampleIdx = RedocNormalizedOptions.normalizePayloadSampleIdx(raw.payloadSampleIdx);
this.expandSingleSchemaField = argValueToBoolean(raw.expandSingleSchemaField);

View File

@ -129,7 +129,7 @@ export class SchemaModel {
} else if (
isChild &&
Array.isArray(schema.oneOf) &&
schema.oneOf.find(s => s.$ref === this.pointer)
schema.oneOf.find((s) => s.$ref === this.pointer)
) {
// we hit allOf of the schema with the parent discriminator
delete schema.oneOf;
@ -207,17 +207,22 @@ export class SchemaModel {
return schema;
});
this.displayType = this.oneOf
.map(schema => {
let name =
schema.typePrefix +
(schema.title ? `${schema.title} (${schema.displayType})` : schema.displayType);
if (name.indexOf(' or ') > -1) {
name = `(${name})`;
}
return name;
})
.join(' or ');
if (this.options.simpleOneOfTypeLabel) {
const types = collectUniqueOneOfTypesDeep(this);
this.displayType = types.join(' or ');
} else {
this.displayType = this.oneOf
.map((schema) => {
let name =
schema.typePrefix +
(schema.title ? `${schema.title} (${schema.displayType})` : schema.displayType);
if (name.indexOf(' or ') > -1) {
name = `(${name})`;
}
return name;
})
.join(' or ');
}
}
private initDiscriminator(
@ -328,7 +333,7 @@ function buildFields(
const props = schema.properties || {};
const additionalProps = schema.additionalProperties;
const defaults = schema.default || {};
let fields = Object.keys(props || []).map(fieldName => {
let fields = Object.keys(props || []).map((fieldName) => {
let field = props[fieldName];
if (!field) {
@ -389,3 +394,23 @@ function buildFields(
function getDiscriminator(schema: OpenAPISchema): OpenAPISchema['discriminator'] {
return schema.discriminator || schema['x-discriminator'];
}
function collectUniqueOneOfTypesDeep(schema: SchemaModel) {
const uniqueTypes = new Set();
function crawl(schema: SchemaModel) {
for (const oneOfType of schema.oneOf || []) {
if (oneOfType.oneOf) {
crawl(oneOfType);
continue;
}
if (oneOfType.type) {
uniqueTypes.add(oneOfType.type);
}
}
}
crawl(schema);
return Array.from(uniqueTypes.values());
}