Skip to content
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

Swagger plugin does not render example for response of array #72

Closed
itsyoboieltr opened this issue Jul 24, 2023 · 5 comments
Closed

Swagger plugin does not render example for response of array #72

itsyoboieltr opened this issue Jul 24, 2023 · 5 comments

Comments

@itsyoboieltr
Copy link

itsyoboieltr commented Jul 24, 2023

Is this the intended behaviour? Arrays can be rendered as examples for the request body, however, for the response it always displays "no example available".

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

const arraySchema = t.Array(t.String());

const app = new Elysia()
  .use(swagger())
  .get('/', () => ['Hello Elysia'], {
    body: arraySchema,
    response: arraySchema,
  })
  .listen(3000);

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

The example response of an object is rendered correctly:

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

const arraySchema = t.Array(t.String());

const app = new Elysia()
  .use(swagger())
  .get('/', () => ({ test: 'Hello Elysia' }), {
    body: arraySchema,
    response: t.Object({ test: t.String() }),
  })
  .listen(3000);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
image
@david-plugge
Copy link

Same issue here, it seems like the schema gets messed up.

{
    "/posts": {
        "get": {
            "responses": {
                "200": {
                    "items": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "format": "uuid",
                                "type": "string"
                            }
                        },
                        "required": ["id"]
                    },
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "array"
                            }
                        }
                    }
                }
            },
            "operationId": "getPosts",
            "description": "Get all posts"
        }
    }
}

I think /posts.get.responses.200.items should live inside /posts.get.responses.200.content.application/json.schema:

{
    "/posts": {
        "get": {
            "responses": {
                "200": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "array",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "id": {
                                            "format": "uuid",
                                            "type": "string"
                                        }
                                    },
                                    "required": ["id"]
                                }
                            }
                        }
                    }
                }
            },
            "operationId": "getPosts",
            "description": "Get all posts"
        }
    }
}

@fecony
Copy link

fecony commented Sep 11, 2023

You can add example of response like this:

const app = new Elysia()
  .use(swagger({}))
  .get('/', () => [1, 2, 3], {
    detail: {
      responses: {
        200: {
          description: 'get numbers',
          content: {
            'application/json': {
              example: [1, 2, 3],
            },
          },
        },
      },
    },
  })

@david-plugge
Copy link

david-plugge commented Sep 11, 2023

Yeah, but the generated schema is still incorrect. Some clients depend on that schema for type generation or other stuff.

*Edit: my bad, it works. But it requires some additional work which i´d rather not do myself

@david-plugge
Copy link

@SaltyAom
Copy link
Member

Sorry for a late response, this is fixed on the latest version.

Screenshot 2567-08-30 at 17 49 42

Thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants