@@ -4,6 +4,7 @@ import type { OpenAPIV2 } from "openapi-types";
44import type Router from "find-my-way" ;
55import { get } from "lodash-es" ;
66import qs from "qs" ;
7+ import querystring from "node:querystring" ;
78import multipart from "parse-multipart-data" ;
89
910import type { Interaction } from "../documents/pact" ;
@@ -15,21 +16,27 @@ import {
1516 formatSchemaPath ,
1617} from "../results/index" ;
1718import { minimumSchema , transformRequestSchema } from "../transform/index" ;
19+ import type { Config } from "../utils/config" ;
1820import { isValidRequest } from "../utils/interaction" ;
1921import { dereferenceOas , splitPath } from "../utils/schema" ;
2022import { getValidateFunction } from "../utils/validation" ;
2123import { findMatchingType , getByContentType } from "./utils/content" ;
2224
23- const parseBody = ( body : unknown , contentType : string ) => {
25+ const parseBody = (
26+ body : unknown ,
27+ contentType : string ,
28+ legacyParser : boolean ,
29+ ) => {
2430 if (
2531 contentType . includes ( "application/x-www-form-urlencoded" ) &&
2632 typeof body === "string"
2733 ) {
28- return qs . parse ( body as string , {
29- allowDots : true ,
30- comma : true ,
31- depth : process . env . QUIRKS ? 0 : undefined ,
32- } ) ;
34+ return legacyParser
35+ ? querystring . parse ( body as string )
36+ : qs . parse ( body as string , {
37+ allowDots : true ,
38+ comma : true ,
39+ } ) ;
3340 }
3441
3542 if ( contentType . includes ( "multipart/form-data" ) && typeof body === "string" ) {
@@ -54,13 +61,16 @@ const parseBody = (body: unknown, contentType: string) => {
5461 return body ;
5562} ;
5663
57- const canValidate = ( contentType : string ) : boolean => {
64+ const canValidate = (
65+ contentType : string ,
66+ disableMultipartFormdata : boolean ,
67+ ) : boolean => {
5868 return ! ! findMatchingType (
5969 contentType ,
6070 [
6171 "application/json" ,
6272 "application/x-www-form-urlencoded" ,
63- process . env . QUIRKS ? "" : "multipart/form-data" ,
73+ disableMultipartFormdata ? "" : "multipart/form-data" ,
6474 ] . filter ( Boolean ) ,
6575 ) ;
6676} ;
@@ -72,6 +82,7 @@ export function* compareReqBody(
7282 route : Router . FindResult < Router . HTTPVersion . V1 > ,
7383 interaction : Interaction ,
7484 index : number ,
85+ config : Config ,
7586) : Iterable < Result > {
7687 const { method, oas, operation, path } = route . store ;
7788 const { body } = interaction . request ;
@@ -108,13 +119,17 @@ export function* compareReqBody(
108119
109120 if (
110121 schema &&
111- canValidate ( contentType ) &&
122+ canValidate ( contentType , config . get ( "disable-multipart-formdata" ) ! ) &&
112123 isValidRequest ( interaction ) &&
113- ( process . env . QUIRKS
124+ ( config . get ( "no-validate-request-body-unless-application-json" )
114125 ? ! ! findMatchingType ( "application/json" , availableRequestContentTypes )
115126 : true )
116127 ) {
117- const value = parseBody ( body , requestContentType ) ;
128+ const value = parseBody (
129+ body ,
130+ requestContentType ,
131+ config . get ( "legacy-parser" ) ! ,
132+ ) ;
118133 const schemaId = `[root].paths.${ path } .${ method } .requestBody.content.${ contentType } ` ;
119134 const validate = getValidateFunction ( ajv , schemaId , ( ) =>
120135 transformRequestSchema ( minimumSchema ( schema , oas ) ) ,
@@ -147,7 +162,7 @@ export function* compareReqBody(
147162 ! ! body &&
148163 ! schema &&
149164 isValidRequest ( interaction ) &&
150- ( process . env . QUIRKS
165+ ( config . get ( "no-validate-request-body-unless-application-json" )
151166 ? ! ! findMatchingType ( "application/json" , availableRequestContentTypes ) ||
152167 availableRequestContentTypes . length === 0
153168 : true )
0 commit comments