-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Keep $ref some how when bundle #173
Comments
There's already some existing discussion going on about this in Issue #135. I think your solution could be acceptable, but I'd like to get some other opinions too |
Indeed this looks good to me. Though perhaps persisting +1 |
Persisting $ref would confuse tools which know what a $ref is and want to do something about it. It could cause some infinite loops, or just errors from tools which warn you they don't know how to deal with a $ref - which is why you're dereferencing in the first place. @MikeRalphson has done a bit of this. |
I simply allowed the caller to specify a property name for it to be copied to if they wanted |
I like @MikeRalphson's approach. We could add an option, like say Any falsy value = remove the $ref as it does currently. Any string value = rename the $ref to the specified value |
That would definitely work in our case (from Issue #135) and wouldn't generate a breaking change. |
Author of json-schema-to-typescript here. We'd love this features, as it would unblock a handful of issues related to emitting more intelligent TypeScript types. As specific example, consider the following JSON Schema: {
additionalProperties: false,
definitions: {
a: {type: 'string'},
b: {type: 'number'}
},
properties: {
c: {
additionalItems: false,
items: [{$ref: '#/definitions/a'}, {$ref: '#/definitions/b'}],
type: 'array'
}
},
title: 'X,
type: 'object'
} We'd like to emit the following TypeScript types for this schema: type A = string
type B = number
type X = {
c: [] | [A] | [A, B]
} Today, we accomplish this by always emitting standalone type names (here, To do that, we'd love a way to know what a schema's original As a side note: It's possible to infer something like this today (roughly: traverse over a dereferenced schema and its source schema at the same time, and annotate the dereferenced schema where you see a Please let me known if this is not something you're interested in, and I'll go ahead and fork |
@bcherny - Thanks for providing another example of why this feature would be useful. If somebody would like to submit a PR, I'd be happy to review it. However, I've realized that it's not actually as easy as I suggested previously. The problem with simply keeping the Instead, it seems like we may need to add an array property (probably with a Symbol to avoid breaking changes) that contains all the refs that pointed to a particular object. |
@JamesMessinger That's a great catch! Thinking about it more, I wonder if a simpler and more general-purpose approach might be to avoid setting a property at all, and exposing an One possible API: const result = await $RefParser.dereference('schema.json', {
onDereference(originalSourceSchema, dereferencedSchema, originalRefPath, absoluteRefPath) {
// ...
}
}) A consumer might then use this API to build up a mapping: const map = new Map
const result = await $RefParser.dereference('schema.json', {
onDereference(originalSourceSchema, dereferencedSchema, originalRefPath, absoluteRefPath) {
if (!map.has(dereferencedSchema)) {
map.set(dereferencedSchema, new Set)
}
map.get(dereferencedSchema).add(originalRefPath)
}
}) This approach has a few benefits over the
|
I like that idea @bcherny! What does everyone else think? Would this work for your use-case? /cc @wenerme @maxtuzz @MikeRalphson @philsturgeon @ftheomunhoz |
Personally, I've only ever needed to know where a |
This callback approach would work for the "live mock server wants to watch all $ref files for changes so it can reload" use-case. We'd just look file all filepaths and strip of anchors and watch that set of files. |
All conversations about changing how $ref should work should be done over here https://github.com/json-schema-org/referencing then when its decided we can all make sure tools follow it. |
Because $ref offen mean type of something, when render views, need the ref type to select the component.
e.g.
Address.json
User.json
If the bundled result is
Then the frontend can use the
"_ref": "Address.json"
to match the component more precisely.The text was updated successfully, but these errors were encountered: