ad-on-is / adonis-autoswagger

Auto-Generate swagger docs for AdonisJS
MIT License
97 stars 32 forks source link

no such file or directory #56

Closed anthony-bernardo closed 3 months ago

anthony-bernardo commented 3 months ago

I installed the latest adonisJS version, but I got this error :

{"message":"ENOENT: no such file or directory, open '/Users/xero/workspaces/foo-api-adonis/config/../app/UsersController.ts'","name":"Error","status":500,"frames":[]}

The issue is that my UsersController.ts is path app/controllers and not in app path

How to handle that ?


// for AdonisJS v6
import path from 'node:path'
import url from 'node:url'
// ---

export default {
    // path: __dirname + "/../", for AdonisJS v5
    path: path.dirname(url.fileURLToPath(import.meta.url)) + '/../', // for AdonisJS v6
    title: 'Foo',
    version: '1.0.0',
    tagIndex: 2,
    snakeCase: true,
    ignore: ['/swagger', '/docs'],
    preferredPutPatch: 'PUT', // if PUT/PATCH are provided for the same route, prefer PUT
    common: {
        parameters: {}, // OpenAPI conform parameters that are commonly used
        headers: {}, // OpenAPI conform headers that are commonly used
    },
    persistAuthorization: true, // persist authorization between reloads on the swagger page
    showFullPath: false, // the path displayed after endpoint summary
}
import router from '@adonisjs/core/services/router'
import AutoSwagger from 'adonis-autoswagger'
import swagger from '#config/swagger'
import UsersController from '#controllers/UsersController'

// returns swagger in YAML
router.get('/swagger', async () => {
    return AutoSwagger.default.docs(router.toJSON(), swagger)
})

// Renders Swagger-UI and passes YAML-output of /swagger
router.get('/docs', async () => {
    return AutoSwagger.default.ui('/swagger', swagger)
    // return AutoSwagger.default.scalar("/swagger", swagger); to use Scalar instead
    // return AutoSwagger.default.rapidoc("/swagger", swagger); to use RapiDoc instead
})

router.get('/', async () => {
    return {
        hello: 'world',
    }
})

router.get('/users', [UsersController, 'index'])
ad-on-is commented 3 months ago

Can you import your controller like this, since this is also the recommended way in the AdonisJS docs.

const UsersController = () => import ('#controllers/UsersController')
ad-on-is commented 3 months ago

@anthony-bernardo Did my previous reply solve your problem?

HasanAshab commented 3 months ago

I always import my controller like this

const DashboardController = () => import('#controllers/v1/dashboard_controller')

but still getting

"ENOENT: no such file or directory, open 'file:///data/data/com.termux/files/home/js/adonis/boilerplate/config/swagger.ts/../app/controllers/v1/dashboard_controller.ts'"

package.json

  "imports": {
    "#controllers/*": "./app/http/controllers/*.js"
     ...

adonisrc.ts

export default defineConfig({
  directories: {
    httpControllers: 'app/http/controllers',
    //...
ad-on-is commented 3 months ago
"ENOENT: no such file or directory, open 'file:///data/data/com.termux/files/home/js/adonis/boilerplate/config/swagger.ts/../app/controllers/v1/dashboard_controller.ts'"

This line looks very strange to me: there shouldn't be a swagger.ts in the path. Would you mind sharing your swagger.ts file, or comparing it to the docs and see whether you have misconfigured it?

HasanAshab commented 3 months ago

Here is my swagger.ts

import path from "node:path";
import url from "node:url";

export default {
  path: path.dirname(url.fileURLToPath(import.meta.url)) + "/../", // for AdonisJS v6
  title: "Foo",
  version: "1.0.0",
  tagIndex: 2,
  snakeCase: true,
  ignore: ["/swagger", "/docs"],
  preferredPutPatch: "PUT",
  persistAuthorization: true,
  showFullPath: false
};
ad-on-is commented 3 months ago
"ENOENT: no such file or directory, open 'file:///data/data/com.termux/files/home/js/adonis/boilerplate/config/swagger.ts/../app/controllers/v1/dashboard_controller.ts'"

This line looks very strange to me: there shouldn't be a swagger.ts in the path. Would you mind sharing your swagger.ts file, or comparing it to the docs and see whether you have misconfigured it?

Yeah, I can reproduce. Moving the controllers to app/http/ and pointing them in package.json doesn't work. I'd have to parse the package.json for that.

HasanAshab commented 3 months ago

I also have pointed that in adonisrc.ts under directories

HasanAshab commented 3 months ago

Any update??

ad-on-is commented 3 months ago

Any update??

Not yet, I need some time to fix it. PRs are always welcome, if you have the time.

ad-on-is commented 3 months ago

Done in version 3.0