From 807e09c9cf2f1f1daec7521fec07b23c981eb3c4 Mon Sep 17 00:00:00 2001
From: Matt Mower <mdmower@cmphys.com>
Date: Wed, 29 Nov 2023 19:41:51 -0800
Subject: [PATCH] Safer handling of multipart nested JSON body props (#878)

If a multipart request body has schema oneOf, anyOf, or allOf, then
automatic parsing of JSON properties throws. An object is expected. Fix
the error today and add a TODO to add support for nested JSON props in
multipart requests that utilize oneOf, anyOf, or allOf.
---
 src/middlewares/openapi.request.validator.ts | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/middlewares/openapi.request.validator.ts b/src/middlewares/openapi.request.validator.ts
index a2d1dc38..d570ab06 100644
--- a/src/middlewares/openapi.request.validator.ts
+++ b/src/middlewares/openapi.request.validator.ts
@@ -206,7 +206,8 @@ export class RequestValidator {
   private multipartNested(req, schemaBody) {
     Object.keys(req.body).forEach((key) => {
       const value = req.body[key];
-      const type = schemaBody?.properties?.body?.properties[key]?.type;
+      // TODO: Add support for oneOf, anyOf, allOf as the body schema
+      const type = schemaBody?.properties?.body?.properties?.[key]?.type;
       if (['array', 'object'].includes(type)) {
         try {
           req.body[key] = JSON.parse(value);
@@ -214,7 +215,7 @@ export class RequestValidator {
           // NOOP
         }
       }
-    })
+    });
     return null;
   }