@gitbook/react-openapi 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/OpenAPICodeSample.jsx +3 -1
- package/dist/OpenAPIOperation.jsx +8 -5
- package/dist/OpenAPIResponse.jsx +16 -14
- package/dist/OpenAPIResponseExample.jsx +156 -47
- package/dist/OpenAPISchema.d.ts +2 -2
- package/dist/OpenAPISchema.jsx +13 -4
- package/dist/OpenAPISpec.jsx +2 -26
- package/dist/generateSchemaExample.d.ts +5 -6
- package/dist/generateSchemaExample.js +13 -8
- package/dist/json2xml.d.ts +4 -0
- package/dist/json2xml.js +7 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/utils.d.ts +26 -2
- package/dist/utils.js +72 -3
- package/package.json +2 -1
- package/src/OpenAPICodeSample.tsx +3 -1
- package/src/OpenAPIOperation.tsx +8 -5
- package/src/OpenAPIResponse.tsx +6 -12
- package/src/OpenAPIResponseExample.tsx +236 -69
- package/src/OpenAPISchema.tsx +18 -6
- package/src/OpenAPISpec.tsx +2 -17
- package/src/__snapshots__/json2xml.test.ts.snap +18 -0
- package/src/generateSchemaExample.ts +9 -7
- package/src/json2xml.test.ts +46 -0
- package/src/json2xml.ts +8 -0
- package/src/resolveOpenAPIOperation.test.ts +1 -1
- package/src/types.ts +1 -0
- package/src/utils.ts +81 -5
package/dist/types.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export interface OpenAPIContextProps extends OpenAPIClientContext {
|
|
|
4
4
|
code: string;
|
|
5
5
|
syntax: string;
|
|
6
6
|
}>;
|
|
7
|
+
renderHeading: (props: {
|
|
8
|
+
deprecated: boolean;
|
|
9
|
+
title: string;
|
|
10
|
+
}) => React.ReactNode;
|
|
7
11
|
/** Spec url for the Scalar Api Client */
|
|
8
12
|
specUrl: string;
|
|
9
13
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,7 +1,31 @@
|
|
|
1
|
-
import type { AnyObject, OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
2
|
-
export declare function checkIsReference(input: unknown): input is OpenAPIV3.ReferenceObject;
|
|
1
|
+
import type { AnyObject, OpenAPIV3, OpenAPIV3_1 } from '@gitbook/openapi-parser';
|
|
2
|
+
export declare function checkIsReference(input: unknown): input is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
|
3
3
|
export declare function createStateKey(key: string, scope?: string): string;
|
|
4
4
|
/**
|
|
5
5
|
* Resolve the description of an object.
|
|
6
6
|
*/
|
|
7
7
|
export declare function resolveDescription(object: AnyObject): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Extract descriptions from an object.
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractDescriptions(object: AnyObject): {
|
|
12
|
+
description: any;
|
|
13
|
+
"x-gitbook-description-html": any;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the first example from an object.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveFirstExample(object: AnyObject): any;
|
|
19
|
+
/**
|
|
20
|
+
* Resolve the schema of a parameter.
|
|
21
|
+
* Extract the description, example and deprecated from parameter.
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveParameterSchema(parameter: OpenAPIV3.ParameterBaseObject): OpenAPIV3.SchemaObject;
|
|
24
|
+
/**
|
|
25
|
+
* Transform a parameter object to a property object.
|
|
26
|
+
*/
|
|
27
|
+
export declare function parameterToProperty(parameter: OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject): {
|
|
28
|
+
propertyName: string | undefined;
|
|
29
|
+
schema: OpenAPIV3.SchemaObject;
|
|
30
|
+
required: boolean | undefined;
|
|
31
|
+
};
|
package/dist/utils.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
1
12
|
export function checkIsReference(input) {
|
|
2
13
|
return typeof input === 'object' && !!input && '$ref' in input;
|
|
3
14
|
}
|
|
@@ -8,9 +19,67 @@ export function createStateKey(key, scope) {
|
|
|
8
19
|
* Resolve the description of an object.
|
|
9
20
|
*/
|
|
10
21
|
export function resolveDescription(object) {
|
|
11
|
-
return 'x-description-html' in object &&
|
|
12
|
-
|
|
22
|
+
return 'x-gitbook-description-html' in object &&
|
|
23
|
+
typeof object['x-gitbook-description-html'] === 'string'
|
|
24
|
+
? object['x-gitbook-description-html'].trim()
|
|
13
25
|
: typeof object.description === 'string'
|
|
14
|
-
? object.description
|
|
26
|
+
? object.description.trim()
|
|
15
27
|
: undefined;
|
|
16
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Extract descriptions from an object.
|
|
31
|
+
*/
|
|
32
|
+
export function extractDescriptions(object) {
|
|
33
|
+
var _a;
|
|
34
|
+
return _a = {
|
|
35
|
+
description: object.description
|
|
36
|
+
},
|
|
37
|
+
_a['x-gitbook-description-html'] = 'x-gitbook-description-html' in object
|
|
38
|
+
? object['x-gitbook-description-html']
|
|
39
|
+
: undefined,
|
|
40
|
+
_a;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolve the first example from an object.
|
|
44
|
+
*/
|
|
45
|
+
export function resolveFirstExample(object) {
|
|
46
|
+
if ('examples' in object && typeof object.examples === 'object' && object.examples) {
|
|
47
|
+
var keys = Object.keys(object.examples);
|
|
48
|
+
var firstKey = keys[0];
|
|
49
|
+
if (firstKey && object.examples[firstKey]) {
|
|
50
|
+
return object.examples[firstKey];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if ('example' in object && object.example !== undefined) {
|
|
54
|
+
return object.example;
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resolve the schema of a parameter.
|
|
60
|
+
* Extract the description, example and deprecated from parameter.
|
|
61
|
+
*/
|
|
62
|
+
export function resolveParameterSchema(parameter) {
|
|
63
|
+
var schema = checkIsReference(parameter.schema) ? undefined : parameter.schema;
|
|
64
|
+
return __assign(__assign(__assign({}, extractDescriptions(parameter)), { example: resolveFirstExample(parameter),
|
|
65
|
+
// Deprecated can be defined at the parameter level
|
|
66
|
+
deprecated: parameter.deprecated }), schema);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Transform a parameter object to a property object.
|
|
70
|
+
*/
|
|
71
|
+
export function parameterToProperty(parameter) {
|
|
72
|
+
var _a;
|
|
73
|
+
if (checkIsReference(parameter)) {
|
|
74
|
+
return {
|
|
75
|
+
propertyName: (_a = parameter.$ref) !== null && _a !== void 0 ? _a : 'Unknown ref',
|
|
76
|
+
schema: {},
|
|
77
|
+
required: undefined,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
propertyName: parameter.name,
|
|
82
|
+
schema: resolveParameterSchema(parameter),
|
|
83
|
+
required: parameter.required,
|
|
84
|
+
};
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"default": "./dist/index.js"
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
|
-
"version": "1.0.
|
|
11
|
+
"version": "1.0.2",
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@gitbook/openapi-parser": "workspace:*",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"@scalar/oas-utils": "^0.2.101",
|
|
17
17
|
"clsx": "^2.1.1",
|
|
18
18
|
"flatted": "^3.2.9",
|
|
19
|
+
"json-xml-parse": "^1.3.0",
|
|
19
20
|
"react-aria-components": "^1.6.0",
|
|
20
21
|
"react-aria": "^3.37.0",
|
|
21
22
|
"usehooks-ts": "^3.1.0",
|
|
@@ -58,7 +58,9 @@ export function OpenAPICodeSample(props: {
|
|
|
58
58
|
(searchParams.size ? `?${searchParams.toString()}` : ''),
|
|
59
59
|
method: data.method,
|
|
60
60
|
body: requestBodyContent
|
|
61
|
-
? generateMediaTypeExample(requestBodyContent[1], {
|
|
61
|
+
? generateMediaTypeExample(requestBodyContent[1], {
|
|
62
|
+
omitEmptyAndOptionalProperties: true,
|
|
63
|
+
})
|
|
62
64
|
: undefined,
|
|
63
65
|
headers: {
|
|
64
66
|
...getSecurityHeaders(data.securities),
|
package/src/OpenAPIOperation.tsx
CHANGED
|
@@ -25,14 +25,17 @@ export function OpenAPIOperation(props: {
|
|
|
25
25
|
blockKey: context.blockKey,
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
const description = resolveDescription(operation)
|
|
28
|
+
const description = resolveDescription(operation);
|
|
29
29
|
|
|
30
30
|
return (
|
|
31
31
|
<div className={clsx('openapi-operation', className)}>
|
|
32
|
-
<div className="openapi-summary"
|
|
33
|
-
|
|
34
|
-
{
|
|
35
|
-
|
|
32
|
+
<div className="openapi-summary">
|
|
33
|
+
{operation.summary
|
|
34
|
+
? context.renderHeading({
|
|
35
|
+
deprecated: operation.deprecated ?? false,
|
|
36
|
+
title: operation.summary,
|
|
37
|
+
})
|
|
38
|
+
: null}
|
|
36
39
|
{operation.deprecated && <div className="openapi-deprecated">Deprecated</div>}
|
|
37
40
|
</div>
|
|
38
41
|
<div className="openapi-columns">
|
package/src/OpenAPIResponse.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
2
2
|
import { OpenAPISchemaProperties } from './OpenAPISchema';
|
|
3
|
-
import { resolveDescription } from './utils';
|
|
3
|
+
import { parameterToProperty, resolveDescription } from './utils';
|
|
4
4
|
import type { OpenAPIClientContext } from './types';
|
|
5
5
|
import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
|
6
6
|
|
|
@@ -27,13 +27,11 @@ export function OpenAPIResponse(props: {
|
|
|
27
27
|
return (
|
|
28
28
|
<div className="openapi-response-body">
|
|
29
29
|
{headers.length > 0 ? (
|
|
30
|
-
<OpenAPIDisclosure context={context} label=
|
|
30
|
+
<OpenAPIDisclosure context={context} label="Headers">
|
|
31
31
|
<OpenAPISchemaProperties
|
|
32
|
-
properties={headers.map(([name, header]) =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
required: header.required,
|
|
36
|
-
}))}
|
|
32
|
+
properties={headers.map(([name, header]) => {
|
|
33
|
+
return parameterToProperty({ name, ...header });
|
|
34
|
+
})}
|
|
37
35
|
context={context}
|
|
38
36
|
/>
|
|
39
37
|
</OpenAPIDisclosure>
|
|
@@ -41,11 +39,7 @@ export function OpenAPIResponse(props: {
|
|
|
41
39
|
<div className="openapi-responsebody">
|
|
42
40
|
<OpenAPISchemaProperties
|
|
43
41
|
id={`response-${context.blockKey}`}
|
|
44
|
-
properties={[
|
|
45
|
-
{
|
|
46
|
-
schema: mediaType.schema ?? {},
|
|
47
|
-
},
|
|
48
|
-
]}
|
|
42
|
+
properties={mediaType.schema ? [{ schema: mediaType.schema }] : []}
|
|
49
43
|
context={context}
|
|
50
44
|
/>
|
|
51
45
|
</div>
|
|
@@ -2,9 +2,9 @@ import type { OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
|
2
2
|
import { generateSchemaExample } from './generateSchemaExample';
|
|
3
3
|
import type { OpenAPIContextProps, OpenAPIOperationData } from './types';
|
|
4
4
|
import { checkIsReference, createStateKey, resolveDescription } from './utils';
|
|
5
|
-
import { stringifyOpenAPI } from './stringifyOpenAPI';
|
|
6
5
|
import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs';
|
|
7
6
|
import { InteractiveSection } from './InteractiveSection';
|
|
7
|
+
import { json2xml } from './json2xml';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Display an example of the response content.
|
|
@@ -38,84 +38,51 @@ export function OpenAPIResponseExample(props: {
|
|
|
38
38
|
return Number(a) - Number(b);
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
-
const
|
|
42
|
-
.map(([key,
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
const key = Object.keys(responseObject.content)[0];
|
|
49
|
-
return (
|
|
50
|
-
responseObject.content['application/json'] ??
|
|
51
|
-
(key ? responseObject.content[key] : null)
|
|
52
|
-
);
|
|
53
|
-
})();
|
|
54
|
-
|
|
55
|
-
if (!mediaTypeObject) {
|
|
41
|
+
const tabs = responses
|
|
42
|
+
.map(([key, responseObject]) => {
|
|
43
|
+
const description = resolveDescription(responseObject);
|
|
44
|
+
|
|
45
|
+
if (checkIsReference(responseObject)) {
|
|
56
46
|
return {
|
|
57
47
|
key: key,
|
|
58
48
|
label: key,
|
|
59
|
-
description
|
|
60
|
-
body:
|
|
49
|
+
description,
|
|
50
|
+
body: (
|
|
51
|
+
<OpenAPIExample
|
|
52
|
+
example={getExampleFromReference(responseObject)}
|
|
53
|
+
context={context}
|
|
54
|
+
syntax="json"
|
|
55
|
+
/>
|
|
56
|
+
),
|
|
61
57
|
};
|
|
62
58
|
}
|
|
63
59
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (firstExample) {
|
|
73
|
-
return firstExample;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (example) {
|
|
79
|
-
return { value: example };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const schema = mediaTypeObject.schema;
|
|
83
|
-
if (!schema) {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return { value: generateSchemaExample(schema) };
|
|
88
|
-
})(),
|
|
89
|
-
);
|
|
60
|
+
if (!responseObject.content || Object.keys(responseObject.content).length === 0) {
|
|
61
|
+
return {
|
|
62
|
+
key: key,
|
|
63
|
+
label: key,
|
|
64
|
+
description,
|
|
65
|
+
body: <OpenAPIEmptyResponseExample />,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
90
68
|
|
|
91
69
|
return {
|
|
92
70
|
key: key,
|
|
93
71
|
label: key,
|
|
94
72
|
description: resolveDescription(responseObject),
|
|
95
|
-
body:
|
|
96
|
-
<context.CodeBlock
|
|
97
|
-
code={
|
|
98
|
-
typeof example.value === 'string'
|
|
99
|
-
? example.value
|
|
100
|
-
: stringifyOpenAPI(example.value, null, 2)
|
|
101
|
-
}
|
|
102
|
-
syntax="json"
|
|
103
|
-
/>
|
|
104
|
-
) : (
|
|
105
|
-
<OpenAPIEmptyResponseExample />
|
|
106
|
-
),
|
|
73
|
+
body: <OpenAPIResponse context={context} content={responseObject.content} />,
|
|
107
74
|
};
|
|
108
75
|
})
|
|
109
76
|
.filter((val): val is { key: string; label: string; body: any; description: string } =>
|
|
110
77
|
Boolean(val),
|
|
111
78
|
);
|
|
112
79
|
|
|
113
|
-
if (
|
|
80
|
+
if (tabs.length === 0) {
|
|
114
81
|
return null;
|
|
115
82
|
}
|
|
116
83
|
|
|
117
84
|
return (
|
|
118
|
-
<OpenAPITabs stateKey={createStateKey('response-example')} items={
|
|
85
|
+
<OpenAPITabs stateKey={createStateKey('response-example')} items={tabs}>
|
|
119
86
|
<InteractiveSection header={<OpenAPITabsList />} className="openapi-response-example">
|
|
120
87
|
<OpenAPITabsPanels />
|
|
121
88
|
</InteractiveSection>
|
|
@@ -123,6 +90,212 @@ export function OpenAPIResponseExample(props: {
|
|
|
123
90
|
);
|
|
124
91
|
}
|
|
125
92
|
|
|
93
|
+
function OpenAPIResponse(props: {
|
|
94
|
+
context: OpenAPIContextProps;
|
|
95
|
+
content: {
|
|
96
|
+
[media: string]: OpenAPIV3.MediaTypeObject;
|
|
97
|
+
};
|
|
98
|
+
}) {
|
|
99
|
+
const { context, content } = props;
|
|
100
|
+
|
|
101
|
+
const entries = Object.entries(content);
|
|
102
|
+
const firstEntry = entries[0];
|
|
103
|
+
|
|
104
|
+
if (!firstEntry) {
|
|
105
|
+
throw new Error('One media type is required');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (entries.length === 1) {
|
|
109
|
+
const [mediaType, mediaTypeObject] = firstEntry;
|
|
110
|
+
return (
|
|
111
|
+
<OpenAPIResponseMediaType
|
|
112
|
+
context={context}
|
|
113
|
+
mediaType={mediaType}
|
|
114
|
+
mediaTypeObject={mediaTypeObject}
|
|
115
|
+
/>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const tabs = entries.map((entry) => {
|
|
120
|
+
const [mediaType, mediaTypeObject] = entry;
|
|
121
|
+
return {
|
|
122
|
+
key: mediaType,
|
|
123
|
+
label: mediaType,
|
|
124
|
+
body: (
|
|
125
|
+
<OpenAPIResponseMediaType
|
|
126
|
+
context={context}
|
|
127
|
+
mediaType={mediaType}
|
|
128
|
+
mediaTypeObject={mediaTypeObject}
|
|
129
|
+
/>
|
|
130
|
+
),
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<OpenAPITabs stateKey={createStateKey('response-media-types')} items={tabs}>
|
|
136
|
+
<InteractiveSection
|
|
137
|
+
header={<OpenAPITabsList />}
|
|
138
|
+
className="openapi-response-media-types"
|
|
139
|
+
>
|
|
140
|
+
<OpenAPITabsPanels />
|
|
141
|
+
</InteractiveSection>
|
|
142
|
+
</OpenAPITabs>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function OpenAPIResponseMediaType(props: {
|
|
147
|
+
mediaTypeObject: OpenAPIV3.MediaTypeObject;
|
|
148
|
+
mediaType: string;
|
|
149
|
+
context: OpenAPIContextProps;
|
|
150
|
+
}) {
|
|
151
|
+
const { mediaTypeObject, mediaType } = props;
|
|
152
|
+
const examples = getExamplesFromMediaTypeObject({ mediaTypeObject, mediaType });
|
|
153
|
+
const syntax = getSyntaxFromMediaType(mediaType);
|
|
154
|
+
const firstExample = examples[0];
|
|
155
|
+
|
|
156
|
+
if (!firstExample) {
|
|
157
|
+
return <OpenAPIEmptyResponseExample />;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (examples.length === 1) {
|
|
161
|
+
return (
|
|
162
|
+
<OpenAPIExample
|
|
163
|
+
example={firstExample.example}
|
|
164
|
+
context={props.context}
|
|
165
|
+
syntax={syntax}
|
|
166
|
+
/>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const tabs = examples.map((example) => {
|
|
171
|
+
return {
|
|
172
|
+
key: example.key,
|
|
173
|
+
label: example.example.summary || example.key,
|
|
174
|
+
body: (
|
|
175
|
+
<OpenAPIExample
|
|
176
|
+
example={firstExample.example}
|
|
177
|
+
context={props.context}
|
|
178
|
+
syntax={syntax}
|
|
179
|
+
/>
|
|
180
|
+
),
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<OpenAPITabs stateKey={createStateKey('response-media-type-examples')} items={tabs}>
|
|
186
|
+
<InteractiveSection
|
|
187
|
+
header={<OpenAPITabsList />}
|
|
188
|
+
className="openapi-response-media-type-examples"
|
|
189
|
+
>
|
|
190
|
+
<OpenAPITabsPanels />
|
|
191
|
+
</InteractiveSection>
|
|
192
|
+
</OpenAPITabs>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Display an example.
|
|
198
|
+
*/
|
|
199
|
+
function OpenAPIExample(props: {
|
|
200
|
+
example: OpenAPIV3.ExampleObject;
|
|
201
|
+
context: OpenAPIContextProps;
|
|
202
|
+
syntax: string;
|
|
203
|
+
}) {
|
|
204
|
+
const { example, context, syntax } = props;
|
|
205
|
+
const code = stringifyExample({ example, xml: syntax === 'xml' });
|
|
206
|
+
|
|
207
|
+
if (code === null) {
|
|
208
|
+
return <OpenAPIEmptyResponseExample />;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return <context.CodeBlock code={code} syntax={syntax} />;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function stringifyExample(args: { example: OpenAPIV3.ExampleObject; xml: boolean }): string | null {
|
|
215
|
+
const { example, xml } = args;
|
|
216
|
+
|
|
217
|
+
if (!example.value) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (typeof example.value === 'string') {
|
|
222
|
+
return example.value;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (xml) {
|
|
226
|
+
return json2xml(example.value);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return JSON.stringify(example.value, null, 2);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Get the syntax from a media type.
|
|
234
|
+
*/
|
|
235
|
+
function getSyntaxFromMediaType(mediaType: string): string {
|
|
236
|
+
if (mediaType.includes('json')) {
|
|
237
|
+
return 'json';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (mediaType === 'application/xml') {
|
|
241
|
+
return 'xml';
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return 'text';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Get examples from a media type object.
|
|
249
|
+
*/
|
|
250
|
+
function getExamplesFromMediaTypeObject(args: {
|
|
251
|
+
mediaType: string;
|
|
252
|
+
mediaTypeObject: OpenAPIV3.MediaTypeObject;
|
|
253
|
+
}): { key: string; example: OpenAPIV3.ExampleObject }[] {
|
|
254
|
+
const { mediaTypeObject, mediaType } = args;
|
|
255
|
+
if (mediaTypeObject.examples) {
|
|
256
|
+
return Object.entries(mediaTypeObject.examples).map(([key, example]) => {
|
|
257
|
+
return {
|
|
258
|
+
key,
|
|
259
|
+
example: checkIsReference(example) ? getExampleFromReference(example) : example,
|
|
260
|
+
};
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (mediaTypeObject.example) {
|
|
265
|
+
return [{ key: 'default', example: { value: mediaTypeObject.example } }];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (mediaTypeObject.schema) {
|
|
269
|
+
if (mediaType === 'application/xml') {
|
|
270
|
+
// @TODO normally we should use the name of the schema but we don't have it
|
|
271
|
+
// fix it when we got the reference name
|
|
272
|
+
const root = mediaTypeObject.schema.xml?.name ?? 'object';
|
|
273
|
+
return [
|
|
274
|
+
{
|
|
275
|
+
key: 'default',
|
|
276
|
+
example: {
|
|
277
|
+
value: {
|
|
278
|
+
[root]: generateSchemaExample(mediaTypeObject.schema, {
|
|
279
|
+
xml: mediaType === 'application/xml',
|
|
280
|
+
}),
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
];
|
|
285
|
+
}
|
|
286
|
+
return [
|
|
287
|
+
{
|
|
288
|
+
key: 'default',
|
|
289
|
+
example: { value: generateSchemaExample(mediaTypeObject.schema) },
|
|
290
|
+
},
|
|
291
|
+
];
|
|
292
|
+
}
|
|
293
|
+
return [];
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Empty response example.
|
|
298
|
+
*/
|
|
126
299
|
function OpenAPIEmptyResponseExample() {
|
|
127
300
|
return (
|
|
128
301
|
<pre className="openapi-response-example-empty">
|
|
@@ -131,15 +304,9 @@ function OpenAPIEmptyResponseExample() {
|
|
|
131
304
|
);
|
|
132
305
|
}
|
|
133
306
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (isReference) {
|
|
140
|
-
// If we find a reference that wasn't resolved or needed to be resolved externally, render out the URL
|
|
141
|
-
return { value: input.value.$ref };
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return input;
|
|
307
|
+
/**
|
|
308
|
+
* Generate an example from a reference object.
|
|
309
|
+
*/
|
|
310
|
+
function getExampleFromReference(ref: OpenAPIV3.ReferenceObject): OpenAPIV3.ExampleObject {
|
|
311
|
+
return { summary: 'Unresolved reference', value: { $ref: ref.$ref } };
|
|
145
312
|
}
|
package/src/OpenAPISchema.tsx
CHANGED
|
@@ -13,8 +13,8 @@ import { OpenAPIDisclosure } from './OpenAPIDisclosure';
|
|
|
13
13
|
type CircularRefsIds = Map<OpenAPIV3.SchemaObject, string>;
|
|
14
14
|
|
|
15
15
|
export interface OpenAPISchemaPropertyEntry {
|
|
16
|
-
propertyName?: string;
|
|
17
|
-
required?: boolean;
|
|
16
|
+
propertyName?: string | undefined;
|
|
17
|
+
required?: boolean | undefined;
|
|
18
18
|
schema: OpenAPIV3.SchemaObject;
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -47,7 +47,7 @@ export function OpenAPISchemaProperty(
|
|
|
47
47
|
? null
|
|
48
48
|
: getSchemaAlternatives(schema, new Set(circularRefs.keys()));
|
|
49
49
|
|
|
50
|
-
if ((properties &&
|
|
50
|
+
if ((properties && properties.length > 0) || schema.type === 'object') {
|
|
51
51
|
return (
|
|
52
52
|
<InteractiveSection id={id} className={clsx('openapi-schema', className)}>
|
|
53
53
|
<OpenAPISchemaPresentation {...props} />
|
|
@@ -397,7 +397,7 @@ export function getSchemaTitle(
|
|
|
397
397
|
let type = 'any';
|
|
398
398
|
|
|
399
399
|
if (schema.enum) {
|
|
400
|
-
type =
|
|
400
|
+
type = `${schema.type} · enum`;
|
|
401
401
|
// check array AND schema.items as this is sometimes null despite what the type indicates
|
|
402
402
|
} else if (schema.type === 'array' && !!schema.items) {
|
|
403
403
|
type = `${getSchemaTitle(schema.items)}[]`;
|
|
@@ -407,7 +407,7 @@ export function getSchemaTitle(
|
|
|
407
407
|
type = schema.type ?? 'object';
|
|
408
408
|
|
|
409
409
|
if (schema.format) {
|
|
410
|
-
type += ` ${schema.format}`;
|
|
410
|
+
type += ` · ${schema.format}`;
|
|
411
411
|
}
|
|
412
412
|
} else if ('anyOf' in schema) {
|
|
413
413
|
type = 'any of';
|
|
@@ -419,8 +419,20 @@ export function getSchemaTitle(
|
|
|
419
419
|
type = 'not';
|
|
420
420
|
}
|
|
421
421
|
|
|
422
|
+
if (schema.minimum || schema.minLength) {
|
|
423
|
+
type += ` · min: ${schema.minimum || schema.minLength}`;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (schema.maximum || schema.maxLength) {
|
|
427
|
+
type += ` · max: ${schema.maximum || schema.maxLength}`;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (schema.default) {
|
|
431
|
+
type += ` · default: ${schema.default}`;
|
|
432
|
+
}
|
|
433
|
+
|
|
422
434
|
if (schema.nullable) {
|
|
423
|
-
type =
|
|
435
|
+
type = `${type} | nullable`;
|
|
424
436
|
}
|
|
425
437
|
|
|
426
438
|
return type;
|
package/src/OpenAPISpec.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import { OpenAPIResponses } from './OpenAPIResponses';
|
|
|
8
8
|
import { OpenAPISchemaProperties } from './OpenAPISchema';
|
|
9
9
|
import { OpenAPISecurities } from './OpenAPISecurities';
|
|
10
10
|
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
|
|
11
|
-
import {
|
|
11
|
+
import { parameterToProperty } from './utils';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Client component to render the spec for the request and response.
|
|
@@ -38,22 +38,7 @@ export function OpenAPISpec(props: { data: OpenAPIOperationData; context: OpenAP
|
|
|
38
38
|
header={group.label}
|
|
39
39
|
>
|
|
40
40
|
<OpenAPISchemaProperties
|
|
41
|
-
properties={group.parameters.map(
|
|
42
|
-
const description = resolveDescription(parameter);
|
|
43
|
-
return {
|
|
44
|
-
propertyName: parameter.name,
|
|
45
|
-
schema: {
|
|
46
|
-
// Description of the parameter is defined at the parameter level
|
|
47
|
-
// we use display it if the schema doesn't override it
|
|
48
|
-
description: description,
|
|
49
|
-
example: parameter.example,
|
|
50
|
-
// Deprecated can be defined at the parameter level
|
|
51
|
-
deprecated: parameter.deprecated,
|
|
52
|
-
...(parameter.schema ?? {}),
|
|
53
|
-
},
|
|
54
|
-
required: parameter.required,
|
|
55
|
-
};
|
|
56
|
-
})}
|
|
41
|
+
properties={group.parameters.map(parameterToProperty)}
|
|
57
42
|
context={context}
|
|
58
43
|
/>
|
|
59
44
|
</InteractiveSection>
|