Add onlyRequiredInSamples option that let user to show only required fields in Request samples.

This commit is contained in:
exoego 2018-09-16 10:21:13 +09:00
parent ab98ab0cb2
commit f4f02e97aa
3 changed files with 14 additions and 3 deletions

View File

@ -221,6 +221,7 @@ You can use all of the following options with standalone version on <redoc> tag
* `nativeScrollbars` - use native scrollbar for sidemenu instead of perfect-scroll (scrolling performance optimization for big specs) * `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. * `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 * `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) * `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 ## Advanced usage of standalone version
@ -242,4 +243,4 @@ Redoc.init('http://petstore.swagger.io/v2/swagger.json', {
----------- -----------
## Development ## Development
see [CONTRIBUTING.md](.github/CONTRIBUTING.md) see [CONTRIBUTING.md](.github/CONTRIBUTING.md)

View File

@ -17,6 +17,7 @@ export interface RedocRawOptions {
hideLoading?: boolean | string; hideLoading?: boolean | string;
hideDownloadButton?: boolean | string; hideDownloadButton?: boolean | string;
disableSearch?: boolean | string; disableSearch?: boolean | string;
onlyRequiredInSamples?: boolean | string;
unstable_ignoreMimeParameters?: boolean; unstable_ignoreMimeParameters?: boolean;
@ -99,6 +100,7 @@ export class RedocNormalizedOptions {
untrustedSpec: boolean; untrustedSpec: boolean;
hideDownloadButton: boolean; hideDownloadButton: boolean;
disableSearch: boolean; disableSearch: boolean;
onlyRequiredInSamples: boolean;
/* tslint:disable-next-line */ /* tslint:disable-next-line */
unstable_ignoreMimeParameters: boolean; unstable_ignoreMimeParameters: boolean;
@ -124,6 +126,7 @@ export class RedocNormalizedOptions {
this.untrustedSpec = argValueToBoolean(raw.untrustedSpec); this.untrustedSpec = argValueToBoolean(raw.untrustedSpec);
this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton); this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton);
this.disableSearch = argValueToBoolean(raw.disableSearch); this.disableSearch = argValueToBoolean(raw.disableSearch);
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters); this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

View File

@ -13,6 +13,7 @@ export class MediaTypeModel {
schema?: SchemaModel; schema?: SchemaModel;
name: string; name: string;
isRequestType: boolean; isRequestType: boolean;
onlyRequiredInSamples: boolean;
/** /**
* @param isRequestType needed to know if skipe RO/RW fields in objects * @param isRequestType needed to know if skipe RO/RW fields in objects
@ -27,6 +28,7 @@ export class MediaTypeModel {
this.name = name; this.name = name;
this.isRequestType = isRequestType; this.isRequestType = isRequestType;
this.schema = info.schema && new SchemaModel(parser, info.schema, '', options); this.schema = info.schema && new SchemaModel(parser, info.schema, '', options);
this.onlyRequiredInSamples = options.onlyRequiredInSamples;
if (info.examples !== undefined) { if (info.examples !== undefined) {
this.examples = mapValues(info.examples, example => new ExampleModel(parser, example)); this.examples = mapValues(info.examples, example => new ExampleModel(parser, example));
} else if (info.example !== undefined) { } else if (info.example !== undefined) {
@ -39,12 +41,17 @@ export class MediaTypeModel {
} }
generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) { generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) {
const samplerOption = {
skipReadOnly: this.isRequestType,
skipNonRequired: this.isRequestType && this.onlyRequiredInSamples,
skipWriteOnly: !this.isRequestType,
};
if (this.schema && this.schema.oneOf) { if (this.schema && this.schema.oneOf) {
this.examples = {}; this.examples = {};
for (const subSchema of this.schema.oneOf) { for (const subSchema of this.schema.oneOf) {
const sample = Sampler.sample( const sample = Sampler.sample(
subSchema.rawSchema, subSchema.rawSchema,
{ skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType }, samplerOption,
parser.spec, parser.spec,
); );
@ -61,7 +68,7 @@ export class MediaTypeModel {
default: new ExampleModel(parser, { default: new ExampleModel(parser, {
value: Sampler.sample( value: Sampler.sample(
info.schema, info.schema,
{ skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType }, samplerOption,
parser.spec, parser.spec,
), ),
}), }),