Skip to content

Make urlCache as a part of load context (#707) #708

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 2 commits into from
Sep 30, 2021
Merged

Make urlCache as a part of load context (#707) #708

merged 2 commits into from
Sep 30, 2021

Conversation

mbelsky
Copy link
Contributor

@mbelsky mbelsky commented Aug 2, 2021

Closes #707

/** Load a schema from local path or remote URL */
export default async function load(
schema: URL | PartialSchema,
options: LoadOptions
): Promise<{ [url: string]: PartialSchema }> {
const urlCache = options.urlCache || new Set<string>();
Copy link
Contributor

Choose a reason for hiding this comment

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

How can we ensure that the cache object being passed in is of type Set when the user is consuming the library with vanilla JS? They will have types provided, but it will not throw an error if they pass a different data structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for asking. As I see this function is not a part of the library public api. And the function uses the cache object for recursive calls optimization. Also as you may see the function does not check types of the other fields of LoadOptions.

@codecov
Copy link

codecov bot commented Aug 12, 2021

Codecov Report

Merging #708 (e78959e) into main (56cc872) will decrease coverage by 2.18%.
The diff coverage is 55.55%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #708      +/-   ##
==========================================
- Coverage   96.34%   94.15%   -2.19%     
==========================================
  Files          13       13              
  Lines         547      565      +18     
  Branches      194      198       +4     
==========================================
+ Hits          527      532       +5     
- Misses         19       32      +13     
  Partials        1        1              
Impacted Files Coverage Δ
src/load.ts 82.50% <40.00%> (-8.93%) ⬇️
src/index.ts 96.66% <100.00%> (ø)
src/transform/schema.ts 99.00% <100.00%> (+0.01%) ⬆️
src/utils.ts 95.00% <100.00%> (-1.56%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 72acb8d...e78959e. Read the comment docs.

@mbelsky
Copy link
Contributor Author

mbelsky commented Aug 13, 2021

Hey @drwpow

I've made two tests runs on my local machine: one on the main branch and one on the pr/pass-cache-in-context. And the tests run time difference is about 100ms. So I'm not sure how may I optimize the pr changes to make the tests pass. If you have any ideas about this issue I would like to hear about it.

Run tests on the main branch
~/gh/mb-openapi-ts (main)$ npm test -- ./tests/v3/index.test.ts 

> [email protected] test
> npm run build && jest --no-cache "./tests/v3/index.test.ts"


> [email protected] build
> rm -rf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json

 PASS  tests/v3/index.test.ts (48.591 s)
  cli
    ✓ reads github.yaml spec (v3) from file (4074 ms)
    ✓ reads github.yaml spec (v3) from file (additional properties) (4303 ms)
    ✓ reads github.yaml spec (v3) from file (immutable types) (4329 ms)
    ✓ reads manifold.yaml spec (v3) from file (1075 ms)
    ✓ reads manifold.yaml spec (v3) from file (additional properties) (1076 ms)
    ✓ reads manifold.yaml spec (v3) from file (immutable types) (1064 ms)
    ✓ reads null-in-enum.yaml spec (v3) from file (694 ms)
    ✓ reads null-in-enum.yaml spec (v3) from file (additional properties) (698 ms)
    ✓ reads null-in-enum.yaml spec (v3) from file (immutable types) (695 ms)
    ✓ reads petstore-openapitools.yaml spec (v3) from file (830 ms)
    ✓ reads petstore-openapitools.yaml spec (v3) from file (additional properties) (829 ms)
    ✓ reads petstore-openapitools.yaml spec (v3) from file (immutable types) (824 ms)
    ✓ reads petstore.yaml spec (v3) from file (821 ms)
    ✓ reads petstore.yaml spec (v3) from file (additional properties) (830 ms)
    ✓ reads petstore.yaml spec (v3) from file (immutable types) (832 ms)
    ✓ reads stripe.yaml spec (v3) from file (3238 ms)
    ✓ reads stripe.yaml spec (v3) from file (additional properties) (3899 ms)
    ✓ reads stripe.yaml spec (v3) from file (immutable types) (3711 ms)
    ✓ reads spec (v3) from remote resource (1196 ms)
  json
    ✓ reads github.yaml from JSON (6150 ms)
    ✓ reads manifold.yaml from JSON (235 ms)
    ✓ reads null-in-enum.yaml from JSON (33 ms)
    ✓ reads petstore-openapitools.yaml from JSON (82 ms)
    ✓ reads petstore.yaml from JSON (87 ms)
    ✓ reads stripe.yaml from JSON (3399 ms)

Test Suites: 1 passed, 1 total
Tests:       25 passed, 25 total
Snapshots:   0 total
Time:        48.72 s
Ran all test suites matching /.\/tests\/v3\/index.test.ts/i.
Run tests on the pr/pass-cache-in-context branch
~/gh/mb-openapi-ts (pr/pass-cache-in-context)$ npm test -- ./tests/v3/index.test.ts 

> [email protected] test
> npm run build && jest --no-cache "./tests/v3/index.test.ts"


> [email protected] build
> rm -rf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json

 PASS  tests/v3/index.test.ts (48.653 s)
  cli
    ✓ reads github.yaml spec (v3) from file (4115 ms)
    ✓ reads github.yaml spec (v3) from file (additional properties) (4447 ms)
    ✓ reads github.yaml spec (v3) from file (immutable types) (4273 ms)
    ✓ reads manifold.yaml spec (v3) from file (902 ms)
    ✓ reads manifold.yaml spec (v3) from file (additional properties) (946 ms)
    ✓ reads manifold.yaml spec (v3) from file (immutable types) (940 ms)
    ✓ reads null-in-enum.yaml spec (v3) from file (610 ms)
    ✓ reads null-in-enum.yaml spec (v3) from file (additional properties) (616 ms)
    ✓ reads null-in-enum.yaml spec (v3) from file (immutable types) (608 ms)
    ✓ reads petstore-openapitools.yaml spec (v3) from file (739 ms)
    ✓ reads petstore-openapitools.yaml spec (v3) from file (additional properties) (724 ms)
    ✓ reads petstore-openapitools.yaml spec (v3) from file (immutable types) (740 ms)
    ✓ reads petstore.yaml spec (v3) from file (754 ms)
    ✓ reads petstore.yaml spec (v3) from file (additional properties) (827 ms)
    ✓ reads petstore.yaml spec (v3) from file (immutable types) (841 ms)
    ✓ reads stripe.yaml spec (v3) from file (3809 ms)
    ✓ reads stripe.yaml spec (v3) from file (additional properties) (4025 ms)
    ✓ reads stripe.yaml spec (v3) from file (immutable types) (3712 ms)
    ✓ reads spec (v3) from remote resource (1226 ms)
  json
    ✓ reads github.yaml from JSON (6001 ms)
    ✓ reads manifold.yaml from JSON (224 ms)
    ✓ reads null-in-enum.yaml from JSON (27 ms)
    ✓ reads petstore-openapitools.yaml from JSON (75 ms)
    ✓ reads petstore.yaml from JSON (83 ms)
    ✓ reads stripe.yaml from JSON (3258 ms)

Test Suites: 1 passed, 1 total
Tests:       25 passed, 25 total
Snapshots:   0 total
Time:        48.798 s
Ran all test suites matching /.\/tests\/v3\/index.test.ts/i.

@kamsar
Copy link

kamsar commented Sep 8, 2021

The lack of this fix really crippled my attempt to use the node API, since the cache lives forever - and does not actually add the cached schema to the second and later call, i.e.

if (urlCache.has(schemaID))
            return options.schemas;

In the above code in a second or later function invocation, urlCache will have the schema cached - but options.schemas will NOT contain it, resulting in spuriously unresolved externals in the output.

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

Awesome fix! Thank you.

@drwpow drwpow merged commit 091b053 into openapi-ts:main Sep 30, 2021
@drwpow
Copy link
Contributor

drwpow commented Sep 30, 2021

@all-contributors please add @mbelsky for code, bug

@allcontributors
Copy link
Contributor

@drwpow

I've put up a pull request to add @mbelsky! 🎉

@drwpow
Copy link
Contributor

drwpow commented Sep 30, 2021

Released in 4.1.1!

@kamsar
Copy link

kamsar commented Sep 30, 2021

@drwpow thanks so much, really appreciate this fix :)

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.

Run openapiTS two times in row throws an Error
4 participants