Open bhuynhdev opened 1 year ago
@sinclairzx81 wdyt?
From some more experimentations and explore, it seems like there is currently zero way to add the encoding
object even without Typebox.
Fastify itself only exposes the schema
object for modification, and there is no other way to add an extra object as the sibling of the schema field. Since encoding
is not standard JSON schema, I kinda understand this situation. Maybe for Swagger/OpenAPI-specific stuff like this, there needs to be some extra option from Fastify-swagger to allow this transformation, like maybe with a decorator, or detecting certain special custom keyword in the schema, and then silently adding an encoding
object before finalizing the OpenAPI spec
A PR is welcomed!
Prerequisites
Issue
I am using Fastify with Typescript through heavy use of Typebox for automatic creation of JSON schema.
For one of my route, I am using "multipart/form-data" as the content type, and I have an array field. In order for Swagger's "Try it out" feature to work with JSON Schema's array validation, I need to specify the "explode: true" option, or else Swagger is just going to send the field as a comma separated string of value, not an actual array (by duplication the field names but with different values in the multipart form data request body)
Typebox allows me to add arbitrary fields to a schema's properties. However, with the new OpenAPI v3 specification, I need the add the "encoding" option in the same depth with the "schema" field, and I don't know how to do it.
Here are examples to illustrate my points:
fastify.post<{ Body: Static }>(
"/test",
{
schema: {
consumes: ["multipart/form-data"],
body: TestBody,
},
},
async function (request) {
console.log(request.body);
return [];
}
);
fastify.post<{ Body: Static }>(
"/test",
{
schema: {
consumes: ["multipart/form-data"],
body: TestBody,
},
},
async function (request) {
console.log(request.body);
return [];
}
);
What option do I have here to implement this feature? I have thought about using the
transform
option in@fastify/swagger
, buttransform
only affect the scheme, so it is still not a way to addencoding
as a sibling of the schema. Are there any options?