Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merge bug fix - mixed up property descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
MilanLund committed Dec 23, 2019
1 parent 12dc910 commit bd3f213
Show file tree
Hide file tree
Showing 6 changed files with 6,963 additions and 1,445 deletions.
5 changes: 4 additions & 1 deletion demo/playground/hmr-playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ const specUrl = 'test.json';

let store;
const options: RedocRawOptions = {
nativeScrollbars: false,
disableSearch: true,
hideDownloadButton: true,
jsonSampleExpandLevel: 3,
pathInMiddlePanel: true,
requiredPropsFirst: true,
sortPropsAlphabetically: true,
theme: {
spacing: {
unit: 4,
Expand Down
8,357 changes: 6,934 additions & 1,423 deletions demo/test.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kentico-kontent-docs-redoc",
"version": "3.0.2",
"version": "3.0.3",
"description": "ReDoc",
"repository": {
"type": "git",
Expand Down
5 changes: 2 additions & 3 deletions src/services/models/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ export class OperationModel implements IMenuItem {
).map(paramOrRef => new FieldModel(this.parser, paramOrRef, this.pointer, this.options));

if (this.options.sortPropsAlphabetically) {
sortByField(_parameters, 'name');
return sortByField(_parameters, 'name');
}
if (this.options.requiredPropsFirst) {
sortByRequired(_parameters);
return sortByRequired(_parameters);
}
return _parameters;
}

@memoize
Expand Down
6 changes: 3 additions & 3 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ function buildFields(
const props = schema.properties || {};
const additionalProps = schema.additionalProperties;
const defaults = schema.default || {};
const fields = Object.keys(props || []).map(fieldName => {
let fields = Object.keys(props || []).map(fieldName => {
let field = props[fieldName];

if (!field) {
Expand Down Expand Up @@ -280,11 +280,11 @@ function buildFields(
});

if (options.sortPropsAlphabetically) {
sortByField(fields, 'name');
fields = sortByField(fields, 'name');
}
if (options.requiredPropsFirst) {
// if not sort alphabetically sort in the order from required keyword
sortByRequired(fields, !options.sortPropsAlphabetically ? schema.required : undefined);
fields = sortByRequired(fields, !options.sortPropsAlphabetically ? schema.required : undefined);
}

if (typeof additionalProps === 'object' || additionalProps === true) {
Expand Down
33 changes: 19 additions & 14 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { dirname } from 'path';
const URLtemplate = require('url-template');

import { FieldModel } from '../services/models';
import { OpenAPIParser } from '../services/OpenAPIParser';
import {
OpenAPIEncoding,
Expand Down Expand Up @@ -413,25 +414,29 @@ export function humanizeConstraints(schema: OpenAPISchema): string[] {
return res;
}

export function sortByRequired(
fields: Array<{ 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);
export function sortByRequired(fields: FieldModel[], order: string[] = []) {
const unrequiredFields: FieldModel[] = [];
const orderedFields: FieldModel[] = [];
const unorderedFields: FieldModel[] = [];

fields.forEach(field => {
if (field.required) {
order.includes(field.name) ? orderedFields.push(field) : unorderedFields.push(field);
} else {
return 0;
unrequiredFields.push(field);
}
});

orderedFields.sort((a, b) => order.indexOf(a.name) - order.indexOf(b.name));

return [...orderedFields, ...unorderedFields, ...unrequiredFields];
}

export function sortByField<T extends string>(fields: Array<{ [P in T]: string }>, param: T) {
fields.sort((a, b) => {
export function sortByField(
fields: FieldModel[],
param: keyof Pick<FieldModel, 'name' | 'description' | 'kind'>,
) {
return [...fields].sort((a, b) => {
return a[param].localeCompare(b[param]);
});
}
Expand Down

0 comments on commit bd3f213

Please sign in to comment.