-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elysia schema creates malformed swagger schema #8
Comments
The issue is still there and it currently stops us from switching to elysia as we have some clients that consume the openapi schema. const app = new Elysia().post(
'/test',
({ body }) => {
return body.map(String);
},
{
type: 'json',
body: t.Array(t.Number()),
response: {
200: t.Array(t.String()),
}
},
); {
"/test": {
"post": {
"parameters": [],
"responses": {
"200": {
"items": {
"type": "string"
},
"content": {
"application/json": {
"schema": {
"type": "array"
}
}
}
}
},
"operationId": "postTest",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "number"
}
}
}
}
}
}
}
} |
@david-plugge you need to declare the response in case of 200. app.get("/api", () => ({ id: 666, name: "John Wick" }), {
response: {
200: t.Object({
id: t.Number({
description: "Unique identifier.",
default: 666,
}),
name: t.String({
description: "Name of mercenary.",
default: "John Wick",
}),
}),
},
detail: {
summary: `Gets details of all available mercenaries.`,
tags: ["Mercenaries"],
},
}); Edit: Wrapped the schema properties inside the |
@igrschmidt that results in the same incorrect schema |
@david-plugge I'm sorry, I gave the wrong example. We need to wrap the schema properties inside the response: {
200: t.Object({
id: t.Number({
description: "Unique identifier.",
default: 666,
}),
name: t.String({
description: "Name of mercenary.",
default: "John Wick",
}),
}),
} This should work now |
@igrschmidt that works unless you want to return an array: response: {
200: t.Array(
t.Object({
id: t.Number({
description: "Unique identifier.",
default: 666,
}),
name: t.String({
description: "Name of mercenary.",
default: "John Wick",
}),
}),
),
} |
I created a minimal reproduction so you can check it out yourself. |
@SaltyAom Please consider this... Arrays are crucial! |
@BE-CH, @david-plugge, @KilianB I put together a workaround until the pull request is merged (via #39) For someone looking to return an array and to control the response description as well as the other parts of the schema (like marking query parameters as not required), here is an example of my approach:
This yields the following Swagger results: |
Ola,
I want to make a nice swagger schema view of my api. While the @elysia/swagger plugin gets there half of the way, it seems to struggle with schemas. I've dug through all the examples, but none seem to mix params and responses together, or the examples are outdated. (Like the readme of this repo). I've tried different ways of creating layouts for the schemas, but non of them seem to work properly.
I've got an Elysia route set up like the following:
Then I run the route like this:
My swagger starts up successfully, but some functionality is broken. I can not inspect the schema, and can not log into my oauth.
I threw the generated swagger schema into insomnia to inspect any errors, it came out with the following:
❌ Elysia generated
Fixes
✅ Error 1
I have an API key in my header, I have defined my swagger schema as such:
This however does not show up in the schema. The schema stays empty.
✅ Error 2
The first error seems to be caused by not putting "type: number" in a schema object:
✅ Error 3
This error seems to be caused by the response schema directly being in the 200 object, instead of inside the content objects.
With these manual fixes, I end up with the schema view that I want:
I hope this post clearly outlines the issues I am facing with the swagger plugin. If there is any context missing, please let me know.
Please don't be another stupid oversight by myself🙏
The text was updated successfully, but these errors were encountered: