Skip to content

Commit 10bca66

Browse files
TATSUNO YasuhiroRomanHotsiy
TATSUNO Yasuhiro
authored andcommitted
feat: New option onlyRequiredInSamples (#646)
* Add onlyRequiredInSamples option that let user to show only required fields in Request samples.
1 parent fbcec82 commit 10bca66

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ You can use all of the following options with standalone version on <redoc> tag
227227
* `nativeScrollbars` - use native scrollbar for sidemenu instead of perfect-scroll (scrolling performance optimization for big specs)
228228
* `hideDownloadButton` - do not show "Download" spec button. **THIS DOESN'T MAKE YOUR SPEC PRIVATE**, it just hides the button.
229229
* `disableSearch` - disable search indexing and search box
230+
* `onlyRequiredInSamples` - shows only required fields in request samples.
230231
* `theme` - ReDoc theme. Not documented yet. For details check source code: [theme.ts](https://github.com/Rebilly/ReDoc/blob/master/src/theme.ts)
231232

232233
## Advanced usage of standalone version

src/services/RedocNormalizedOptions.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface RedocRawOptions {
1818
hideLoading?: boolean | string;
1919
hideDownloadButton?: boolean | string;
2020
disableSearch?: boolean | string;
21+
onlyRequiredInSamples?: boolean | string;
2122
showExtensions?: boolean | string | string[];
2223

2324
unstable_ignoreMimeParameters?: boolean;
@@ -117,6 +118,7 @@ export class RedocNormalizedOptions {
117118
untrustedSpec: boolean;
118119
hideDownloadButton: boolean;
119120
disableSearch: boolean;
121+
onlyRequiredInSamples: boolean;
120122
showExtensions: boolean | string[];
121123

122124
/* tslint:disable-next-line */
@@ -144,6 +146,7 @@ export class RedocNormalizedOptions {
144146
this.untrustedSpec = argValueToBoolean(raw.untrustedSpec);
145147
this.hideDownloadButton = argValueToBoolean(raw.hideDownloadButton);
146148
this.disableSearch = argValueToBoolean(raw.disableSearch);
149+
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
147150
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
148151

149152
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

src/services/models/MediaType.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class MediaTypeModel {
1313
schema?: SchemaModel;
1414
name: string;
1515
isRequestType: boolean;
16+
onlyRequiredInSamples: boolean;
1617

1718
/**
1819
* @param isRequestType needed to know if skipe RO/RW fields in objects
@@ -27,6 +28,7 @@ export class MediaTypeModel {
2728
this.name = name;
2829
this.isRequestType = isRequestType;
2930
this.schema = info.schema && new SchemaModel(parser, info.schema, '', options);
31+
this.onlyRequiredInSamples = options.onlyRequiredInSamples;
3032
if (info.examples !== undefined) {
3133
this.examples = mapValues(info.examples, example => new ExampleModel(parser, example));
3234
} else if (info.example !== undefined) {
@@ -39,12 +41,17 @@ export class MediaTypeModel {
3941
}
4042

4143
generateExample(parser: OpenAPIParser, info: OpenAPIMediaType) {
44+
const samplerOptions = {
45+
skipReadOnly: this.isRequestType,
46+
skipNonRequired: this.isRequestType && this.onlyRequiredInSamples,
47+
skipWriteOnly: !this.isRequestType,
48+
};
4249
if (this.schema && this.schema.oneOf) {
4350
this.examples = {};
4451
for (const subSchema of this.schema.oneOf) {
4552
const sample = Sampler.sample(
4653
subSchema.rawSchema,
47-
{ skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType },
54+
samplerOptions,
4855
parser.spec,
4956
);
5057

@@ -61,7 +68,7 @@ export class MediaTypeModel {
6168
default: new ExampleModel(parser, {
6269
value: Sampler.sample(
6370
info.schema,
64-
{ skipReadOnly: this.isRequestType, skipWriteOnly: !this.isRequestType },
71+
samplerOptions,
6572
parser.spec,
6673
),
6774
}),

0 commit comments

Comments
 (0)