Skip to content

fix: load yaml file transform ref bug #1097

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

Closed
wants to merge 6 commits into from

Conversation

liangskyli
Copy link
Contributor

Changes

fix: #1093

How to Review

How can a reviewer review your changes? What should be kept in mind for this review?

Checklist

  • Unit tests updated
  • README updated
  • examples/ directory updated (if applicable)

@changeset-bot
Copy link

changeset-bot bot commented May 8, 2023

⚠️ No Changeset found

Latest commit: f864d81

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@liangskyli
Copy link
Contributor Author

I found a problem with the code being processed
How to determine $ref has been transformed?

@@ -279,6 +279,10 @@ export default async function load(

const ref = parseRef(node.$ref);

// when ref.path is [], $ref has been transform, skip transform
if (ref.path.length === 0 && (ref.filename.startsWith("components[") || ref.filename.startsWith("external["))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm this may not be true in all cases; $refs could also reference the operations interface as well.

This feels like masking a potential bug rather than pinpointing exactly why the $ref isn’t being transformed correctly.

Is this an invalid schema?

Or is this a specific bug with specific $refs?

Rather than change this, we should find out exactly what kind of $ref isn’t being transformed properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for this bug is:
use items: &ref_0 and items: *ref_0, Causes the ref reference in the currentSchema to point to the same address, and the currentSchema data has changed, resulting in ref transform many times!
I use JSON.parse and JSON.stringify to handle schema data before walk currentSchema to refactor code.

@drwpow drwpow force-pushed the main branch 5 times, most recently from c94653a to ae7a4f3 Compare May 11, 2023 15:29
@drwpow
Copy link
Contributor

drwpow commented May 25, 2023

Thanks so much for opening this PR. This updated version is an improvement over the initial PR. But the solution here to JSON.parse and JSON.stringify every schema that comes in is too far-reaching. It would have performance impacts for all users when this is a problem unique to people using js-yaml to generate YAML files. And it could introduce other bugs not currently caught by tests.

The root of the problem lies in the parseRef() utility—its one and only job is to transform #/components/schemas/Foo into components["schemas"]["Foo"]. However, in this bug, it’s actually receiving the string "components[\"schemas\"][\"Foo\"]" from js-yaml and it just doesn’t know how to parse it. So rather than JSON.parse and JSON.stringify everything, we can just teach parseRef() to handle this alternate $ref form as a much more targeted solution.

If it’s all right, I’d prefer handling the fix and writing a test, to ensure it’s working as expected. I’d also like to make a performance improvement here as well.

I’m going to close this PR as it’s I’d prefer another solution to this bug, but you still get full marks for your contribution! 🙂

@drwpow drwpow closed this May 25, 2023
@drwpow drwpow mentioned this pull request May 25, 2023
3 tasks
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.

use the same openapi, json file generate ok, but yaml file generate error
2 participants