Skip to content

chore: replace lodash forEach with native Object.entries/values - #27409

Merged
innerdvations merged 7 commits into
strapi:developfrom
unrevised6419:chore/replace-lodash-foreach-with-native
Aug 20, 2026
Merged

innerdvations merged 7 commits into
strapi:developfrom
unrevised6419:chore/replace-lodash-foreach-with-native

Conversation

@unrevised6419

@unrevised6419 unrevised6419 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

What does it do?

  1. Replaces the remaining _.forEach(object, ...) lodash calls with native for...of loops over Object.entries(object) / Object.values(object). Also drops the now-unused lodash default import in two files that had no other _. usage left.
  2. Fixes a bug found while doing (1): in content-api's getRoutesMap, when a plugin's routes is a flat array, the computed routes were discarded instead of being assigned to routesMap, so those plugins' routes never appeared under plugin::<name>. Added a regression test.

Why is it needed?

(1) reduces reliance on lodash in favor of native JS where a straightforward native equivalent exists, with no behavior change. (2) is a genuine correctness fix uncovered during that cleanup.

How to test it?

  • yarn test:unit, yarn test:ts, yarn lint
  • New test: packages/core/core/src/services/content-api/__tests__/index.test.ts covers both the array and record shapes of plugin.routes in getRoutesMap.

Related issue(s)/PR(s)

None

@unrevised6419
unrevised6419 force-pushed the chore/replace-lodash-foreach-with-native branch from 8bac642 to 1ddfc7e Compare August 18, 2026 16:55
Fewer lodash calls, smaller bundle, no behavior change.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@unrevised6419
unrevised6419 force-pushed the chore/replace-lodash-foreach-with-native branch from 1ddfc7e to 4ba868b Compare August 18, 2026 17:15
@unrevised6419

Copy link
Copy Markdown
Contributor Author

While converting `_.forEach` in `content-api/index.ts`, found a pre-existing bug in `getRoutesMap`: when `plugin.routes` is an array, the computed `plugin.routes.map(transformPrefix).filter(filterContentAPI)` result was discarded instead of being assigned to `routesMap[`plugin::${pluginName}`]`, so array-style plugin routes never appeared in the content-API routes map.

Fixed in 4ba868b, with a regression test in `packages/core/core/src/services/content-api/tests/index.test.ts`.

@unrevised6419
unrevised6419 marked this pull request as ready for review August 18, 2026 17:19
@dosubot dosubot Bot added pr: chore This PR contains chore tasks (cleanups, configs, tooling...) pr: fix This PR is fixing a bug source: core:core Source is core/core labels Aug 18, 2026
@unrevised6419
unrevised6419 force-pushed the chore/replace-lodash-foreach-with-native branch 3 times, most recently from 8a537d3 to dde5f55 Compare August 18, 2026 20:34
…utes map

getRoutesMap computed the filtered routes for array-style plugin.routes but
never assigned them to routesMap, so those plugins never appeared under
plugin::<name>.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@unrevised6419
unrevised6419 force-pushed the chore/replace-lodash-foreach-with-native branch from dde5f55 to b9f1e2a Compare August 18, 2026 20:36
innerdvations
innerdvations previously approved these changes Aug 19, 2026

@innerdvations innerdvations left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@innerdvations innerdvations added this to the 5.52.2 milestone Aug 19, 2026
@innerdvations
innerdvations enabled auto-merge (squash) August 19, 2026 13:12
auto-merge was automatically disabled August 19, 2026 13:37

Head branch was pushed to by a user without write access

@unrevised6419

Copy link
Copy Markdown
Contributor Author

@innerdvations did fix the type errors, please trigger CI again

@innerdvations innerdvations left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lodash → native loops and the getRoutesMap array-route fix look good.

Please drop the i18n Struct typing commit (and the @strapi/types peer / yarn.lock change). model was already any, so Object.entries(model.attributes) type-checks without Struct — that commit is not needed to fix the loop swap. Keep the PluginDeclaration move; that one is the real typing follow-on from Object.entries(userPluginsConfig).

If you still want the annotations, use import type { Struct } from '@strapi/types' only and leave @strapi/types as a devDependency (same as graphql/documentation). Do not add it as a peer or a runtime dependency — Struct is a type-only export.

AI-assisted comment.

import _ from 'lodash';
import { pick, pipe, has, prop, isNil, cloneDeep, isArray } from 'lodash/fp';
import { errors, contentTypes as contentTypeUtils } from '@strapi/utils';
import { Struct } from '@strapi/types';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer reverting this file to native loops + any (as in the earlier commit). Object.entries(model.attributes) does not need Struct when model is any.

If you keep the annotations, this should be import type { Struct } from '@strapi/types' like the rest of this plugin — Struct is export type * as Struct.

AI-assisted comment.

@unrevised6419 unrevised6419 Aug 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.forEach resolved model.attributes attr type to any. Object.entries resolves it to unknown.

@strapi/types is already declared as a dev dependency, it was missing only the peer one, which ESLint reported as missing.

Struct.ComponentSchema | Struct.ContentTypeSchema is already used for the model type in this workspace. It was only missed for a few calls down.

PS: hate it when AI get these things wrong )

Comment thread packages/plugins/i18n/package.json Outdated
"peerDependencies": {
"@strapi/admin": "^5.0.0",
"@strapi/content-manager": "^5.0.0",
"@strapi/types": "^5.0.0",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not add @strapi/types as a peer. It is already a devDependency, and with import type there is no runtime/package contract change. Revert this peer and the matching yarn.lock hunk.

AI-assisted comment.

@unrevised6419 unrevised6419 Aug 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the time, @strapi/types get's exposed through plugins public API. Without it as a peer dependency, type check will fail in a isolated dependencies environment.

Probably what we want is to mark the peer dependency as optional.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically all strapi packages/plugins should be dev+peer, and added as "dependencies" on @strapi/strapi or @strapi/core

@unrevised6419

Copy link
Copy Markdown
Contributor Author

@innerdvations should be good now

@innerdvations innerdvations removed the pr: fix This PR is fixing a bug label Aug 19, 2026

@innerdvations innerdvations left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@innerdvations
innerdvations enabled auto-merge (squash) August 20, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: chore This PR contains chore tasks (cleanups, configs, tooling...) source: core:core Source is core/core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants