@@ -44,16 +44,24 @@ class Node<T, P> {
4444}
4545type SchemaObjectNode = Node < SchemaObject , SchemaObject > ;
4646
47- function isParameterObject ( node : ParameterObject | ReferenceObject ) : node is ParameterObject {
48- return ! ( ( node as ReferenceObject ) . $ref ) ;
47+ function isParameterObject (
48+ node : ParameterObject | ReferenceObject ,
49+ ) : node is ParameterObject {
50+ return ! ( node as ReferenceObject ) . $ref ;
4951}
50- function isReferenceObject ( node : ArraySchemaObject | NonArraySchemaObject | ReferenceObject ) : node is ReferenceObject {
51- return ! ! ( ( node as ReferenceObject ) . $ref ) ;
52+ function isReferenceObject (
53+ node : ArraySchemaObject | NonArraySchemaObject | ReferenceObject ,
54+ ) : node is ReferenceObject {
55+ return ! ! ( node as ReferenceObject ) . $ref ;
5256}
53- function isArraySchemaObject ( node : ArraySchemaObject | NonArraySchemaObject | ReferenceObject ) : node is ArraySchemaObject {
54- return ! ! ( ( node as ArraySchemaObject ) . items ) ;
57+ function isArraySchemaObject (
58+ node : ArraySchemaObject | NonArraySchemaObject | ReferenceObject ,
59+ ) : node is ArraySchemaObject {
60+ return ! ! ( node as ArraySchemaObject ) . items ;
5561}
56- function isNonArraySchemaObject ( node : ArraySchemaObject | NonArraySchemaObject | ReferenceObject ) : node is NonArraySchemaObject {
62+ function isNonArraySchemaObject (
63+ node : ArraySchemaObject | NonArraySchemaObject | ReferenceObject ,
64+ ) : node is NonArraySchemaObject {
5765 return ! isArraySchemaObject ( node ) && ! isReferenceObject ( node ) ;
5866}
5967
@@ -119,6 +127,10 @@ export class SchemaPreprocessor {
119127 // Now that we've processed paths, clone a response spec if we are validating responses
120128 this . apiDocRes = ! ! this . responseOpts ? cloneDeep ( this . apiDoc ) : null ;
121129
130+ if ( this . apiDoc . components ) {
131+ this . removeExamples ( this . apiDoc . components ) ;
132+ }
133+
122134 const schemaNodes = {
123135 schemas : componentSchemas ,
124136 requestBodies : r ?. requestBodies ,
@@ -163,7 +175,7 @@ export class SchemaPreprocessor {
163175 // Since OpenAPI 3.1, paths can be a #ref to reusable path items
164176 // The following line mutates the paths item to dereference the reference, so that we can process as a POJO, as we would if it wasn't a reference
165177 this . apiDoc . paths [ p ] = pathItem ;
166-
178+
167179 for ( const method of Object . keys ( pathItem ) ) {
168180 if ( httpMethods . has ( method ) ) {
169181 const operation = < OpenAPIV3 . OperationObject > pathItem [ method ] ;
@@ -173,7 +185,8 @@ export class SchemaPreprocessor {
173185 const node = new Root < OpenAPIV3 . OperationObject > ( operation , path ) ;
174186 const requestBodies = this . extractRequestBodySchemaNodes ( node ) ;
175187 const responseBodies = this . extractResponseSchemaNodes ( node ) ;
176- const requestParameters = this . extractRequestParameterSchemaNodes ( node ) ;
188+ const requestParameters =
189+ this . extractRequestParameterSchemaNodes ( node ) ;
177190
178191 requestBodySchemas . push ( ...requestBodies ) ;
179192 responseSchemas . push ( ...responseBodies ) ;
@@ -246,7 +259,10 @@ export class SchemaPreprocessor {
246259 recurse ( node , child , opts ) ;
247260 } ) ;
248261 } else if ( schema . additionalProperties ) {
249- const child = new Node ( node , schema . additionalProperties , [ ...node . path , 'additionalProperties' ] ) ;
262+ const child = new Node ( node , schema . additionalProperties , [
263+ ...node . path ,
264+ 'additionalProperties' ,
265+ ] ) ;
250266 recurse ( node , child , opts ) ;
251267 }
252268 } ;
@@ -302,7 +318,7 @@ export class SchemaPreprocessor {
302318 this . handleReadonly ( pschema , nschema , options ) ;
303319 this . handleWriteonly ( pschema , nschema , options ) ;
304320 this . processDiscriminator ( pschema , nschema , options ) ;
305- this . removeExamples ( pschema , nschema , options )
321+ this . removeSchemaExamples ( pschema , nschema , options ) ;
306322 }
307323 }
308324 }
@@ -458,21 +474,23 @@ export class SchemaPreprocessor {
458474 }
459475 }
460476
461- private removeExamples (
477+ private removeSchemaExamples (
462478 parent : OpenAPIV3 . SchemaObject ,
463479 schema : OpenAPIV3 . SchemaObject ,
464480 opts ,
465481 ) {
466- // Remove example and examples from all schema types, not just objects
467- if ( schema ?. example ) {
468- delete schema . example ;
469- }
470- if ( schema ?. examples ) {
471- delete schema . examples ;
472- }
482+ this . removeExamples ( parent ) ;
483+ this . removeExamples ( schema ) ;
484+ }
485+
486+ private removeExamples (
487+ object : OpenAPIV3 . SchemaObject | OpenAPIV3 . MediaTypeObject ,
488+ ) : void {
489+ delete object ?. example ;
490+ delete object ?. examples ;
473491 }
474492
475- private handleReadonly (
493+ private handleReadonly (
476494 parent : OpenAPIV3 . SchemaObject ,
477495 schema : OpenAPIV3 . SchemaObject ,
478496 opts ,
@@ -536,6 +554,10 @@ private handleReadonly(
536554 mediaTypeObject . schema ,
537555 ) ;
538556 op . requestBody . content [ type ] . schema = mediaTypeSchema ;
557+
558+ // TODO replace with visitor
559+ this . removeExamples ( op . requestBody . content [ type ] ) ;
560+
539561 const path = [ ...node . path , 'requestBody' , 'content' , type , 'schema' ] ;
540562 result . push ( new Root ( mediaTypeSchema , path ) ) ;
541563 }
@@ -575,6 +597,10 @@ private handleReadonly(
575597 type ,
576598 'schema' ,
577599 ] ;
600+
601+ // TODO replace with visitor
602+ this . removeExamples ( rschema . content [ type ] ) ;
603+
578604 schemas . push ( new Root ( schema , path ) ) ;
579605 }
580606 }
@@ -586,21 +612,25 @@ private handleReadonly(
586612 private extractRequestParameterSchemaNodes (
587613 operationNode : Root < OperationObject > ,
588614 ) : Root < SchemaObject > [ ] {
589-
590615 return ( operationNode . schema . parameters ?? [ ] ) . flatMap ( ( node ) => {
591616 const parameterObject = isParameterObject ( node ) ? node : undefined ;
617+
618+ // TODO replace with visitor
619+ // TODO This does not handle JSON query parameters
620+ this . removeExamples ( parameterObject ) ;
621+
592622 if ( ! parameterObject ?. schema ) return [ ] ;
593623
594- const schema = isNonArraySchemaObject ( parameterObject . schema ) ?
595- parameterObject . schema :
596- undefined ;
624+ const schema = isNonArraySchemaObject ( parameterObject . schema )
625+ ? parameterObject . schema
626+ : undefined ;
597627 if ( ! schema ) return [ ] ;
598628
599629 return new Root ( schema , [
600630 ...operationNode . path ,
601631 'parameters' ,
602632 parameterObject . name ,
603- parameterObject . in
633+ parameterObject . in ,
604634 ] ) ;
605635 } ) ;
606636 }
0 commit comments