-
-
Notifications
You must be signed in to change notification settings - Fork 223
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
Syncronous API #14
Comments
yeah, that's doable. obviously, it'll only work in Node though. Resolving external |
Indeed, I need it server side only in my case |
Right now I'm using something like: var data, done = false;
$RefParser.dereference(schema, function(err, schema) {
if (err) {
console.error(err);
return done = false;
}
data = schema;
done = true;
});
require('deasync').loopWhile(function(){return !done;});
return data; |
I'd love to see this as well. |
+1 |
This would make using supertest easier as well. +1 |
Apologies for the +1 spam, but I'd love to see this as well. For my use case, I only need synchronous support for resolving local file references. I would not need to synchronously resolve remote HTTP references. |
Good news. I've already started working on a synchronous API 👍 |
@BigstickCarpet I have a need for the synchronous API as soon as possible, and would be happy to contribute to help get it ready for release. Is there a branch and list of remaining tasks that I could dive into? Thanks! |
Hi @lange - You can see my work-in-progress here, but it's not finished yet or ready to use. I'm not quite ready to accept pull-requests yet, because the code is still very much in flux. This is a complete rewrite with a brand-new API, and nothing is fully solidified yet. It already has a synchronous API though, and a few tests that use it, so I'm definitely making good progress. I hope to have an alpha version ready for public feedback and testing within a few weeks. |
Understood - thanks for the reply @BigstickCarpet! |
add +1 to get notified - thanks a lot :) |
@mashpie For future reference there is a |
@willfarrell true, wanted to opt my plus one in addition |
Did anything ever come of this? I, too, would very very much love to do
without having to mess about with callbacks and promises. |
In the mean time, I made this: https://github.com/MikeTaylor/json-schema-ref-parser-sync I'm not proud of it, but it works. I hope it's of use to someone else. |
This would be great to implement. |
I've had to manually write this logic in my application because it supports a sync & async API. Does anyone have a rough outline of which APIs this would need to support? A bit of direction and I'd be willing to work on it. |
The deasync solution mentioned by @willfarrell and used by apidoc-plugin-schema unfortunately no longer works with the latest versions of json-schema-ref-parser since it's built on async/await. |
|
This library works for me: https://www.npmjs.com/package/json-schema-deref-sync |
Is this still necessary in an async/await world? It seems like the complaints of “I don’t want to mess around with callbacks and promises” would no longer be relevant with a bit of await now? |
@philsturgeon The exception is in |
@philsturgeon the problem is not in the complexity, but in that some use cases require synchronous behaviour. With async version the users of my function had to always add await, when invoking the function. But the often forgot to do this, because the function was meant to be used in a synchronous context. And because of this they often ended up with unhandled promise rejection errors, which were swallowed by Node. |
The problem is that the library currently does too much in a single function. Functions like
Only #1 and #2 need to be async. All the rest can be done synchronously. And, indeed, many people don't need #1 and #2 at all because they already have the schema object in memory, not in a file on disk. I'm currently working on separating the above functionality into separate functions so they'll work better with workflows that could be fully synchronous. |
@philsturgeon Node is still heavily synchronous. I have a node module which does dereference() while creating module.exports and it's used by most of the program. |
I'm considering my question answered already folks, don't need to keep explaining it! 😅 From @JamesMessinger
Looking forward to that. |
Hey all, reaching out in desperation as a bit of a JS/Node noob when it comes to async/await... I've a couple of questions that if anyone could help me with or at least point me in the right direction, I'd be massively grateful. My use case is that I'm attempting to return an object from a function and within that function I'm required to parse out a schema.
Note: I'm doing this in a TS project. |
@michaelmelody91 and for anyone interested, I was able to get synchronous dereferencing for a schema object like so: import $RefParser from '@apidevtools/json-schema-ref-parser';
import _dereference from '@apidevtools/json-schema-ref-parser/lib/dereference';
export const dereference = (schema) => {
const parser = new $RefParser();
parser.parse(schema);
parser.schema = schema;
_dereference(parser); // NOTE: mutates schema
return schema;
}; |
The caveat of the solution above is that it mutates the original object, which may not be what you want. You should probably make a deep copy of the schema object before running |
Ok there are a couple of workarounds suggested and the new library will have a synchronous option, so lets call this thread a day. Thank you! |
Hi @philsturgeon, can you elaborate more on "the new library will have a synchronous option"? What is this "new library" you speak of? |
Over here: https://github.com/APIDevTools/json-schema-reader This tool will eventually replace json-schema-ref-parser but is not yet complete. The goal is to continue meaningful changes over there whilst getting it ship shape, although priorities and availability for the maintainers is a struggle at the moment. If you'd like to muck about with that lib and see how far you get that'd be handy. |
I had a problem with this solution, because the latest version require _dereference(parser, { dereference: { circular: true } }); // NOTE: mutates schema |
Hey!
You did a great job here!
I need to use your API syncronously ( because I need it in a syncronous framework). Is there any plan to do it in the future?
Thank you
The text was updated successfully, but these errors were encountered: