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

Not able to access endpoints for openapi.json and swagger.json #10734

Closed
ShubjeetPal opened this issue Aug 13, 2021 · 19 comments
Closed

Not able to access endpoints for openapi.json and swagger.json #10734

ShubjeetPal opened this issue Aug 13, 2021 · 19 comments
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature source: plugin:documentation Source is plugin/documentation package

Comments

@ShubjeetPal
Copy link

ShubjeetPal commented Aug 13, 2021

Bug report

Not able to access endpoints for openapi.json and swagger.json

I am deploying Strapi v3.6.6 from my local (windows 2019 vm) to Azure App Service using Azure DevOps
I want to use the openapi spec endpoint to setup Strapi APIs in Azure API Management. These internal APIs will then be used by an Azure Static Web App frontend.
Before Setting up APIs in Azure API Management, I want to view the openapi.json and swagger.json through 'Public' role and then I will setup authentication for APIs
But I am not able to access endpoints for openapi.json and swagger.json from localhost.

I am only able to setup APIs in API manager by manually uploading the file full_documentation.json. I want to use the endpoint as it will always give the updated/ regenerated json whenever model is updated.

Steps to reproduce the behavior

As per documentation https://strapi.io/documentation/developer-docs/latest/development/plugins/documentation.html#swagger-json-and-openapi-json-files
the openapi.json and swagger.json should be accessible. But when I try the url on localhost
http://localhost:1337/documentation/v1.0.0/openapi.json
I get a 404-not found error.
I do not see any permissions to set under setting-> users and permissions plugins -> roles -> public.

Expected behavior

As per Strapi Documentation as given below it should be accessible.
If the default /documentation base URL is used, the endpoints exposed are:

  • /documentation/swagger.json or /documentation/openapi.json: the OpenAPI spec for the latest version of your API. Both endpoints serve the same document.
  • /documentation/vN.N.N/swagger.json or /documentation/vN.N.N/openapi.json: request the spec for a specific version of your API. For example: /documentation/v1.0.0/openapi.json

Screenshots

Code snippets

System

  • Node.js version: 14.17.4
  • NPM version: 6.14.14
  • Strapi version: 3.6.6
  • Database: postgres (PostgreSQL client for Node.js. pg 8.7.1)
  • Operating system: Local on Azure vm (Windows 2019 server)
  • strapi-plugin-documentation: 3.6.6

Additional context

Am I missing something?
Is this a bug?
Can you give an example how to access the endpoints for openapi.json and swagger.json.

@derrickmehaffy
Copy link
Member

This is a templated message

Hello, please follow the issue template.

A proper issue submission let's us better understand the origin of your bug and therefore help you.

I will reopen your issue when we receive the issue following the template guidelines and properly fill out the template. You can see the template guidelines for bug reports here.

Please update the issue with the template and we can reopen this report.

Thank you.

@ShubjeetPal
Copy link
Author

Hello, I have updated the issue following the template.
Please reopen this report

@hondem
Copy link

hondem commented Aug 14, 2021

Please can we reopen this? Have the same issue now.

@ShubjeetPal
Copy link
Author

Please can we reopen this? Have the same issue now.

Is this an existing issue?

@derrickmehaffy derrickmehaffy added the status: pending reproduction Waiting for free time to reproduce the issue, or more information label Aug 16, 2021
@hondem
Copy link

hondem commented Aug 17, 2021

Please can we reopen this? Have the same issue now.

Is this an existing issue?

Yes, it is.

@ShubjeetPal
Copy link
Author

Please can we reopen this? Have the same issue now.

Is this an existing issue?

Yes, it is.

Is there any resolution or any workaround for it currently

@Kraudia
Copy link

Kraudia commented Aug 19, 2021

I can confirm this issue. I have enabled all role permissions for documentation plugs but index permission just enables access to GET /documentation and /documentation/v:major(\d+).:minor(\d+).:patch(\d+).
image

Nothing happened, I am still getting 404 error during enetring http://localhost:1337/documentation/v1.0.0/openapi.json or http://localhost:1337/documentation/openapi.json.

I am afraid that the docs are outdated as I am not able to access those json files (openapi.json or swagger.json) because I really can't find any route that would serve them up.

I think workaround for it is implementation of own custom controller that returns /extensions/documentation/documentation/1.0.0/full_documentation.json file.

@MarkKragerup
Copy link

MarkKragerup commented Aug 19, 2021

It currently seems that the only way to serve the json file as of right now, is through a custom controller. I can try to create a pull request for it later.

Here is how to go about it:
Create 2 folders in /api, called /swaggerjson/config and /swaggerjson/controllers.

In swaggerjson/config create a routes.json file and put the following code:

{ "routes": [ { "method": "GET", "path": "/swagger.json", "handler": "Swaggerjson.index" } ] }

In the /swaggerjson/controllers create a Swaggerjson.js file, and put the following code:

const fs = require("fs");
module.exports = {
  index: async ctx => fs.readFileSync('extensions/documentation/documentation/1.0.0/full_documentation.json', 'utf8')
};

You should now be able to access https://yourapi/swagger.json.

@Kraudia
Copy link

Kraudia commented Aug 19, 2021

I have just created a similar workaround to @MarkKragerup solution but I implemented two endpoints as I wanted to use documentation's version as param.

You would also need to send authorization header (use JWT token as Bearer) to access above endpoints.
Please create two new files:

app/api/docs/config/routes.json

{
  "routes": [
    {
      "method": "GET",
      "path": "/docs/openapi.json",
      "handler": "docs.index",
      "config": {
        "policies": [
          "plugins::documentation.index"
        ]
      }
    },
    {
      "method": "GET",
      "path": "/docs/v:major(\\d+).:minor(\\d+).:patch(\\d+)/openapi.json",
      "handler": "docs.index",
      "config": {
        "policies": [
          "plugins::documentation.index"
        ]
      }
    }
  ]
}

app/api/docs/controllers/docs.js

'use strict';

const fs = require('fs-extra');
const path = require('path');

module.exports = {
  index: async (ctx) => {
    try {
      const { major, minor, patch } = ctx.params;
      const version =
        major && minor && patch
          ? `${major}.${minor}.${patch}`
          : strapi.plugins.documentation.config.info.version;

      const openAPISpecsPath = path.join(
        strapi.config.appPath,
        'extensions',
        'documentation',
        'documentation',
        version,
        'full_documentation.json',
      );

      const documentation = fs.readFileSync(openAPISpecsPath, 'utf8');
      const response = JSON.parse(documentation);

      ctx.send(response);
    } catch (e) {
      strapi.log.error(e);
      ctx.badRequest(null, e.message);
    }
  },
};

It should work.

@MarkKragerup
Copy link

There is a pending PR on this issue - and a merged documentation PR. This explains why the documentation is incorrectly claiming it works - the base PR needs merging.

Base PR:
#8486

Doc PR:
strapi/documentation#351

@derrickmehaffy derrickmehaffy added issue: feature request Issue suggesting a new feature and removed status: pending reproduction Waiting for free time to reproduce the issue, or more information labels Jan 5, 2022
@derrickmehaffy derrickmehaffy added this to To be reviewed (Open) in Developer Experience - Old via automation Jan 5, 2022
@derrickmehaffy derrickmehaffy added the source: plugin:documentation Source is plugin/documentation package label Jan 5, 2022
@derrickmehaffy derrickmehaffy linked a pull request Jan 5, 2022 that will close this issue
@derrickmehaffy derrickmehaffy added issue: enhancement Issue suggesting an enhancement to an existing feature and removed issue: feature request Issue suggesting a new feature labels May 6, 2022
@derrickmehaffy derrickmehaffy removed this from To be reviewed (Open) in Developer Experience - Old May 6, 2022
@tommy92
Copy link

tommy92 commented Jun 30, 2022

I'm using Strapi v4.1.12 and the documentation plugin in v4.2.2.

There is no way to access any of these:

  • /documentation/v1.0.0/swagger.json
  • /documentation/v1.0.0/openapi.json
  • /documentation/swagger.json
  • /documentation/openapi.json

Any ideas how to make it work?

@roelbeerens
Copy link
Contributor

This is what I did:

Add src/extensions/documentation/controllers/documentation.js with the following content:

'use strict';
const fs = require('fs-extra');
const path = require('path');
module.exports = {
  async openapi(ctx) {
    try {
      const { major, minor, patch } = ctx.params;
      const version =
        major && minor && patch ? `${major}.${minor}.${patch}` : strapi.plugins.documentation.config('info.version');
      const openAPISpecsPath = path.join(
        strapi.dirs.app.src,
        'extensions',
        'documentation',
        'documentation',
        version,
        'full_documentation.json'
      );
      const documentation = fs.readFileSync(openAPISpecsPath, 'utf8');
      const response = JSON.parse(documentation);
      ctx.send(response);
    } catch (e) {
      strapi.log.error(e);
      ctx.badRequest(null, e.message);
    }
  }
};

Add src/extensions/documentation/routes/custom-documentation.js with the following content:

'use strict';
const restrictAccess = require('@strapi/plugin-documentation/server/middlewares/restrict-access');
/**
 * company router.
 */
module.exports = {
  routes: [
    {
      method: 'GET',
      path: '/openapi.json',
      handler: 'documentation.openapi',
      config: {
        auth: false,
        middlewares: [restrictAccess]
      }
    },
    {
      method: 'GET',
      path: '/v:major(\\d+).:minor(\\d+).:patch(\\d+)/openapi.json',
      handler: 'documentation.openapi',
      config: {
        auth: false,
        middlewares: [restrictAccess]
      }
    }
  ]
};

Add src/extensions/documentation/strapi-server.js with the following content:

'use strict';
const documentationController = require('./controllers/documentation');
const documentationRoutes = require('./routes/custom-documentation');
module.exports = async (plugin) => {
  // Controllers
  plugin.controllers.documentation = {
    ...plugin.controllers.documentation,
    ...documentationController
  };
  // Routes
  plugin.routes = [...plugin.routes, ...documentationRoutes.routes];
  return plugin;
};

Hopefully this helps somebody, would like to see these routes in the plugin though...

@grech-ca
Copy link

It's not directly related to the bug, but if you work on monorepo including both the app and the strapi, you can make a workaround like this

input: path.resolve(process.cwd(), '../admin/src/extensions/documentation/documentation/1.0.0/full_documentation.json'),

@xanghyr
Copy link

xanghyr commented Dec 6, 2022

Thank you @roelbeerens !
This is perfectly working for me. It is sad to have to come find a solution in github issues, while it must be default behaviour...

Anyway, I have a problem, probably not related to your solution : my components do not have their "required" info.
For my fields in my collection types root, I can see which are required, but all my fields inside components I can't. Any idea of how to add this in the full_documentation.json ?

@AlexandrKurbanov19
Copy link

The problem has not been fixed in newer versions. The solution was the instruction roelbeerens. Thanks for your help!

@derrickmehaffy
Copy link
Member

Hello,

In order to keep our GitHub issues clean and only related to bug reports I've moved your enhancement / feature requested over to our feedback database.

You can find your request here: https://feedback.strapi.io/developer-experience/p/not-able-to-access-endpoints-for-openapijson-and-swaggerjson

Thanks!

@robozb
Copy link

robozb commented Nov 11, 2023

+1

@robozb
Copy link

robozb commented Nov 11, 2023

If your frontend project is beside to your strapi files a good alternative to read the the file directly from the filesystem, for example:
npx openapi-typescript ../strapi/src/extensions/documentation/documentation/1.0.0/full_documentation.json -o src/lib/types/api-types.ts

@groteck
Copy link

groteck commented Mar 21, 2024

In typescript this solution doesn't work because the package @strapi/plugin-documentation/server/middlewares/restrict-access is not exposed in the package.json and also there is no type definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature source: plugin:documentation Source is plugin/documentation package
Projects
Status: Fixed/Shipped
Archived in project
Archived in project
Development

Successfully merging a pull request may close this issue.