@@ -222,6 +222,183 @@ describe('getBody', () => {
222222 expect ( result . isOptional ) . toBe ( true ) ;
223223 } ) ;
224224
225+ describe ( 'isBlob detection' , ( ) => {
226+ it . each ( [
227+ [ 'application/octet-stream' ] ,
228+ [ 'application/pdf' ] ,
229+ [ 'application/zip' ] ,
230+ ] ) ( 'sets isBlob to true for %s request body' , ( binaryApplicationType ) => {
231+ const requestBody : OpenApiRequestBodyObject = {
232+ content : {
233+ [ binaryApplicationType ] : {
234+ schema : { type : 'string' , format : 'binary' } ,
235+ } ,
236+ } ,
237+ required : true ,
238+ } ;
239+
240+ const result = getBody ( {
241+ requestBody,
242+ operationName : 'uploadFile' ,
243+ context : createContext ( ) ,
244+ } ) ;
245+
246+ expect ( result . isBlob ) . toBe ( true ) ;
247+ } ) ;
248+
249+ it ( 'sets isBlob to false for application/json request body' , ( ) => {
250+ const requestBody : OpenApiRequestBodyObject = {
251+ content : {
252+ 'application/json' : {
253+ schema : {
254+ type : 'object' ,
255+ properties : { name : { type : 'string' } } ,
256+ } ,
257+ } ,
258+ } ,
259+ required : true ,
260+ } ;
261+
262+ const result = getBody ( {
263+ requestBody,
264+ operationName : 'createPet' ,
265+ context : createContext ( ) ,
266+ } ) ;
267+
268+ expect ( result . isBlob ) . toBe ( false ) ;
269+ } ) ;
270+
271+ it ( 'sets isBlob to true for text/csv with format: binary schema' , ( ) => {
272+ const requestBody : OpenApiRequestBodyObject = {
273+ content : {
274+ 'text/csv' : {
275+ schema : { type : 'string' , format : 'binary' } ,
276+ } ,
277+ } ,
278+ required : true ,
279+ } ;
280+
281+ const result = getBody ( {
282+ requestBody,
283+ operationName : 'uploadCsv' ,
284+ context : createContext ( ) ,
285+ } ) ;
286+
287+ expect ( result . isBlob ) . toBe ( true ) ;
288+ } ) ;
289+
290+ it ( 'sets isBlob to true for image content type' , ( ) => {
291+ const requestBody : OpenApiRequestBodyObject = {
292+ content : {
293+ 'image/png' : {
294+ schema : { type : 'string' , format : 'binary' } ,
295+ } ,
296+ } ,
297+ required : true ,
298+ } ;
299+
300+ const result = getBody ( {
301+ requestBody,
302+ operationName : 'uploadImage' ,
303+ context : createContext ( ) ,
304+ } ) ;
305+
306+ expect ( result . isBlob ) . toBe ( true ) ;
307+ } ) ;
308+
309+ it ( 'sets isBlob to true for */* with inline format: binary schema' , ( ) => {
310+ const requestBody : OpenApiRequestBodyObject = {
311+ content : {
312+ '*/*' : {
313+ schema : { type : 'string' , format : 'binary' } ,
314+ } ,
315+ } ,
316+ required : true ,
317+ } ;
318+
319+ const result = getBody ( {
320+ requestBody,
321+ operationName : 'uploadFile' ,
322+ context : createContext ( ) ,
323+ } ) ;
324+
325+ expect ( result . isBlob ) . toBe ( true ) ;
326+ } ) ;
327+
328+ it ( 'sets isBlob to true for $ref to a format: binary schema' , ( ) => {
329+ const context = createContext ( ) ;
330+ context . spec . components = {
331+ ...context . spec . components ,
332+ schemas : {
333+ ...context . spec . components ?. schemas ,
334+ TestPdfFile : { type : 'string' , format : 'binary' } ,
335+ } ,
336+ } ;
337+
338+ const requestBody : OpenApiRequestBodyObject = {
339+ content : {
340+ '*/*' : {
341+ schema : { $ref : '#/components/schemas/TestPdfFile' } ,
342+ } ,
343+ } ,
344+ required : true ,
345+ } ;
346+
347+ const result = getBody ( {
348+ requestBody,
349+ operationName : 'uploadFile' ,
350+ context,
351+ } ) ;
352+
353+ expect ( result . isBlob ) . toBe ( true ) ;
354+ } ) ;
355+
356+ it ( 'sets isBlob to true for schema with contentMediaType: application/octet-stream' , ( ) => {
357+ const requestBody : OpenApiRequestBodyObject = {
358+ content : {
359+ 'application/json' : {
360+ schema : {
361+ type : 'string' ,
362+ contentMediaType : 'application/octet-stream' ,
363+ } ,
364+ } ,
365+ } ,
366+ required : true ,
367+ } ;
368+
369+ const result = getBody ( {
370+ requestBody,
371+ operationName : 'uploadFile' ,
372+ context : createContext ( ) ,
373+ } ) ;
374+
375+ expect ( result . isBlob ) . toBe ( true ) ;
376+ } ) ;
377+
378+ it ( 'sets isBlob to false when contentMediaType has contentEncoding (base64-encoded string)' , ( ) => {
379+ const requestBody : OpenApiRequestBodyObject = {
380+ content : {
381+ 'application/json' : {
382+ schema : {
383+ type : 'string' ,
384+ contentMediaType : 'application/octet-stream' ,
385+ contentEncoding : 'base64' ,
386+ } ,
387+ } ,
388+ } ,
389+ required : true ,
390+ } ;
391+
392+ const result = getBody ( {
393+ requestBody,
394+ operationName : 'uploadFile' ,
395+ context : createContext ( ) ,
396+ } ) ;
397+
398+ expect ( result . isBlob ) . toBe ( false ) ;
399+ } ) ;
400+ } ) ;
401+
225402 describe ( 'x-codegen-request-body-name' , ( ) => {
226403 it ( 'uses custom name from inline request body extension' , ( ) => {
227404 const result = getBody ( {
0 commit comments