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

Add option for skipping enum quotes #968

Merged
merged 2 commits into from
Jul 15, 2019
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
16 changes: 11 additions & 5 deletions src/components/Fields/EnumValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import * as React from 'react';
import { ExampleValue, FieldLabel } from '../../common-elements/fields';

import { l } from '../../services/Labels';
import { OptionsContext } from '../OptionsProvider';

export interface EnumValuesProps {
values: string[];
type: string;
}

export class EnumValues extends React.PureComponent<EnumValuesProps> {
static contextType = OptionsContext;
render() {
const { values, type } = this.props;
const { enumSkipQuotes } = this.context;
if (!values.length) {
return null;
}
Expand All @@ -21,11 +24,14 @@ export class EnumValues extends React.PureComponent<EnumValuesProps> {
{type === 'array' ? l('enumArray') : ''}{' '}
{values.length === 1 ? l('enumSingleValue') : l('enum')}:
</FieldLabel>
{values.map((value, idx) => (
<React.Fragment key={idx}>
<ExampleValue>{JSON.stringify(value)}</ExampleValue>{' '}
</React.Fragment>
))}
{values.map((value, idx) => {
const exampleValue = enumSkipQuotes ? value : JSON.stringify(value);
return (
<React.Fragment key={idx}>
<ExampleValue>{exampleValue}</ExampleValue>
</React.Fragment>
);
})}
</div>
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Fields/FieldDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import { FieldDetail } from './FieldDetail';
import { Badge } from '../../common-elements/';

import { l } from '../../services/Labels';
import { OptionsContext } from '../OptionsProvider';

export class FieldDetails extends React.PureComponent<FieldProps> {
static contextType = OptionsContext;
render() {
const { showExamples, field, renderDiscriminatorSwitch } = this.props;
const { enumSkipQuotes } = this.context;

const { schema, description, example, deprecated } = field;

Expand Down Expand Up @@ -65,7 +68,7 @@ export class FieldDetails extends React.PureComponent<FieldProps> {
<Badge type="warning"> {l('deprecated')} </Badge>
</div>
)}
<FieldDetail label={l('default') + ':'} value={schema.default} />
<FieldDetail raw={enumSkipQuotes} label={l('default') + ':'} value={schema.default} />
{!renderDiscriminatorSwitch && <EnumValues type={schema.type} values={schema.enum} />}{' '}
{exampleField}
{<Extensions extensions={{ ...field.extensions, ...schema.extensions }} />}
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface RedocRawOptions {
allowedMdComponents?: Dict<MDXComponentMeta>;

labels?: LabelsConfigRaw;
enumSkipQuotes?: boolean | string;
}

function argValueToBoolean(val?: string | boolean): boolean {
Expand Down Expand Up @@ -125,6 +126,7 @@ export class RedocNormalizedOptions {
onlyRequiredInSamples: boolean;
showExtensions: boolean | string[];
hideSingleRequestSampleTab: boolean;
enumSkipQuotes: boolean;

/* tslint:disable-next-line */
unstable_ignoreMimeParameters: boolean;
Expand Down Expand Up @@ -156,6 +158,7 @@ export class RedocNormalizedOptions {
this.onlyRequiredInSamples = argValueToBoolean(raw.onlyRequiredInSamples);
this.showExtensions = RedocNormalizedOptions.normalizeShowExtensions(raw.showExtensions);
this.hideSingleRequestSampleTab = argValueToBoolean(raw.hideSingleRequestSampleTab);
this.enumSkipQuotes = argValueToBoolean(raw.enumSkipQuotes);

this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

Expand Down