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

Enums aren't generated to OAS/Swagger Specification #90

Open
marclave opened this issue Jan 9, 2024 · 3 comments
Open

Enums aren't generated to OAS/Swagger Specification #90

marclave opened this issue Jan 9, 2024 · 3 comments

Comments

@marclave
Copy link
Collaborator

marclave commented Jan 9, 2024

I might be doing something wrong, but when i make an endpoint with an enum in the body i don't get the enum in the swagger file

import { Elysia, t } from 'elysia'

export const plugin = new Elysia({
    prefix: '/a'
})
    .post('/hello-world', ({ body }) => body, {
        body: t.Object({
            type: t.Enum({
                MOD: 'mod_skin',
                VIP: 'vip_skin',
            })
        }),
    })

the output is

"requestBody": {
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "type": {
                  "anyOf": [
                    {
                      "const": "mod_skin",
                      "type": "string"
                    },
                    {
                      "const": "vip_skin",
                      "type": "string"
                    },

vs

schema:
            type: string
            enum: [asc, desc]

so the rendered output is either

Scalar
image
Swagger UI
image

let me know if it's something I can change or if it's something we need to add to the swagger generation code ✨

@Nmans01
Copy link

Nmans01 commented Jan 10, 2024

+1, I've just noticed the same.
image

@rottmann
Copy link

Same with Union and Literal.

@nxht
Copy link
Contributor

nxht commented May 23, 2024

As it seems Typebox won't implement OpenAPI compatible schema, I think it'd be good to have custom Typebox schema for enum in elysia, or elysia-swagger.

For example, here's the way I'm currently handling enums.

import type { StringOptions, TSchema } from '@sinclair/typebox';
import { Kind, TypeGuard, TypeRegistry } from '@sinclair/typebox';

export type StringEnumOption = StringOptions;

export type TStringEnum = TSchema &
  StringOptions & {
    [Kind]: 'StringEnum';
    static: string;
    type: 'string';
    enum: string[];
  };

export const tStringEnum = (
  enumStrings: string[],
  options: StringEnumOption = {},
): TStringEnum => {
  const { ...stringOptions } = options;

  return {
    ...stringOptions,
    enum: enumStrings,
    [Kind]: 'StringEnum',
    type: 'string',
    error: `Invalid enum value. Expected: ['${enumStrings.join("','")}']`,
  } as TStringEnum;
};

TypeRegistry.Set<TStringEnum>('StringEnum', (schema, value) => {
  if (typeof value !== 'string') return false;

  if (schema.enum.includes(value)) {
    return true;
  }
  return false;
});

Usage example

const YN = tStringEnum(['Y', 'N'], { maxLength: 1 })

And the result schema

{
  "YN": {
    "maxLength": 1,
    "enum": [
      "Y",
      "N"
    ],
    "type": "string"
  }
}

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