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

Error: Model -> Schema - Enum do not work #52

Open
Arthuros120 opened this issue Mar 4, 2024 · 6 comments
Open

Error: Model -> Schema - Enum do not work #52

Arthuros120 opened this issue Mar 4, 2024 · 6 comments

Comments

@Arthuros120
Copy link

Arthuros120 commented Mar 4, 2024

Enum management in models generates an error.

Here's my model:

export default class Model extends BaseModel {
  static selfAssignPrimaryKey = true

  @column({ isPrimary: true })
  // @example('XXXX')
  declare label: string

  @column()
  // @example('XXXX')
  declare otherLabel: string

  @column()
  // @example("Lorem ipsum dolor sit amet")
  declare desc: string | null

  @column()
  // @enum('XX', 'YYY', 'ZZZZZ', 'AAAA', 'BBBB', 'CCCC', 'DDDD'
  // @example('XX')
  declare dirType: EDirType
}

Here's the enum:

export enum EDirType {
  'XX' = 'XX',
  'YYY' = 'YYY',
  'ZZZZZ' = 'ZZZZZ',
  'AAAA' = 'AAAA',
  'BBBB' = 'BBBB',
  'CCCC' = 'CCCC',
  'DDDD' = 'DDDD',
}

Here's the generated diagram:

    Model:
      type: "object"
      properties:
        label:
          type: "string"
          example: "XXX"
        otherLabel:
          type: "string"
          example: "XXXX'"
        desc:
          type: "string"
          example: "\"Lorem ipsum dolor sit amet\""
        dir_type:
          $ref: "#/components/schemas/EDirType"
          example: "'XX'"
      description: "Model"

...

    e_dir_type:
      type: "object"
      properties: {}
      description: "Model"

My swagger.ts configuration:

import path from 'node:path'
import url from 'node:url'

export default {
  path: path.dirname(url.fileURLToPath(import.meta.url)) + '/../',
  title: 'api',
  version: '0.0.0',
  tagIndex: 2,
  snakeCase: true,
  ignore: ['/api/swagger', '/api/docs', '/api/v1/directions', '/api/v1/directions/:id'],
  preferredPutPatch: 'PUT',
  common: {
    parameters: {},
    headers: {},
  },
  persistAuthorization: true,
  showFullPath: false,
}

Open API doesn't understand: "#/components/schemas/EDirType" it returns unresolved reference.

How can I do this without replacing the enum with a string?

I'm getting an error because I'm using Japa's functionality:

export const plugins: Config['plugins'] = [
  assert({
    openApi: {
      schemas: [app.makePath('swagger.yml')],
    },
  }),

Sincerely sorry for the offuscation of the code, hope this issue remains relevant

@ad-on-is
Copy link
Owner

ad-on-is commented Mar 4, 2024

Autoswagger doesn't handle enums yet, but a PR is more then welcome... also when you use @enum(), you MUST NOT use @example() since @enum() uses the first element as example

@Arthuros120
Copy link
Author

Thank you very much for your reply. I really need it, so if I have the time I'll look into it.

@Yanis02015
Copy link

I need it too

@ad-on-is
Copy link
Owner

Where do you guys usually store your enums?

They can literally be all over the place.

Right now AutoSwagger parses everything under models/* as a Model, everything under interfaces/* as an IF and the corresponding /controllers/**/*.ts of a route.

@Yanis02015
Copy link

We could store the enums in the interfaces directly, at worst

@sjorobekov
Copy link

we store them under app/enums

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