Skip to content

unusedComponents crashes when a component type is fully emptied during recursion #210

@Ayo-Awe

Description

@Ayo-Awe

Bug

When unusedComponents cleanup recurses and a component type becomes completely empty (all entries removed in a previous pass), the traversal crashes with:

TypeError: Cannot read properties of undefined (reading 'includes')
at openapi-format.js:759

The overlay file is not involved — the crash occurs with unusedComponents alone.

Root cause

Line 759 in openapi-format.js:

if (stripUnused.includes(this.path[1]) && unusedComp[this.path[1]].includes(this.key)) {

On the first recursion pass, unusedComp is built from component types present in the spec at that point. If a type (e.g. securitySchemes) is completely emptied during that pass, it is absent from unusedComp on the second pass. stripUnused still references it, so unusedComp[this.path[1]] is undefined rather than [].

Fix

if (stripUnused.includes(this.path[1]) && (unusedComp[this.path[1]] ?? []).includes(this.key)) {

Reproduction

Any spec where:

  1. A component type has only one entry (e.g. a single security scheme)
  2. That entry becomes unused after other components are removed
  3. The unusedComponents recursion runs a second pass after the type is fully emptied

Version

openapi-format 1.31.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions