@@ -18,6 +18,7 @@ import type {
1818import { entityToJSONSchema } from 'payload'
1919import type { SanitizedPluginOptions } from '../types.js'
2020import { isHiddenField } from '../utils/fields.js'
21+ import { shouldIncludeCollection , shouldIncludeGlobal } from '../utils/filters.js'
2122import { mapValuesAsync , visitObjectNodes } from '../utils/objects.js'
2223import { type ComponentType , collectionName , componentName , globalName } from './naming.js'
2324import { apiKeySecurity , generateSecuritySchemes } from './securitySchemes.js'
@@ -587,33 +588,44 @@ const generateGlobalOperations = async (
587588 }
588589}
589590
590- const generateComponents = ( req : Pick < PayloadRequest , 'payload' > ) => {
591+ const generateComponents = (
592+ req : Pick < PayloadRequest , 'payload' > ,
593+ options : SanitizedPluginOptions ,
594+ ) => {
591595 const schemas : Record < string , JSONSchema4 > = {
592596 supportedTimezones : {
593597 type : 'string' ,
594598 example : 'Europe/Prague' ,
595599 } ,
596600 }
597601
598- for ( const collection of Object . values ( req . payload . collections ) ) {
602+ const collections = Object . values ( req . payload . collections ) . filter ( collection =>
603+ shouldIncludeCollection ( collection , options . filters ) ,
604+ )
605+
606+ const globals = req . payload . globals . config . filter ( global =>
607+ shouldIncludeGlobal ( global , options . filters ) ,
608+ )
609+
610+ for ( const collection of collections ) {
599611 const { singular } = collectionName ( collection )
600612 schemas [ componentName ( 'schemas' , singular ) ] = generateSchemaObject (
601613 req . payload . config ,
602614 collection ,
603615 )
604616 }
605617
606- for ( const collection of Object . values ( req . payload . collections ) ) {
618+ for ( const collection of collections ) {
607619 Object . assign ( schemas , generateQueryOperationSchemas ( collection ) )
608620 }
609621
610- for ( const global of req . payload . globals . config ) {
622+ for ( const global of globals ) {
611623 Object . assign ( schemas , generateGlobalSchemas ( req . payload . config , global ) )
612624 }
613625
614626 const requestBodies : Record < string , OpenAPIV3_1 . RequestBodyObject > = { }
615627
616- for ( const collection of Object . values ( req . payload . collections ) ) {
628+ for ( const collection of collections ) {
617629 const { singular } = collectionName ( collection )
618630 requestBodies [ componentName ( 'requestBodies' , singular ) ] = generateRequestBodySchema (
619631 req . payload . config ,
@@ -624,15 +636,15 @@ const generateComponents = (req: Pick<PayloadRequest, 'payload'>) => {
624636 generateRequestBodySchema ( req . payload . config , collection , 'patch' )
625637 }
626638
627- for ( const global of req . payload . globals . config ) {
639+ for ( const global of globals ) {
628640 requestBodies [ componentName ( 'requestBodies' , globalName ( global ) ) ] =
629641 generateGlobalRequestBody ( global )
630642 }
631643
632644 const responses : Record < string , OpenAPIV3_1 . ResponseObject > = Object . assign (
633645 { } ,
634- ...Object . values ( req . payload . collections ) . map ( generateCollectionResponses ) ,
635- ...req . payload . globals . config . map ( global => ( {
646+ ...collections . map ( generateCollectionResponses ) ,
647+ ...globals . map ( global => ( {
636648 [ componentName ( 'responses' , globalName ( global ) ) ] : generateGlobalResponse ( global ) ,
637649 } ) ) ,
638650 )
@@ -644,18 +656,22 @@ export const generateV30Spec = async (
644656 req : Pick < PayloadRequest , 'payload' | 'protocol' | 'headers' > ,
645657 options : SanitizedPluginOptions ,
646658) : Promise < OpenAPIV3 . Document > => {
647- const { schemas, requestBodies, responses } = generateComponents ( req )
659+ const { schemas, requestBodies, responses } = generateComponents ( req , options )
660+
661+ const filters = options . filters ?? { }
662+ const collections = Object . values ( req . payload . collections ) . filter ( collection =>
663+ shouldIncludeCollection ( collection , filters ) ,
664+ )
665+ const globals = req . payload . globals . config . filter ( global => shouldIncludeGlobal ( global , filters ) )
648666
649667 const spec = {
650668 openapi : '3.0.3' ,
651669 info : options . metadata ,
652670 servers : [ { url : `${ req . protocol } //${ req . headers . get ( 'host' ) } ` } ] ,
653671 paths : Object . assign (
654672 { } ,
655- ...( await Promise . all (
656- Object . values ( req . payload . collections ) . map ( generateCollectionOperations ) ,
657- ) ) ,
658- ...( await Promise . all ( req . payload . globals . config . map ( generateGlobalOperations ) ) ) ,
673+ ...( await Promise . all ( collections . map ( generateCollectionOperations ) ) ) ,
674+ ...( await Promise . all ( globals . map ( generateGlobalOperations ) ) ) ,
659675 ) ,
660676 components : {
661677 securitySchemes : generateSecuritySchemes ( options . authEndpoint ) ,
@@ -704,18 +720,22 @@ export const generateV31Spec = async (
704720 req : Pick < PayloadRequest , 'payload' | 'protocol' | 'headers' > ,
705721 options : SanitizedPluginOptions ,
706722) : Promise < OpenAPIV3_1 . Document > => {
707- const { schemas, requestBodies, responses } = generateComponents ( req )
723+ const { schemas, requestBodies, responses } = generateComponents ( req , options )
724+
725+ const filters = options . filters ?? { }
726+ const collections = Object . values ( req . payload . collections ) . filter ( collection =>
727+ shouldIncludeCollection ( collection , filters ) ,
728+ )
729+ const globals = req . payload . globals . config . filter ( global => shouldIncludeGlobal ( global , filters ) )
708730
709731 const spec = {
710732 openapi : '3.1.0' ,
711733 info : options . metadata ,
712734 servers : [ { url : `${ req . protocol } //${ req . headers . get ( 'host' ) } ` } ] ,
713735 paths : Object . assign (
714736 { } ,
715- ...( await Promise . all (
716- Object . values ( req . payload . collections ) . map ( generateCollectionOperations ) ,
717- ) ) ,
718- ...( await Promise . all ( req . payload . globals . config . map ( generateGlobalOperations ) ) ) ,
737+ ...( await Promise . all ( collections . map ( generateCollectionOperations ) ) ) ,
738+ ...( await Promise . all ( globals . map ( generateGlobalOperations ) ) ) ,
719739 ) ,
720740 components : {
721741 securitySchemes : generateSecuritySchemes ( options . authEndpoint ) ,
0 commit comments