V4 REST API OpenAPI/Swagger Documentation users, roles and permissions missing

Hello mate.
Yes, indeed in V4 there are a bug where user-role relationship will not populate even if you use the populate argument in your query. For now there is a work around to this until the official version is fixed

Create a strapi-server.js file in /src/extensions/users-permissions/ and put this code in

module.exports = plugin => {
  const sanitizeOutput = (user) => {
    const {
      password, resetPasswordToken, confirmationToken, ...sanitizedUser
    } = user; // be careful, you need to omit other private attributes yourself
    return sanitizedUser;
  };

  plugin.controllers.user.me = async (ctx) => {
    if (!ctx.state.user) {
      return ctx.unauthorized();
    }
    const user = await strapi.entityService.findOne(
      'plugin::users-permissions.user',
      ctx.state.user.id,
      { populate: ['role'] }
    );

    ctx.body = sanitizeOutput(user);
  };

  plugin.controllers.user.find = async (ctx) => {
    const users = await strapi.entityService.findMany(
      'plugin::users-permissions.user',
      { ...ctx.params, populate: ['role'] }
    );

    ctx.body = users.map(user => sanitizeOutput(user));
  };

  return plugin;
};

This will makes by default the role relationship to populate in /api/users/me and /api/users. Also you can add more relationships if you need.

Source: Population does not work for Users in Users-Permissions · Issue #11957 · strapi/strapi · GitHub
By: @iicdii

1 Like