swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

Difference in resolving URI's causes difficult-to-find errors

Open mptyspace opened this issue 2 years ago • 0 comments

So yesterday I ran into a problem that threw the following error:

Unable to load " + refFormat + " ref: " + file + " path: "+parentDirectory

And it lead me down a merry chase to the issue. Ultimatetely, it comes down to this.

Consider the following references:

./foo.yaml
./foo.yaml/

If I were to resolve this path as a URL, my server would resolve this as a full path: (...)/foo.yaml/. It would immediately result in an error.

However, (most) filesystems do not, they would resolve it as: (...)/foo.yaml. Correcting the 'mistake' of the trailing slash, and loading the schema anyway.

Now, let's assume that in foo.yaml we have a reference to another file, like ../bar.yaml. Now, we've got problems.

The URI resolver has the following outcomes:

./foo.yaml + ../bar.yaml = ../bar.yaml
./foo.yaml/ + ../bar.yaml = ./bar.yaml

I've been thinking about this since yesterday, and I have no good solution direction for this problem. There's a few things I considered:

  • Do we know what exact filepath was eventually resolved when loading the bar.yaml schema. If so, we could compare it to the path we thought it was at, and fix it if it was different.
  • Can we remove trailing slashes if we are resolving a file in the context of a file system? But how would you identify that file without explicit naming conventions?
  • Is this not a actually a problem, but programmer error? The annoying thing is that the error is not in the reference to bar.yaml from foo.yaml, but the reference to foo.yaml. I wonder if we could at least clarify that.

mptyspace avatar Oct 24 '23 07:10 mptyspace