Skip to content
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

How to get the source of a reference? #142

Open
sticklerdev opened this issue Aug 21, 2018 · 5 comments
Open

How to get the source of a reference? #142

sticklerdev opened this issue Aug 21, 2018 · 5 comments

Comments

@sticklerdev
Copy link

sticklerdev commented Aug 21, 2018

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')

@whitlockjc
Copy link
Owner

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 [email protected]+, 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.

@dainyl
Copy link

dainyl commented Jul 17, 2019

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.

@whitlockjc
Copy link
Owner

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

@dainyl
Copy link

dainyl commented Jul 18, 2019

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'
}

@whitlockjc
Copy link
Owner

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?

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

No branches or pull requests

3 participants