Skip to content

Commit

Permalink
Merge e5c6221 into 755a909
Browse files Browse the repository at this point in the history
  • Loading branch information
lysenko86 committed Feb 14, 2021
2 parents 755a909 + e5c6221 commit 4740445
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 2 additions & 3 deletions demo/openapi.yaml
Expand Up @@ -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
Expand Down Expand Up @@ -1193,7 +1192,7 @@ x-webhooks:
summary: New pet
description: Information about a new pet in the systems
operationId: newPet
tags:
tags:
- pet
requestBody:
content:
Expand All @@ -1202,4 +1201,4 @@ x-webhooks:
$ref: "#/components/schemas/Pet"
responses:
"200":
description: Return a 200 status to indicate that the data was received successfully
description: Return a 200 status to indicate that the data was received successfully
2 changes: 1 addition & 1 deletion demo/playground/hmr-playground.tsx
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Expand Up @@ -42,6 +42,7 @@ export interface RedocRawOptions {
maxDisplayedEnumValues?: number;
ignoreNamedSchemas?: string[] | string;
hideSchemaPattern?: boolean;
disableDefaultSample?: boolean;
}

function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
Expand Down Expand Up @@ -196,6 +197,7 @@ export class RedocNormalizedOptions {

ignoreNamedSchemas: Set<string>;
hideSchemaPattern: boolean;
disableDefaultSample: boolean;

constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
raw = { ...defaults, ...raw };
Expand Down Expand Up @@ -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);
}
}
20 changes: 15 additions & 5 deletions src/services/models/MediaType.ts
Expand Up @@ -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,
Expand All @@ -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,
),
Expand Down

0 comments on commit 4740445

Please sign in to comment.