@@ -201,67 +201,66 @@ export function compileOperation(
201201
202202 // Validate the body against the schema
203203 if ( operation . requestBody ) {
204- if ( operation . requestBody . required ) {
205- nodes . push (
206- builders . ifStatement (
207- builders . binaryExpression (
208- '===' ,
209- builders . memberExpression (
210- requestIdentifier ,
211- builders . identifier ( 'body' ) ,
212- ) ,
213- builders . identifier ( 'undefined' ) ,
214- ) ,
215- builders . blockStatement ( [
216- builders . returnStatement ( buildRequestError ( 400 , 'body is required' ) ) ,
217- ] ) ,
218- ) ,
219- ) ;
220- }
221-
222204 const contentTypeSchema = operation . requestBody . content ?. [ 'application/json' ] ?. schema ;
223- if ( contentTypeSchema ) {
224- const bodyFn = compileValueSchema ( compiler , contentTypeSchema ) ;
225- const bodyResult = builders . identifier ( 'body' ) ;
205+ const bodyFn = contentTypeSchema
206+ ? compileValueSchema ( compiler , contentTypeSchema )
207+ : null ;
208+ const bodyResult = builders . identifier ( 'body' ) ;
226209
227- nodes . push (
228- builders . variableDeclaration ( 'const' , [
229- builders . variableDeclarator (
230- bodyResult ,
231- builders . callExpression ( bodyFn , [
232- builders . arrayExpression ( [ builders . literal ( 'body' ) ] ) ,
233- builders . memberExpression (
234- requestIdentifier ,
235- builders . identifier ( 'body' ) ,
236- ) ,
237- ] ) ,
238- ) ,
239- ] ) ,
240- ) ;
241-
242- nodes . push (
243- builders . ifStatement (
244- builders . binaryExpression (
245- 'instanceof' ,
246- bodyResult ,
247- ValidationErrorIdentifier ,
248- ) ,
249- builders . blockStatement ( [ builders . returnStatement ( bodyResult ) ] ) ,
250- builders . blockStatement ( [
251- builders . expressionStatement (
252- builders . assignmentExpression (
253- '=' ,
254- builders . memberExpression (
255- requestIdentifier ,
256- builders . identifier ( 'body' ) ,
257- ) ,
258- bodyResult ,
259- ) ,
260- ) ,
261- ] ) ,
210+ nodes . push (
211+ builders . ifStatement (
212+ builders . binaryExpression (
213+ '===' ,
214+ builders . memberExpression ( requestIdentifier , builders . identifier ( 'body' ) ) ,
215+ builders . identifier ( 'undefined' ) ,
262216 ) ,
263- ) ;
264- }
217+ builders . blockStatement (
218+ operation . requestBody . required
219+ ? [ builders . returnStatement ( buildRequestError ( 400 , 'body is required' ) ) ]
220+ : [ ] ,
221+ ) ,
222+ builders . blockStatement (
223+ bodyFn
224+ ? [
225+ builders . variableDeclaration ( 'const' , [
226+ builders . variableDeclarator (
227+ bodyResult ,
228+ builders . callExpression ( bodyFn , [
229+ builders . arrayExpression ( [ builders . literal ( 'body' ) ] ) ,
230+ builders . memberExpression (
231+ requestIdentifier ,
232+ builders . identifier ( 'body' ) ,
233+ ) ,
234+ ] ) ,
235+ ) ,
236+ ] ) ,
237+ builders . ifStatement (
238+ builders . binaryExpression (
239+ 'instanceof' ,
240+ bodyResult ,
241+ ValidationErrorIdentifier ,
242+ ) ,
243+ builders . blockStatement ( [
244+ builders . returnStatement ( bodyResult ) ,
245+ ] ) ,
246+ builders . blockStatement ( [
247+ builders . expressionStatement (
248+ builders . assignmentExpression (
249+ '=' ,
250+ builders . memberExpression (
251+ requestIdentifier ,
252+ builders . identifier ( 'body' ) ,
253+ ) ,
254+ bodyResult ,
255+ ) ,
256+ ) ,
257+ ] ) ,
258+ ) ,
259+ ]
260+ : [ ] ,
261+ ) ,
262+ ) ,
263+ ) ;
265264 } else {
266265 nodes . push (
267266 builders . ifStatement (
0 commit comments