✅ Raw request content types #869
Replies: 7 comments 2 replies
-
Hello @master-chu , thank you for the very detailed problem statement. |
Beta Was this translation helpful? Give feedback.
-
The current implementation on determining the MIME types of the Endpoint (src/endpoint.ts) this.mimeTypes = {
input: hasUpload(inputSchema) ? [mimeMultipart] : [mimeJson], // <—— this line
positive: getMimeTypesFromApiResponse(apiResponse.positive),
negative: getMimeTypesFromApiResponse(apiResponse.negative),
}; |
Beta Was this translation helpful? Give feedback.
-
The problem, however, is that the entire body is the file, not its property, like when having uploads supplied using |
Beta Was this translation helpful? Give feedback.
-
@master-chu , consider this: const app = express();
/** @link https://expressjs.com/en/4x/api.html#express.raw */
app.use(express.raw()); // default type is application/octet-stream
// custom middleware that places the raw data into body.data
app.use((req, res, next) => {
if (req.is("application/octet-stream")) {
req.body = { data: req.body }
}
next();
});
// custom express app approach
attachRouting(config, app); // express-zod-api
On that I'll have to think a bit more. |
Beta Was this translation helpful? Give feedback.
-
I think I could make a config option to:
That's the idea. Should be clean enough. |
Beta Was this translation helpful? Give feedback.
-
Check this out, @master-chu : #1281 |
Beta Was this translation helpful? Give feedback.
-
🚀 The feature released in v14.2.0, @master-chu |
Beta Was this translation helpful? Give feedback.
-
Problem
Currently it looks like the only supported request (input) content types are
application/json
andmultipart/form-data
. My understanding is that if the input schema contains az.upload()
, then it will be form data, otherwise JSON.It would be great if arbitrary types could be specified. Express supports this with their
raw
middleware. I have an endpoint that acceptsapplication/octet-stream
data as the entire request body, and we were actually able to make this work withexpress-api-zod
by intercepting theresponse.body
object and forwarding it to the handler function. However, in the generated OpenApi docs, the content type is still specified asapplication/json
, which makes it impossible to actually call from the Swagger UI.Example
Let's say I have an endpoint called like
POST /my-endpoint/1
In plain express, the endpoint would look like:
And in
express-zod-api
it looks like:This works fine, but as I mentioned the generated spec ends up looking like this:
where
application/json
ought to beapplication/octet-stream
, which changes the interface Swagger gives to test the endpoint.Proposal
I don't have a great solution for how to support this, but one possible approach would be similar to the
z.upload
pattern:But since path params and request body are typically merged, I don't really know what the resulting
{ input }
object should look like if you have both. Alternatively, if the intended content type could be inferred from the inclusion of a custom middleware, that would allow them to be separated easily.Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions