@@ -849,6 +849,54 @@ describe('generator', () => {
849849 ] ) ;
850850 } ) ;
851851
852+ test ( 'with defined security' , ( ) => {
853+ const appRouter = t . router ( {
854+ protectedEndpoint : t . procedure
855+ . meta ( {
856+ openapi : { method : 'POST' , path : '/secure/endpoint' , protect : [ 'a' , 'b' ] } ,
857+ } )
858+ . input ( z . object ( { name : z . string ( ) } ) )
859+ . output ( z . object ( { name : z . string ( ) } ) )
860+ . query ( ( { input } ) => ( { name : input . name } ) ) ,
861+ } ) ;
862+
863+ const openApiDocument = generateOpenApiDocument ( appRouter , {
864+ ...defaultDocOpts ,
865+ securitySchemes : {
866+ a : {
867+ type : 'apiKey' ,
868+ name : 'a' ,
869+ in : 'header' ,
870+ } ,
871+ b : {
872+ type : 'apiKey' ,
873+ name : 'b' ,
874+ in : 'header' ,
875+ } ,
876+ } ,
877+ } ) ;
878+
879+ expect ( openApiSchemaValidator . validate ( openApiDocument ) . errors ) . toEqual ( [ ] ) ;
880+ expect ( openApiDocument . paths [ '/secure/endpoint' ] ! . post ! . security ) . toEqual ( [
881+ { a : [ ] } ,
882+ { b : [ ] } ,
883+ ] ) ;
884+ } ) ;
885+
886+ test ( 'with missing securityScheme' , ( ) => {
887+ const appRouter = t . router ( {
888+ protectedEndpoint : t . procedure
889+ . meta ( { openapi : { method : 'POST' , path : '/secure/endpoint' , protect : [ 'a' , 'b' ] } } )
890+ . input ( z . object ( { name : z . string ( ) } ) )
891+ . output ( z . object ( { name : z . string ( ) } ) )
892+ . query ( ( { input } ) => ( { name : input . name } ) ) ,
893+ } ) ;
894+
895+ expect ( ( ) => {
896+ generateOpenApiDocument ( appRouter , defaultDocOpts ) ;
897+ } ) . toThrowError ( '[query.protectedEndpoint] - "a,b" must exists in "securitySchemes"' ) ;
898+ } ) ;
899+
852900 test ( 'with schema descriptions' , ( ) => {
853901 const appRouter = t . router ( {
854902 createUser : t . procedure
@@ -2807,26 +2855,26 @@ describe('generator', () => {
28072855 method : 'GET' ,
28082856 path : '/query-example/{name}' ,
28092857 responseHeaders : {
2810- " X-RateLimit-Limit" : {
2811- description : " Request limit per hour." ,
2858+ ' X-RateLimit-Limit' : {
2859+ description : ' Request limit per hour.' ,
28122860 schema : {
2813- type : " integer"
2814- }
2861+ type : ' integer' ,
2862+ } ,
28152863 } ,
2816- " X-RateLimit-Remaining" : {
2817- description : " The number of requests left for the time window." ,
2864+ ' X-RateLimit-Remaining' : {
2865+ description : ' The number of requests left for the time window.' ,
28182866 schema : {
2819- type : " integer"
2820- }
2821- }
2822- }
2867+ type : ' integer' ,
2868+ } ,
2869+ } ,
2870+ } ,
28232871 } ,
28242872 } )
28252873 . input ( z . object ( { name : z . string ( ) , greeting : z . string ( ) } ) )
28262874 . output ( z . object ( { output : z . string ( ) } ) )
28272875 . query ( ( { input } ) => ( {
28282876 output : `${ input . greeting } ${ input . name } ` ,
2829- } ) )
2877+ } ) ) ,
28302878 } ) ;
28312879
28322880 const openApiDocument = generateOpenApiDocument ( appRouter , defaultDocOpts ) ;
0 commit comments