ad-on-is / adonis-autoswagger

Auto-Generate swagger docs for AdonisJS
MIT License
129 stars 41 forks source link

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

Open Arthuros120 opened 8 months ago

Arthuros120 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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

Yanis02015 commented 7 months ago

I need it too

ad-on-is commented 7 months ago

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 commented 7 months ago

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

sjorobekov commented 5 months ago

we store them under app/enums