Skip to content

Instantly share code, notes, and snippets.

@dansp89
Last active July 17, 2023 03:16
Show Gist options
  • Save dansp89/37130ea4c40aee8e303e717fca80694c to your computer and use it in GitHub Desktop.
Save dansp89/37130ea4c40aee8e303e717fca80694c to your computer and use it in GitHub Desktop.
Swagger Authorize Button Error on Strapi 4. After installation it will be fixed and the swagger authorization button will be ready for use.. add the swaggerFix.js file to the root of your strapi project
{
// other here
"scripts": {
"postinstall": "node swaggerFix.js",
// others here
}
}
/**
* Fix bug in Strapi 4
* Swagger Authorize Button Error
* Source: <https://github.com/strapi/strapi/pull/16788/commits/23de7e699cb88de94dbf87c9ea9f265a1415fe7a>
*/
const fs = require('fs');
const path = require('path');
const filePath = path.resolve(__dirname, '../node_modules/@strapi/plugin-documentation/server/services/documentation.js');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const newString = data.replace(
/if \(generatedSchemas\) \{\s+draft\.components = \{\s+schemas: \{ \.\.\.draft\.components\.schemas, \.\.\.generatedSchemas \},\s+\};\s+\}/,
'draft.components.schemas = { ...draft.components.schemas, ...generatedSchemas };'
);
fs.writeFile(filePath, newString, 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
console.log('Fixed with success!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment