Editor not able to resolve external reference with $ref
Hi, I am trying to refer to an external definition file with $ref. The editor is failing to compile. I am using editor.swagger.io
Here is how I am setting the reference in my swagger definition file:
'500':
description: Unexpected error
schema:
$ref: 'Response-status.yaml#/Response-status'
I have a file called "Response-status.yaml", stored locally, that is defined as follows:
Response-status:
type: object
required:
- status
- http-code
properties:
status:
type: string
description: Status of the request.
........
.......
Please advise.
Here is the error message I see:
Details
Object
code: "UNRESOLVABLE_REFERENCE"
message: "Not Found"
path: Array [5]
level: 900
type: "Swagger Error"
description: "Not Found"
lineNumber: 84
I found this issue too. When i inspect the network logs on the browser i can see that the editor is resolving relative references relative to the editor URL instead of relative to the document URL.
Do you still have this issue? We just recently updated Sway
Yes, I still see the same issue. Though the error message is much well defined now.
code: "UNRESOLVABLE_REFERENCE"
message: "Reference could not be resolved: ./schema/response-status.schema"
path: Array [7]
error: "obj must be an Array or an Object"
level: 900
type: "Swagger Error"
description: "Reference could not be resolved: ./schema/response-status.schema"
lineNumber: 80
I am using editor.swagger.io
Surprisingly, the editor on Swaggerhub works fine with the same definition file.
Swagger-UI does not complain and actually does picks up a single level of $ref as well.
@fehguy How should a relative URL like this "./schema/response-status.schema" be resolved. As of now, the URL gets added to the swagger.io URL.
in the editor, it's a little odd. Simply put, the relative URL is supposed to be relative to the location of the swagger definition.
If the swagger URL was loaded from http://foo.bar.com/swagger.json, the relative location should be http://foo.bar.com/schema/response-status.schema. If the URL was loaded from http://foo.bar.com/ (note, trailing slash), it'd be relative to that location.
But since the editor doesn't have a location to load from (you could import it), the relative location doesn't make much sense. If we had to solve this, I would say it's relative to the scheme + host + basePath location, but even that you could argue is wrong.
@RitwikChatterjee what is it you're expecting it to resolve to?
According to @fehguy we can not resolve relative paths in Swagger Editor.
It seems that the relative $ref should resolve to something other than the editor URL. Maybe we can create a configuration property that specifies the location of the swagger spec for the purpose of resolving the relative refs.
Because the swagger spec supports the relative $ref, the editor should support it in some fashion if possible.
Can this issue be revisited before closing it as wont-fix?
That's some pretty critical functionality that you're writing off, I guess I'll be waiting for another swagger editor that supports our need to split API design across multiple files.
@pgtelus that's not really correct. What's being discussed here is "relative file paths", which in an editor context, do not make sense because there is no "root path" for the swagger definition being edited.
We can infer something, but it will always be wrong. For example, the host + basePath is for the api and most people do not deploy the swagger definition there.
I think I understand that, but it sounds like an explanation of why swagger-editor cannot or will not do what the OP wants. The user has an API documented in two files, one of which refers to the other. Does swagger-editor support this? This is functionality I need as well.
@pgtelus yes it does. You need to use absolute paths:
$ref: http://petstore.swagger.io/v2/swagger.json#/definitions/Pet
Works just fine as a schema item. The only issue here was about the paths being relative which doesn't make sense for the editor.
Gotcha ok thanks for the clarification.
It's still an issue for us, we need a solution that let's us keep relevant API definition files in a common location without having to host them somewhere permanent outside of the working repository.
so you are saying the requirement for a relative location of shared files? How about opening a new issue specifically on that and we can see how it could be handled. As previously suggested, it's not clear what they need to be relative to.
I'm happy to do that, issue 977 created. Apologies for not being rigorous but I think it's clear enough to get the point across and at least generate some discussion.
sure, thank you
in swagger.yaml, $ref for file must use full path, e.g. $ref: "api/swagger/routes/calendar.yaml" in /calendar.yaml contains another $ref: "../model/calendar.yaml", in here it must be relative path, because model/calendar.yaml‘s parent is api/swagger/routes/calendar.yaml
in a world, use full path in swagger.yaml, in other yaml file use relative path