Skip to content

Restrict contentType for request body #192

Open
@DeluxeOwl

Description

@DeluxeOwl

I'm using the example from https://elysiajs.com/eden/treaty/parameters.html#file-upload

import { Elysia, t } from "elysia";
import swagger from "@elysiajs/swagger";

const app = new Elysia()
	.post(
		"/image",
		({ body: { image, title } }) => {
			console.info(title, image);
		},
		{
			body: t.Object({
				title: t.String(),
				image: t.Files(),
			}),
		}
	)
	.use(swagger())
	.listen(3001);

console.log(`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}/swagger`);

How can I restrict the openapi content type to just multipart/form-data? Note: I tried parse: "multipart/form-data" and nothing changed

Image

Currently it generates the following json, notice the content below

{
  "openapi": "3.0.3",
  "info": {
    "title": "Elysia Documentation",
    "description": "Development documentation",
    "version": "0.0.0"
  },
  "paths": {
    "/image": {
      "post": {
        "parameters": [],
        "operationId": "postImage",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string"
                  },
                  "image": {
                    "elysiaMeta": "Files",
                    "default": "Files",
                    "type": "array",
                    "items": {
                      "default": "Files",
                      "type": "string",
                      "format": "binary"
                    }
                  }
                },
                "required": [
                  "title",
                  "image"
                ]
              }
            },
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string"
                  },
                  "image": {
                    "elysiaMeta": "Files",
                    "default": "Files",
                    "type": "array",
                    "items": {
                      "default": "Files",
                      "type": "string",
                      "format": "binary"
                    }
                  }
                },
                "required": [
                  "title",
                  "image"
                ]
              }
            },
            "text/plain": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string"
                  },
                  "image": {
                    "elysiaMeta": "Files",
                    "default": "Files",
                    "type": "array",
                    "items": {
                      "default": "Files",
                      "type": "string",
                      "format": "binary"
                    }
                  }
                },
                "required": [
                  "title",
                  "image"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {}
        }
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

Found the issue

elysia-swagger/src/utils.ts

Lines 154 to 158 in 65b20fc

const contentType = hook?.type ?? [
'application/json',
'multipart/form-data',
'text/plain'
]

The issue is that the type doesn't exist on LocalHook anymore, it was replaced by parse

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions