@@ -849,6 +849,54 @@ describe('generator', () => {
849
849
] ) ;
850
850
} ) ;
851
851
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
+
852
900
test ( 'with schema descriptions' , ( ) => {
853
901
const appRouter = t . router ( {
854
902
createUser : t . procedure
@@ -2807,26 +2855,26 @@ describe('generator', () => {
2807
2855
method : 'GET' ,
2808
2856
path : '/query-example/{name}' ,
2809
2857
responseHeaders : {
2810
- " X-RateLimit-Limit" : {
2811
- description : " Request limit per hour." ,
2858
+ ' X-RateLimit-Limit' : {
2859
+ description : ' Request limit per hour.' ,
2812
2860
schema : {
2813
- type : " integer"
2814
- }
2861
+ type : ' integer' ,
2862
+ } ,
2815
2863
} ,
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.' ,
2818
2866
schema : {
2819
- type : " integer"
2820
- }
2821
- }
2822
- }
2867
+ type : ' integer' ,
2868
+ } ,
2869
+ } ,
2870
+ } ,
2823
2871
} ,
2824
2872
} )
2825
2873
. input ( z . object ( { name : z . string ( ) , greeting : z . string ( ) } ) )
2826
2874
. output ( z . object ( { output : z . string ( ) } ) )
2827
2875
. query ( ( { input } ) => ( {
2828
2876
output : `${ input . greeting } ${ input . name } ` ,
2829
- } ) )
2877
+ } ) ) ,
2830
2878
} ) ;
2831
2879
2832
2880
const openApiDocument = generateOpenApiDocument ( appRouter , defaultDocOpts ) ;
0 commit comments