disableDefaultSample option

This commit is contained in:
Oleksandr 2021-02-14 02:03:22 +02:00
parent 755a9095f5
commit e5c6221afe
4 changed files with 21 additions and 9 deletions

View File

@ -505,7 +505,6 @@ paths:
type: string
format: uri
description: This URL will be called by the server when the desired event will occur
example: https://myserver.com/send/callback/here
eventName:
type: string
description: Event name for the subscription

View File

@ -26,7 +26,7 @@ const specUrl =
(userUrl && userUrl[1]) || (swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'openapi.yaml');
let store;
const options: RedocRawOptions = { nativeScrollbars: false, maxDisplayedEnumValues: 3 };
const options: RedocRawOptions = { nativeScrollbars: false, maxDisplayedEnumValues: 3, disableDefaultSample: true };
async function init() {
const spec = await loadAndBundleSpec(specUrl);

View File

@ -42,6 +42,7 @@ export interface RedocRawOptions {
maxDisplayedEnumValues?: number;
ignoreNamedSchemas?: string[] | string;
hideSchemaPattern?: boolean;
disableDefaultSample?: boolean;
}
function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
@ -196,6 +197,7 @@ export class RedocNormalizedOptions {
ignoreNamedSchemas: Set<string>;
hideSchemaPattern: boolean;
disableDefaultSample: boolean;
constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
raw = { ...defaults, ...raw };
@ -257,5 +259,6 @@ export class RedocNormalizedOptions {
: raw.ignoreNamedSchemas?.split(',').map((s) => s.trim());
this.ignoreNamedSchemas = new Set(ignoreNamedSchemas);
this.hideSchemaPattern = argValueToBoolean(raw.hideSchemaPattern);
this.disableDefaultSample = argValueToBoolean(raw.disableDefaultSample);
}
}

View File

@ -44,11 +44,11 @@ export class MediaTypeModel {
),
};
} else if (isJsonLike(name)) {
this.generateExample(parser, info);
this.generateExample(parser, info, options.disableDefaultSample);
}
}
generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) {
generateExample(parser: OpenAPIParser, info: OpenAPIMediaType, disableDefaultSample: boolean) {
const samplerOptions = {
skipReadOnly: this.isRequestType,
skipNonRequired: this.isRequestType && this.onlyRequiredInSamples,
@ -74,12 +74,22 @@ export class MediaTypeModel {
);
}
} else if (this.schema) {
const sampledData = Sampler.sample(info.schema, samplerOptions, parser.spec);
if (disableDefaultSample && info.schema) {
const properties = parser.deref(info.schema)?.properties || {};
Object.keys(properties).map((propName) => {
const property = properties[propName];
if (!property.example && sampledData[propName] !== undefined) {
delete sampledData[propName];
}
});
}
this.examples = {
default: new ExampleModel(
parser,
{
value: Sampler.sample(info.schema, samplerOptions, parser.spec),
},
{ value: sampledData },
this.name,
info.encoding,
),