Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #424: Add onlyRequiredInSamples option #646

Merged
merged 3 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,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)
* `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
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface RedocRawOptions {
hideLoading?: boolean | string;
hideDownloadButton?: boolean | string;
disableSearch?: boolean | string;
onlyRequiredInSamples?: boolean | string;
showExtensions?: boolean | string | string[];

unstable_ignoreMimeParameters?: boolean;
Expand Down Expand Up @@ -117,6 +118,7 @@ export class RedocNormalizedOptions {
untrustedSpec: boolean;
hideDownloadButton: boolean;
disableSearch: boolean;
onlyRequiredInSamples: boolean;
showExtensions: boolean | string[];

/* tslint:disable-next-line */
Expand Down Expand Up @@ -144,6 +146,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.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);

this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);
Expand Down
11 changes: 9 additions & 2 deletions src/services/models/MediaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -39,12 +41,17 @@ export class MediaTypeModel {
}

generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) {
const samplerOptions = {
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 },
samplerOptions,
parser.spec,
);

Expand All @@ -61,7 +68,7 @@ export class MediaTypeModel {
default: new ExampleModel(parser, {
value: Sampler.sample(
info.schema,
{ skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType },
samplerOptions,
parser.spec,
),
}),
Expand Down