|
| 1 | +import { getOr } from 'lodash/fp'; |
1 | 2 | import React from "react";
|
2 | 3 | import Markdown from "react-markdown/with-html";
|
3 | 4 |
|
4 | 5 | import CharDisplay from "./charDisplay";
|
5 | 6 |
|
6 | 7 | export const BodySchema = props => {
|
7 | 8 | const { data } = props;
|
| 9 | + const requiredProperties = getOr([], ['requestBody', 'content', 'application_json', 'schema', 'required'], data) || []; |
| 10 | + const properties = getOr(null, ['requestBody', 'content', 'application_json', 'schema', 'properties'], data); |
| 11 | + |
| 12 | + const sortByRequired = (a, b) => { |
| 13 | + const r1 = requiredProperties.includes(a) ? 1 : 0; |
| 14 | + const r2 = requiredProperties.includes(b) ? 1 : 0; |
| 15 | + // sorting and adding required fields at the top |
| 16 | + if (r1 > r2) { |
| 17 | + return -1; |
| 18 | + } |
| 19 | + if (r1 < r2) { |
| 20 | + return 1; |
| 21 | + } |
| 22 | + if (a > b) { |
| 23 | + return 1; |
| 24 | + } |
| 25 | + if (a < b) { |
| 26 | + return -1; |
| 27 | + } |
| 28 | + return 0; |
| 29 | + } |
8 | 30 |
|
9 | 31 | return (
|
10 | 32 | <>
|
11 | 33 | <div className="my-8">
|
12 | 34 | <h3>Request Body Schema</h3>
|
13 | 35 | </div>
|
14 |
| - {data.requestBody.content.application_json && |
15 |
| - data.requestBody.content.application_json.schema && |
16 |
| - data.requestBody.content.application_json.schema.properties && |
17 |
| - Object.keys(data.requestBody.content.application_json.schema.properties) |
18 |
| - .sort((a, b) => { |
19 |
| - const required = |
20 |
| - data.requestBody.content.application_json.schema.required || []; |
21 |
| - const r1 = required.includes(a) ? 1 : 0; |
22 |
| - const r2 = required.includes(b) ? 1 : 0; |
23 |
| - // sorting and adding required fields at the top |
24 |
| - if (r1 > r2) { |
25 |
| - return -1; |
26 |
| - } |
27 |
| - if (r1 < r2) { |
28 |
| - return 1; |
29 |
| - } |
30 |
| - if (a > b) { |
31 |
| - return 1; |
32 |
| - } |
33 |
| - if (a < b) { |
34 |
| - return -1; |
35 |
| - } |
36 |
| - return 0; |
37 |
| - }) |
| 36 | + {properties && |
| 37 | + Object.keys(properties) |
| 38 | + .sort(sortByRequired) |
38 | 39 | .map((p, i) => {
|
39 | 40 | const b =
|
40 | 41 | data.requestBody.content.application_json.schema.properties[p];
|
|
0 commit comments