@@ -103,6 +103,79 @@ describe("OpenAPI 3.0 schema patterns", () => {
103103 } ) ;
104104 } ) ;
105105
106+ describe ( "OpenAPI 3.1 response refs with apiKey security" , ( ) => {
107+ const schema = {
108+ openapi : "3.1.0" ,
109+ info : { title : "Xquik API" , version : "1.0" } ,
110+ paths : {
111+ "/api/v1/x/tweets/search" : {
112+ get : {
113+ operationId : "searchTweets" ,
114+ parameters : [
115+ {
116+ name : "q" ,
117+ in : "query" ,
118+ required : true ,
119+ schema : { type : "string" } ,
120+ } ,
121+ ] ,
122+ responses : {
123+ "200" : {
124+ description : "Search results" ,
125+ content : {
126+ "application/json" : {
127+ schema : { $ref : "#/components/schemas/TweetSearchResponse" } ,
128+ } ,
129+ } ,
130+ } ,
131+ } ,
132+ security : [ { apiKey : [ ] } ] ,
133+ } ,
134+ } ,
135+ } ,
136+ components : {
137+ securitySchemes : {
138+ apiKey : {
139+ type : "apiKey" ,
140+ in : "header" ,
141+ name : "x-api-key" ,
142+ } ,
143+ } ,
144+ schemas : {
145+ TweetSearchResponse : {
146+ type : "object" ,
147+ properties : {
148+ data : {
149+ type : "array" ,
150+ items : { $ref : "#/components/schemas/Tweet" } ,
151+ } ,
152+ } ,
153+ } ,
154+ Tweet : {
155+ type : "object" ,
156+ properties : {
157+ id : { type : "string" } ,
158+ text : { type : "string" } ,
159+ } ,
160+ } ,
161+ } ,
162+ } ,
163+ } ;
164+
165+ it ( "should dereference nested response schemas and preserve apiKey metadata" , async ( ) => {
166+ const parser = new $RefParser ( ) ;
167+ const result = await parser . dereference ( structuredClone ( schema ) ) ;
168+
169+ const responseSchema = ( result as any ) . paths [ "/api/v1/x/tweets/search" ] . get . responses [ "200" ] . content [
170+ "application/json"
171+ ] . schema ;
172+ expect ( responseSchema ) . to . equal ( ( result as any ) . components . schemas . TweetSearchResponse ) ;
173+ expect ( responseSchema . properties . data . items ) . to . equal ( ( result as any ) . components . schemas . Tweet ) ;
174+ expect ( ( result as any ) . components . securitySchemes . apiKey . name ) . to . equal ( "x-api-key" ) ;
175+ expect ( parser . $refs . circular ) . to . equal ( false ) ;
176+ } ) ;
177+ } ) ;
178+
106179 describe ( "oneOf discriminator pattern" , ( ) => {
107180 const schema = {
108181 type : "object" ,
@@ -200,16 +273,10 @@ describe("OpenAPI 3.0 schema patterns", () => {
200273 type : "object" ,
201274 properties : {
202275 owner : {
203- anyOf : [
204- { $ref : "#/definitions/Person" } ,
205- { type : "null" } ,
206- ] ,
276+ anyOf : [ { $ref : "#/definitions/Person" } , { type : "null" } ] ,
207277 } ,
208278 address : {
209- anyOf : [
210- { $ref : "#/definitions/Address" } ,
211- { type : "null" } ,
212- ] ,
279+ anyOf : [ { $ref : "#/definitions/Address" } , { type : "null" } ] ,
213280 } ,
214281 } ,
215282 definitions : {
@@ -218,10 +285,7 @@ describe("OpenAPI 3.0 schema patterns", () => {
218285 properties : {
219286 name : { type : "string" } ,
220287 address : {
221- anyOf : [
222- { $ref : "#/definitions/Address" } ,
223- { type : "null" } ,
224- ] ,
288+ anyOf : [ { $ref : "#/definitions/Address" } , { type : "null" } ] ,
225289 } ,
226290 } ,
227291 } ,
0 commit comments