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

Editor not able to resolve external reference with $ref

Open RitwikChatterjee opened this issue 11 years ago • 19 comments

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.

RitwikChatterjee avatar Nov 28 '15 04:11 RitwikChatterjee

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

RitwikChatterjee avatar Nov 30 '15 05:11 RitwikChatterjee

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.

yeiniel avatar Jan 19 '16 15:01 yeiniel

Do you still have this issue? We just recently updated Sway

saharj avatar Jan 29 '16 06:01 saharj

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.

RitwikChatterjee avatar Feb 03 '16 22:02 RitwikChatterjee

Swagger-UI does not complain and actually does picks up a single level of $ref as well.

RitwikChatterjee avatar Feb 04 '16 23:02 RitwikChatterjee

@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.

saharj avatar Feb 18 '16 05:02 saharj

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?

fehguy avatar Feb 18 '16 06:02 fehguy

According to @fehguy we can not resolve relative paths in Swagger Editor.

saharj avatar Mar 10 '16 05:03 saharj

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?

jeff9finger avatar Mar 10 '16 16:03 jeff9finger

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 avatar Jun 15 '16 16:06 pgtelus

@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.

fehguy avatar Jun 15 '16 16:06 fehguy

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 avatar Jun 15 '16 17:06 pgtelus

@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.

fehguy avatar Jun 15 '16 17:06 fehguy

Gotcha ok thanks for the clarification.

pgtelus avatar Jun 15 '16 17:06 pgtelus

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.

pgtelus avatar Jun 15 '16 17:06 pgtelus

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.

fehguy avatar Jun 15 '16 17:06 fehguy

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.

pgtelus avatar Jun 15 '16 18:06 pgtelus

sure, thank you

fehguy avatar Jun 15 '16 18:06 fehguy

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

zeng-ge avatar Oct 13 '17 09:10 zeng-ge