@@ -5,9 +5,8 @@ import clsx from 'clsx';
5
5
import { Markdown } from './Markdown' ;
6
6
import { OpenAPIDisclosure } from './OpenAPIDisclosure' ;
7
7
import { OpenAPISchemaName } from './OpenAPISchemaName' ;
8
- import { stringifyOpenAPI } from './stringifyOpenAPI' ;
9
8
import type { OpenAPIClientContext } from './types' ;
10
- import { checkIsReference , resolveDescription } from './utils' ;
9
+ import { checkIsReference , resolveDescription , resolveFirstExample } from './utils' ;
11
10
12
11
type CircularRefsIds = Map < OpenAPIV3 . SchemaObject , string > ;
13
12
@@ -199,30 +198,22 @@ export function OpenAPISchemaEnum(props: { enumValues: any[] }) {
199
198
) ;
200
199
}
201
200
202
- export function OpenAPISchemaPresentation ( props : { property : OpenAPISchemaPropertyEntry } ) {
201
+ export function OpenAPISchemaPresentation (
202
+ props : { property : OpenAPISchemaPropertyEntry } & { showType ?: boolean }
203
+ ) {
203
204
const {
204
205
property : { schema, propertyName, required } ,
206
+ showType = true ,
205
207
} = props ;
206
208
207
- const shouldDisplayExample = ( schema : OpenAPIV3 . SchemaObject ) : boolean => {
208
- return (
209
- ( typeof schema . example === 'string' && ! ! schema . example ) ||
210
- typeof schema . example === 'number' ||
211
- typeof schema . example === 'boolean' ||
212
- ( Array . isArray ( schema . example ) && schema . example . length > 0 ) ||
213
- ( typeof schema . example === 'object' &&
214
- schema . example !== null &&
215
- Object . keys ( schema . example ) . length > 0 )
216
- ) ;
217
- } ;
218
-
219
209
const description = resolveDescription ( schema ) ;
210
+ const example = resolveFirstExample ( schema ) ;
220
211
221
212
return (
222
213
< div className = "openapi-schema-presentation" >
223
214
< OpenAPISchemaName
224
215
schema = { schema }
225
- type = { getSchemaTitle ( schema ) }
216
+ type = { showType ? getSchemaTitle ( schema ) : undefined }
226
217
propertyName = { propertyName }
227
218
required = { required }
228
219
/>
@@ -237,9 +228,9 @@ export function OpenAPISchemaPresentation(props: { property: OpenAPISchemaProper
237
228
{ description ? (
238
229
< Markdown source = { description } className = "openapi-schema-description" />
239
230
) : null }
240
- { shouldDisplayExample ( schema ) ? (
231
+ { example ? (
241
232
< div className = "openapi-schema-example" >
242
- Example: < code > { formatExample ( schema . example ) } </ code >
233
+ Example: < code > { example } </ code >
243
234
</ div >
244
235
) : null }
245
236
{ schema . pattern ? (
@@ -257,7 +248,9 @@ export function OpenAPISchemaPresentation(props: { property: OpenAPISchemaProper
257
248
/**
258
249
* Get the sub-properties of a schema.
259
250
*/
260
- function getSchemaProperties ( schema : OpenAPIV3 . SchemaObject ) : null | OpenAPISchemaPropertyEntry [ ] {
251
+ export function getSchemaProperties (
252
+ schema : OpenAPIV3 . SchemaObject
253
+ ) : null | OpenAPISchemaPropertyEntry [ ] {
261
254
// check array AND schema.items as this is sometimes null despite what the type indicates
262
255
if ( schema . type === 'array' && schema . items && ! checkIsReference ( schema . items ) ) {
263
256
const items = schema . items ;
@@ -418,16 +411,3 @@ function getDisclosureLabel(schema: OpenAPIV3.SchemaObject): string {
418
411
419
412
return schema . title || 'child attributes' ;
420
413
}
421
-
422
- function formatExample ( example : any ) : string {
423
- if ( typeof example === 'string' ) {
424
- return example
425
- . replace ( / \n / g, ' ' ) // Replace newlines with spaces
426
- . replace ( / \s + / g, ' ' ) // Collapse multiple spaces/newlines into a single space
427
- . replace ( / ( [ \{ \} : , ] ) \s + / g, '$1 ' ) // Ensure a space after {, }, :, and ,
428
- . replace ( / \s + ( [ \{ \} : , ] ) / g, ' $1' ) // Ensure a space before {, }, :, and ,
429
- . trim ( ) ;
430
- }
431
-
432
- return stringifyOpenAPI ( example ) ;
433
- }
0 commit comments