Skip to content

Fix mutating $refs in Node.js API #1203

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

Merged
merged 1 commit into from
Jul 6, 2023
Merged

Fix mutating $refs in Node.js API #1203

merged 1 commit into from
Jul 6, 2023

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Jul 6, 2023

Changes

Fixes #1178.

How to Review

N/A

Checklist

  • Unit tests updated
  • README updated
  • examples/ directory updated (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Jul 6, 2023

🦋 Changeset detected

Latest commit: 62a6524

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

This PR includes changesets to release 1 package
Name Type
openapi-typescript 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

@drwpow drwpow force-pushed the fix-schema-mutation branch from bd7e8b9 to 0fd610d Compare July 6, 2023 23:10
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 6, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 62a6524
Status: ✅  Deploy successful!
Preview URL: https://a6ee627e.openapi-ts.pages.dev
Branch Preview URL: https://fix-schema-mutation.openapi-ts.pages.dev

View logs

@drwpow drwpow force-pushed the fix-schema-mutation branch 3 times, most recently from 40db951 to 8488a47 Compare July 6, 2023 23:31
@drwpow drwpow force-pushed the fix-schema-mutation branch from 8488a47 to 62a6524 Compare July 6, 2023 23:37
@@ -173,7 +173,7 @@ export default async function load(schema: URL | Subschema | Readable, options:
else if (typeof schema === "object") {
options.schemas[schemaID] = {
hint: "OpenAPI3",
schema: schema as any,
schema: JSON.parse(JSON.stringify(schema)), // create deep clone of inline schema (don’t mutate)
Copy link
Contributor Author

@drwpow drwpow Jul 6, 2023

Choose a reason for hiding this comment

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

So funny enough, after some lazy performance testing in V8, JSON.parse(JSON.stringify(…)) was faster than structuredClone(). Which I guess makes sense with JSON-serializable data that we’re working with. I imagine it‘s also because structuredClone() hasn’t had as much time to be optimized, comparatively. But I’d hope long-term the latter would be faster.

This is probably not that much of a hack as I originally thought it was. And this only runs in the Node.js API (only when needed)

@drwpow drwpow merged commit 902fde1 into main Jul 6, 2023
@drwpow drwpow deleted the fix-schema-mutation branch July 6, 2023 23:40
@github-actions github-actions bot mentioned this pull request Jul 6, 2023
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.

openapiTS mutates the schema object
1 participant