@@ -6,9 +6,8 @@ import { InteractiveSection } from './InteractiveSection';
6
6
import { Markdown } from './Markdown' ;
7
7
import { OpenAPIDisclosure } from './OpenAPIDisclosure' ;
8
8
import { OpenAPISchemaName } from './OpenAPISchemaName' ;
9
- import { stringifyOpenAPI } from './stringifyOpenAPI' ;
10
9
import type { OpenAPIClientContext } from './types' ;
11
- import { checkIsReference , resolveDescription } from './utils' ;
10
+ import { checkIsReference , resolveDescription , resolveFirstExample } from './utils' ;
12
11
13
12
type CircularRefsIds = Map < OpenAPIV3 . SchemaObject , string > ;
14
13
@@ -269,28 +268,19 @@ export function OpenAPISchemaEnum(props: { enumValues: any[] }) {
269
268
) ;
270
269
}
271
270
272
- export function OpenAPISchemaPresentation ( props : OpenAPISchemaPropertyEntry ) {
273
- const { schema, propertyName, required } = props ;
274
-
275
- const shouldDisplayExample = ( schema : OpenAPIV3 . SchemaObject ) : boolean => {
276
- return (
277
- ( typeof schema . example === 'string' && ! ! schema . example ) ||
278
- typeof schema . example === 'number' ||
279
- typeof schema . example === 'boolean' ||
280
- ( Array . isArray ( schema . example ) && schema . example . length > 0 ) ||
281
- ( typeof schema . example === 'object' &&
282
- schema . example !== null &&
283
- Object . keys ( schema . example ) . length > 0 )
284
- ) ;
285
- } ;
271
+ export function OpenAPISchemaPresentation (
272
+ props : OpenAPISchemaPropertyEntry & { showType ?: boolean }
273
+ ) {
274
+ const { schema, propertyName, required, showType = true } = props ;
286
275
287
276
const description = resolveDescription ( schema ) ;
277
+ const example = resolveFirstExample ( schema ) ;
288
278
289
279
return (
290
280
< div className = "openapi-schema-presentation" >
291
281
< OpenAPISchemaName
292
282
schema = { schema }
293
- type = { getSchemaTitle ( schema ) }
283
+ type = { showType ? getSchemaTitle ( schema ) : undefined }
294
284
propertyName = { propertyName }
295
285
required = { required }
296
286
/>
@@ -305,9 +295,9 @@ export function OpenAPISchemaPresentation(props: OpenAPISchemaPropertyEntry) {
305
295
{ description ? (
306
296
< Markdown source = { description } className = "openapi-schema-description" />
307
297
) : null }
308
- { shouldDisplayExample ( schema ) ? (
298
+ { example ? (
309
299
< div className = "openapi-schema-example" >
310
- Example: < code > { formatExample ( schema . example ) } </ code >
300
+ Example: < code > { example } </ code >
311
301
</ div >
312
302
) : null }
313
303
{ schema . pattern ? (
@@ -325,7 +315,9 @@ export function OpenAPISchemaPresentation(props: OpenAPISchemaPropertyEntry) {
325
315
/**
326
316
* Get the sub-properties of a schema.
327
317
*/
328
- function getSchemaProperties ( schema : OpenAPIV3 . SchemaObject ) : null | OpenAPISchemaPropertyEntry [ ] {
318
+ export function getSchemaProperties (
319
+ schema : OpenAPIV3 . SchemaObject
320
+ ) : null | OpenAPISchemaPropertyEntry [ ] {
329
321
// check array AND schema.items as this is sometimes null despite what the type indicates
330
322
if ( schema . type === 'array' && ! ! schema . items ) {
331
323
const items = schema . items ;
@@ -485,16 +477,3 @@ function getDisclosureLabel(schema: OpenAPIV3.SchemaObject): string | undefined
485
477
486
478
return schema . title ;
487
479
}
488
-
489
- function formatExample ( example : any ) : string {
490
- if ( typeof example === 'string' ) {
491
- return example
492
- . replace ( / \n / g, ' ' ) // Replace newlines with spaces
493
- . replace ( / \s + / g, ' ' ) // Collapse multiple spaces/newlines into a single space
494
- . replace ( / ( [ \{ \} : , ] ) \s + / g, '$1 ' ) // Ensure a space after {, }, :, and ,
495
- . replace ( / \s + ( [ \{ \} : , ] ) / g, ' $1' ) // Ensure a space before {, }, :, and ,
496
- . trim ( ) ;
497
- }
498
-
499
- return stringifyOpenAPI ( example ) ;
500
- }
0 commit comments