diff --git a/demo/openapi.yaml b/demo/openapi.yaml index 74c16b70..539e157d 100644 --- a/demo/openapi.yaml +++ b/demo/openapi.yaml @@ -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 @@ -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: @@ -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 \ No newline at end of file + description: Return a 200 status to indicate that the data was received successfully diff --git a/demo/playground/hmr-playground.tsx b/demo/playground/hmr-playground.tsx index 737c3db6..39b31895 100644 --- a/demo/playground/hmr-playground.tsx +++ b/demo/playground/hmr-playground.tsx @@ -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); diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 58d4b8b0..fe59a828 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -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; 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); } } diff --git a/src/services/models/MediaType.ts b/src/services/models/MediaType.ts index 7807c358..0f939123 100644 --- a/src/services/models/MediaType.ts +++ b/src/services/models/MediaType.ts @@ -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, ),