feat: Support mapping repeated keys to array in form-data #2654
+190
−22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds the support of mapping repeated form keys into arrays for validation in multipart form-data and url-encoded form-data. Using repeated keys to fulfill arrays in form-data is nowadays a popular workaround whenever an array is needed inside form-data (reference, reference). In the old version of Prism, repeated keys will be treated as overriding previous values, thus failing the validation.
Checklist
Additional context
I discovered this need when working with Stainless auto-generated SDK, where Prism is used to test the SDK. Whenever I have an array in multipart form-data, Prism simply fails with 422, but the same input easily passes with real server. Thus, I dug into the workarounds online and realized that repeating keys is an untold standard in accomplishing this need. OpenAI happens to have the exact same awkward situation with Stainless + Prism, and there is no workaround online for mock server like this, thus I decided to make Prism support it.
Interesting side note: OpenAI's library tests were luckily passing because their field is optional, and the key name is mismatched in their OpenAPI schema, thus Prism simply thought that the key was not provided and treated it as valid request. OpenAI's SDK (and per API reference) passes
timestamp_granularities[]
into the Prism while havingtimestamp_granularities
as key name in the Stainless-hosted OpenAPI schema, so Prism thinks it is a valid request, but it is actually being untested. In my use case, my array field needs to be required, so I end up needing to find a workaround more seriously. With this patch, OpenAI's SDKs could then actually be tested against their actual OpenAPI schema (withtimestamp_granularities[]
as key name).