adonis5-swagger icon indicating copy to clipboard operation
adonis5-swagger copied to clipboard

Running in Heroku (Empty swagger.json)

Open D40298008 opened this issue 1 year ago • 5 comments

I stood up a starter Adonis5 application (API) and I'm trying to get it to run in production on Heroku. As a temporary workaround, I hard coded mode: 'RUNTIME' inside of swagger.ts. This seemed to work fine and I could see my one controller's docs just fine. Of course, I have since put this back.

First, I ran into this issue: https://github.com/reg2005/adonis5-swagger/issues/41

  • Commenting out PORT and APP_NAME worked for me (in env.ts).

Then, I ran into this issue: https://github.com/reg2005/adonis5-swagger/issues/33

  • Creating a /docs folder with a /docs/.keep finally made the build successful.

However, now my docs have all disappeared. The swagger page shows this error: No operations defined in spec! and my swagger.json file appears to have been built like this:

{"openapi":"3.0.0","info":{"title":"My Adonis5 App","version":"1.0.0"},"paths":{},"components":{},"tags":[]}

I checked the server and both files were built this way...the one in /docs/swagger.json and then the one that got copied to /build/docs/swagger.json.

As per the instructions (https://github.com/reg2005/adonis5-swagger#production-using) my package.json file has the postbuild script:

"build"     : "node ace build --production",
"postbuild" : "node ace swagger:generate && cp -a docs/ build/docs",

My versions:

adonis5-swagger : 1.4.1
node : 20.2.0

If I run node ace swagger:generate locally, it creates the proper /docs/swagger.json file. Any idea why it's empty in production?

D40298008 avatar May 26 '23 20:05 D40298008

My Swagger.ts file is as follows:

import { SwaggerConfig } from '@ioc:Adonis/Addons/Swagger'

export default {
    uiEnabled   : true,            // Disable or enable SwaggerUI route.
    uiUrl       : 'docs',          // URL path to SwaggerUI.
    specEnabled : true,            // Disable or enable swagger.json route.
    middleware  : [],              // Middlewares array, for protect your swagger docs and spec endpoints.
    specUrl     : '/swagger.json',

    options : {
        definition  : {
            openapi : '3.0.0',
            info : {
                title   : 'My Adonis5 App,
                version : '1.0.0',
            }
        },

        apis : [
            'app/**/*.ts',
            'docs/swagger/**/*.yml',
            'start/routes.ts'
        ],
        basePath : '/'
    },
    mode         : process.env.NODE_ENV === 'production' ? 'PRODUCTION' : 'RUNTIME',
    specFilePath : 'docs/swagger.json',

} as SwaggerConfig

D40298008 avatar May 26 '23 21:05 D40298008

It might be an issue with current working directory in heroku. Please try to check such pattern:

 apis : [
            '**/*.ts'
        ],

VladyslavP98 avatar May 28 '23 13:05 VladyslavP98

And if you have access to build log, you can log additional detailts about your environment. For example:

"postbuild" : "node ace swagger:generate && cp -a docs/ build/docs && ls && cat ./build/docs/swagger.json",

VladyslavP98 avatar May 28 '23 13:05 VladyslavP98

It might be an issue with current working directory in heroku. Please try to check such pattern:

 apis : [
            '**/*.ts'
        ],

Thank you for this! This worked for me.

I'm going to fiddle with the build logging try and see why the original app/**/*.ts wasn't working because I can clearly see that my swagger docs were in my controller app/Controllers/TestController.ts

D40298008 avatar May 30 '23 16:05 D40298008

Hello,

I was using the old recommendation which worked fine, however, it was searching too much and Heroku has introduced some folders owned by root which throw an error trying to search through.

apis : [
    '**/*.ts'
],

My app is always being built from the following folder pattern, but the trailing hash in the folder name changes every time. It might be alphanumeric, but I think it's just hex.

/tmp/build_7bd45f8b

I've tried numerous glob matches (like below) but nothing seems to be working.

apis : [
    process.env.NODE_ENV === 'production' ? '/tmp/build_*/app/**/*.ts' : 'app/**/*.ts',
],

apis : [
    process.env.NODE_ENV === 'production' ? '/tmp/build_[[:alnum:]]+/app/**/*.ts' : 'app/**/*.ts',
],

Assuming my production post-build swagger generate command is being run from / and my app is inside of /tmp/build_7bd4...5f8b, is there a recommended glob for these?

Is posix matching perhaps disabled?

Is there a debug option for the swagger:generate command?

D40298008 avatar Jan 12 '24 00:01 D40298008