whitlockjc / json-refs

Various utilities for JSON Pointers (http://tools.ietf.org/html/rfc6901) and JSON References (http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get the source of a reference?

sticklerdev opened this issue · comments

Say I have three files, A which has a reference to B, which in turn has a reference to C. When I do a resolveRefs on A (or one of the find* variants), the result I get has a refs property and each element in that contains where the reference pointed to but there is no indication of where that reference was "defined" (the location/document and the hash of the element that contained the '$ref). Would it be possible to extend the RefDetails to also include this information?

I have looked into RefPostProcessor but am not sure if this information is passed to the processor.

I am writing a simple validator and this information is useful for reporting errors back to the user - at least in cases where the reference is invalid, missing or generates a warning (additional properties with a '$ref')

There's two things here and I'll discuss them individually.

Definition Location

The definition location is recorded as the key of the refs property. Now...for documents with external/remote references, the everything is recorded relative to root of the root document. In json-refs@v3.0.0+, there is a new property to attempt to provide the fully-qualified location, fqURI. Since the keys are JSON Pointers, you can use JsonRefs#pathFromPtr to get the path segments to traverse the input object to find the location.

File Column/Line

This is something I've wanted to do for a while and it can be done but it requires hefty dependencies and extra processing time. I'm not against it, and other tools (oval and sway) could benefit from it. It's something I want to do but I've just not prioritized it.

It seems the path to the source file isn't currently being given to RefPostProcessor and I don't have the fully qualified location fqURI there either.

I am trying to write some custom logic for resolving files to mimic node_modules resolution, but I feel like I'm stuck due to not having the source file location.

Is it possible to add the source file location to the arguments of RefPostProcessor? Would be handy for RefPreProcessor too.

refDetails provided to RefPostProcessor should have the location. If it doesn't, it's a bug.

This is an example of what I'm seeing in refDetails (first argument to RefPostProcessor)

{
    def: { '$ref': './test.yaml' },
    uri: './test.yaml',
    uriDetails:
    {
        scheme: undefined,
        userinfo: undefined,
        host: undefined,
        port: undefined,
        path: './test.yaml',
        query: undefined,
        fragment: undefined,
        reference: 'relative'
    },
    type: 'relative'
}

Is this referenced from the parent document? If so, you could compute the path based on the knowledge of the parent document. Also, what version of json-refs are you using?