Skip to content

Commit

Permalink
feat: reqired-first sort order for params
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 5, 2018
1 parent 8bd71de commit ecf33d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
10 changes: 8 additions & 2 deletions src/components/SideMenu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ export class MenuItem extends React.Component<MenuItemProps> {

export interface OperationMenuItemContentProps {
item: OperationModel;
className?: string;
}

@observer
class OperationMenuItemContent extends React.Component<OperationMenuItemContentProps> {
render() {
const { item } = this.props;
const { item, className } = this.props;
return (
<MenuItemLabel depth={item.depth} active={item.active} deprecated={item.deprecated}>
<MenuItemLabel
className={className}
depth={item.depth}
active={item.active}
deprecated={item.deprecated}
>
<OperationBadge type={item.httpVerb} />
<MenuItemTitle width="calc(100% - 32px)">{item.name}</MenuItemTitle>
</MenuItemLabel>
Expand Down
12 changes: 11 additions & 1 deletion src/services/models/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { SecurityRequirementModel } from './SecurityRequirement';

import { OpenAPIExternalDocumentation, OpenAPIServer } from '../../types';

import { getOperationSummary, isAbsolutePath, JsonPointer, stripTrailingSlash } from '../../utils';
import {
getOperationSummary,
isAbsolutePath,
JsonPointer,
stripTrailingSlash,
sortByRequired,
} from '../../utils';
import { ContentItemModel, ExtendedOpenAPIOperation } from '../MenuBuilder';
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
Expand Down Expand Up @@ -77,6 +83,10 @@ export class OperationModel implements IMenuItem {
.concat(operationSpec.parameters || [])
.map(paramOrRef => new FieldModel(parser, paramOrRef, this._$ref, options));

if (options.requiredPropsFirst) {
sortByRequired(this.parameters);
}

let hasSuccessResponses = false;
this.responses = Object.keys(operationSpec.responses || [])
.filter(code => {
Expand Down
17 changes: 2 additions & 15 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isNamedDefinition,
isPrimitiveType,
JsonPointer,
sortByRequired,
} from '../../utils/';

// TODO: refactor this model, maybe use getters instead of copying all the values
Expand Down Expand Up @@ -226,7 +227,7 @@ function buildFields(
});

if (options.requiredPropsFirst) {
sortFields(fields, schema.required);
sortByRequired(fields, schema.required);
}

if (typeof additionalProps === 'object') {
Expand All @@ -246,17 +247,3 @@ function buildFields(

return fields;
}

function sortFields(fields: FieldModel[], order: string[] = []) {
fields.sort((a, b) => {
if (!a.required && b.required) {
return 1;
} else if (a.required && !b.required) {
return -1;
} else if (a.required && b.required) {
return order.indexOf(a.name) > order.indexOf(b.name) ? 1 : -1;
} else {
return 0;
}
});
}
17 changes: 17 additions & 0 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,21 @@ export function humanizeConstraints(schema: OpenAPISchema): string[] {
return res;
}

export function sortByRequired(
fields: { required: boolean; name: string }[],
order: string[] = [],
) {
fields.sort((a, b) => {
if (!a.required && b.required) {
return 1;
} else if (a.required && !b.required) {
return -1;
} else if (a.required && b.required) {
return order.indexOf(a.name) > order.indexOf(b.name) ? 1 : -1;
} else {
return 0;
}
});
}

export const SECURITY_SCHEMES_SECTION = 'section/Authentication/';

0 comments on commit ecf33d2

Please sign in to comment.