From f4f02e97aaf79952119bb4f569c6bf74caa692ab Mon Sep 17 00:00:00 2001 From: exoego Date: Sun, 16 Sep 2018 10:21:13 +0900 Subject: [PATCH 1/2] Add onlyRequiredInSamples option that let user to show only required fields in Request samples. --- README.md | 3 ++- src/services/RedocNormalizedOptions.ts | 3 +++ src/services/models/MediaType.ts | 11 +++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 98d1c39a69..2804c8e572 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ You can use all of the following options with standalone version on tag * `nativeScrollbars` - use native scrollbar for sidemenu instead of perfect-scroll (scrolling performance optimization for big specs) * `hideDownloadButton` - do not show "Download" spec button. **THIS DOESN'T MAKE YOUR SPEC PRIVATE**, it just hides the button. * `disableSearch` - disable search indexing and search box +* `onlyRequiredInSamples` - shows only required fields in request samples. * `theme` - ReDoc theme. Not documented yet. For details check source code: [theme.ts](https://github.com/Rebilly/ReDoc/blob/master/src/theme.ts) ## Advanced usage of standalone version @@ -242,4 +243,4 @@ Redoc.init('http://petstore.swagger.io/v2/swagger.json', { ----------- ## Development -see [CONTRIBUTING.md](.github/CONTRIBUTING.md) \ No newline at end of file +see [CONTRIBUTING.md](.github/CONTRIBUTING.md) diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 851d4b1b11..df26333094 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -17,6 +17,7 @@ export interface RedocRawOptions { hideLoading?: boolean | string; hideDownloadButton?: boolean | string; disableSearch?: boolean | string; + onlyRequiredInSamples?: boolean | string; unstable_ignoreMimeParameters?: boolean; @@ -99,6 +100,7 @@ export class RedocNormalizedOptions { untrustedSpec: boolean; hideDownloadButton: boolean; disableSearch: boolean; + onlyRequiredInSamples: boolean; /* tslint:disable-next-line */ unstable_ignoreMimeParameters: boolean; @@ -124,6 +126,7 @@ export class RedocNormalizedOptions { this.untrustedSpec = argValueToBoolean(raw.untrustedSpec); this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton); this.disableSearch = argValueToBoolean(raw.disableSearch); + this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples); this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters); diff --git a/src/services/models/MediaType.ts b/src/services/models/MediaType.ts index ff471e2715..90fc5f216e 100644 --- a/src/services/models/MediaType.ts +++ b/src/services/models/MediaType.ts @@ -13,6 +13,7 @@ export class MediaTypeModel { schema?: SchemaModel; name: string; isRequestType: boolean; + onlyRequiredInSamples: boolean; /** * @param isRequestType needed to know if skipe RO/RW fields in objects @@ -27,6 +28,7 @@ export class MediaTypeModel { this.name = name; this.isRequestType = isRequestType; this.schema = info.schema && new SchemaModel(parser, info.schema, '', options); + this.onlyRequiredInSamples = options.onlyRequiredInSamples; if (info.examples !== undefined) { this.examples = mapValues(info.examples, example => new ExampleModel(parser, example)); } else if (info.example !== undefined) { @@ -39,12 +41,17 @@ export class MediaTypeModel { } generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) { + const samplerOption = { + skipReadOnly: this.isRequestType, + skipNonRequired: this.isRequestType && this.onlyRequiredInSamples, + skipWriteOnly: !this.isRequestType, + }; if (this.schema && this.schema.oneOf) { this.examples = {}; for (const subSchema of this.schema.oneOf) { const sample = Sampler.sample( subSchema.rawSchema, - { skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType }, + samplerOption, parser.spec, ); @@ -61,7 +68,7 @@ export class MediaTypeModel { default: new ExampleModel(parser, { value: Sampler.sample( info.schema, - { skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType }, + samplerOption, parser.spec, ), }), From 57e0d109608b276330f63bfe44fc61cbdb92bd05 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Fri, 5 Oct 2018 16:08:04 +0300 Subject: [PATCH 2/2] rename samplerOption to samplerOptions --- src/services/models/MediaType.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/models/MediaType.ts b/src/services/models/MediaType.ts index 90fc5f216e..941a35fbe7 100644 --- a/src/services/models/MediaType.ts +++ b/src/services/models/MediaType.ts @@ -41,7 +41,7 @@ export class MediaTypeModel { } generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) { - const samplerOption = { + const samplerOptions = { skipReadOnly: this.isRequestType, skipNonRequired: this.isRequestType && this.onlyRequiredInSamples, skipWriteOnly: !this.isRequestType, @@ -51,7 +51,7 @@ export class MediaTypeModel { for (const subSchema of this.schema.oneOf) { const sample = Sampler.sample( subSchema.rawSchema, - samplerOption, + samplerOptions, parser.spec, ); @@ -68,7 +68,7 @@ export class MediaTypeModel { default: new ExampleModel(parser, { value: Sampler.sample( info.schema, - samplerOption, + samplerOptions, parser.spec, ), }),