Skip to content

fix: return on invalid ref pointer in remove-unused-components#2286

Merged
tatomyr merged 4 commits intoRedocly:mainfrom
jooola:fix-remove-unused-components-invalid-ref
Sep 17, 2025
Merged

fix: return on invalid ref pointer in remove-unused-components#2286
tatomyr merged 4 commits intoRedocly:mainfrom
jooola:fix-remove-unused-components-invalid-ref

Conversation

@jooola
Copy link
Copy Markdown
Contributor

@jooola jooola commented Sep 8, 2025

What/Why/How?

When bundling a specification with --remove-unused-components --dereferenced, and the specification has an invalid reference:

  parameters:
    - $ref: "../parameters.yml#/QueryProjectCreatedBefore.fail"

The process fails with:

  $redocly bundle my-api --dereferenced --remove-unused-components --output dist/my-api.json
  bundling src/my-api.yml using configuration for api 'my-api'...
  Something went wrong when processing /work/openapi-spec/src/openapi.yml:

    - Cannot read properties of undefined (reading 'split')

This change ensures that when the absolute pointer cannot be split into 2 parts, the visitor returns and let redocly report the Can't resolve $ref error.

Note that not all invalid references run into this bug, for example the following reference will correctly be reported as an error:

  parameters:
    - $ref: "../parameters.yml#/QueryProjectCreatedBefore/fail"

Reference

  parameters:
    - $ref: "../parameters.yml#/QueryProjectCreatedBefore.fail"

Testing

Couldn't find a way to test this bug with the existing tests functions. To fully test this, 2 files are required, one referencing the other.

Check yourself

  • Code changed? - Tested with Redoc/Realm/Reunite (internal)
  • All new/updated code is covered by tests
  • New package installed? - Tested in different environments (browser/node)

Security

  • The security impact of the change has been considered
  • Code follows company security practices and guidelines

@jooola jooola requested a review from a team as a code owner September 8, 2025 22:05
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Sep 8, 2025

🦋 Changeset detected

Latest commit: e85fb7e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@redocly/openapi-core Patch
@redocly/cli Patch
@redocly/respect-core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tatomyr
Copy link
Copy Markdown
Collaborator

tatomyr commented Sep 9, 2025

Thanks for contributing, @jooola! Could you add a unit or an E2E test to cover this case?

@jooola jooola force-pushed the fix-remove-unused-components-invalid-ref branch from 76083e3 to 7cea153 Compare September 9, 2025 19:48
@jooola
Copy link
Copy Markdown
Contributor Author

jooola commented Sep 9, 2025

@tatomyr Done, I hope the test are in the correct location.

@jooola jooola force-pushed the fix-remove-unused-components-invalid-ref branch from 7cea153 to 3d5748b Compare September 9, 2025 20:02
@jooola jooola requested a review from a team as a code owner September 9, 2025 20:02
@jooola jooola force-pushed the fix-remove-unused-components-invalid-ref branch 2 times, most recently from 34b2cbd to 1f3ab58 Compare September 13, 2025 18:38
@tatomyr
Copy link
Copy Markdown
Collaborator

tatomyr commented Sep 16, 2025

@jooola could you also add the corresponding fix for OAS2 here? Otherwise looks good to me.

@tatomyr
Copy link
Copy Markdown
Collaborator

tatomyr commented Sep 17, 2025

Or we will do it ourselves 🙂. Anyway, thank you for your contribution!
cc @DmitryAnansky

jooola and others added 4 commits September 17, 2025 16:05
When bundling a specification with --remove-unused-components, and
the specification has an invalid reference:

  parameters:
    - $ref: "../parameters.yml#/QueryProjectCreatedBefore.fail"

The process fails with:

  $redocly bundle my-api --dereferenced --remove-unused-components --output dist/my-api.json
  bundling src/my-api.yml using configuration for api 'my-api'...
  Something went wrong when processing /work/openapi-spec/src/openapi.yml:

    - Cannot read properties of undefined (reading 'split')

This change ensures that when the absolute pointer cannot be split into 2
parts, the visitor returns and let redocly report the "Can't resolve $ref" error.

Note that not all invalid references run into this bug, for example the following
reference will correctly be reported as an error:

  parameters:
    - $ref: "../parameters.yml#/QueryProjectCreatedBefore/fail"
@tatomyr tatomyr force-pushed the fix-remove-unused-components-invalid-ref branch from 7a29547 to e85fb7e Compare September 17, 2025 13:05
@tatomyr tatomyr merged commit b59b49c into Redocly:main Sep 17, 2025
32 checks passed
@jooola jooola deleted the fix-remove-unused-components-invalid-ref branch September 17, 2025 13:55
@el2iot2
Copy link
Copy Markdown

el2iot2 commented Sep 25, 2025

Heads up, a recent setup was working on ran afoul of (possibly? probably?) this change. Got an error like:
image

But in VSCode with redocly extension, it shows no errors and is valid:
image
(example invalid entry supplied to show validation was on and working)

Had several such apis with $ref usages side by side, and only one was marked invalid.

By commenting remove-unused-components in redocly.yaml, the validation error went away.

I suspect that the remove references somehow dropped the securitySchemes in a way that made it look invalid to a later pass??

Anyway...submitted for consideration.

CC @jooola

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants