Skip to content

Commit

Permalink
feat: new option simpleOneOfTypeLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Aug 4, 2020
1 parent 2ce7189 commit 7af2efe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -180,6 +181,7 @@ export class RedocNormalizedOptions {
jsonSampleExpandLevel: number;
enumSkipQuotes: boolean;
hideSchemaTitles: boolean;
simpleOneOfTypeLabel: boolean;
payloadSampleIdx: number;
expandSingleSchemaField: boolean;

Expand Down Expand Up @@ -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);

Expand Down
51 changes: 38 additions & 13 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}

0 comments on commit 7af2efe

Please sign in to comment.