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

JSON References resolve base path - version 3

Open okwei2000 opened this issue 6 years ago • 66 comments

With version 3 of the editor, it does not resolve relative references. Version 2 seems to have it fixed. https://github.com/swagger-api/swagger-editor/pull/1000

Version 2 of the editor has a preference panel, where the 'Pointer Resolution Base Path' can be set. How this can be done in Version 3?

  Clinic:
    $ref: './Clinic.yaml'

Could not resolve reference because of: Tried to resolve a relative URL, without having a basePath. path: './Clinic.yaml' basePath: 'undefined' Jump to line 59

okwei2000 avatar Jul 19 '17 15:07 okwei2000

Can you share a full definition?

webron avatar Jul 19 '17 22:07 webron

A.yaml (The main file, it's accessible by http://myhost.com/A.yaml)

swagger: '2.0'
info:
  title: Service A
  version: 1.0.0
host: www.myhost.com
schemes:
  - https
basePath: /rest/
produces:
  - application/json
  - application/xml
paths:
  /test:
    get:
      summary: Summary
      consumes:
        - application/json
        - application/xml
      parameters:
        - name: code
          in: query
          description: The code to search for
          required: true
          type: string
      responses:
        '200':
          description: Clinics
          schema:
            $ref: './B.yaml'

B.yaml (in the same directory, http://myhost.com/B.yaml)

properties:
  name:
    type: string

I got error:

Resolver error at paths./test.get.responses.200.schema.$ref Could not resolve reference because of: Tried to resolve a relative URL, without having a basePath. path: './B.yaml' basePath: 'undefined' Jump to line 29

If I replace the $ref: './B.yaml' with $ref: 'http://myhost.com/B.yaml', then everything is OK.

okwei2000 avatar Jul 20 '17 02:07 okwei2000

+1 I'm having the same issue. It would be nice to be able to set 'Pointer Resolution Base Path' in version 3.

moon0326 avatar Jul 20 '17 07:07 moon0326

I don't see the behavior of this being changed soon. The editor works locally and has no knowledge of where the file is hosted.

webron avatar Jul 20 '17 20:07 webron

@webron that's exactly the reason the 2.x release had the useful option pointerResolutionBasePath

gunzip avatar Jul 20 '17 20:07 gunzip

I understand, but in terms of priorities, it's not at the top. We'll gladly review PRs though.

webron avatar Jul 20 '17 21:07 webron

In the mean time, is there a workaround? can I hard code the pointerResolutionBasePath somewhere in the source code?

okwei2000 avatar Jul 21 '17 01:07 okwei2000

My workaround has been to use the docker image swaggerapi/swagger.io-editor, which allows you to revert to the 2.x interface and use the old preference settings.

EDIT: Just noticed that the link simply takes you to the hosted version on swagger.io, so if you're trying to do only local work that won't help you.

jbinnography avatar Jul 26 '17 17:07 jbinnography

Ouch! I this effectively means I can't use version 3 for big projects where things need to be organized into multiple files for clarity.

joshmaker avatar Sep 13 '17 22:09 joshmaker

@joshmaker My current workaround..

https://github.com/moon0326/swagger-ui-watcher works fine with local relative path without base path. It now supports OpenAPI as well.

moon0326 avatar Sep 13 '17 23:09 moon0326

thx

GitHub30 avatar Nov 10 '17 08:11 GitHub30

I need to rollback but I cant find version 2.x of swagger-editor-dist.

intotecho avatar Dec 15 '17 03:12 intotecho

+1 for a resolution to this issue. It prevents using v3 of the editor for larger projects.

andy-maier avatar Feb 06 '18 12:02 andy-maier

+1 - this is kind of a big deal to me as well

CameronGo avatar Feb 06 '18 21:02 CameronGo

+1 same for me, our team is fine with the portability of the online editor, but we're still forced to use v2 editor because of this...

arlix85 avatar Feb 16 '18 16:02 arlix85

Same for me, I need relative paths in documents because there's distinct enviroments hosting Swagger-editor (i.e., distinct URLs).

giuliana-bezerra avatar Feb 21 '18 17:02 giuliana-bezerra

+1 for a fix of this issue.Without references to local schema files we need to look around for another tool :(

sprelacart avatar Mar 06 '18 08:03 sprelacart

+1 - V2 only viable option for real world API development that leverages common schema definitions in separate file(s). Very disappointing.

liam-donohoe avatar Mar 06 '18 16:03 liam-donohoe

+1 would love to use it as well.

jorgii avatar Mar 20 '18 15:03 jorgii

Could you update what's the status of this PR? We externalized definitions in yams files, and couldn't reference them in any swagger-editor V 3 (the latest tried 3..5.1) using relative paths that we are able to resolve in V 2 (e.g. $ref: './external.yaml#someDef'). It's a major roadblock for us to move to V 3

zbajbutovic avatar Apr 03 '18 14:04 zbajbutovic

This is not a PR, it's a feature request. At the moment, we're not doing any active work on supporting it, but we might in the future.

webron avatar Apr 03 '18 17:04 webron

Thanks Ron. Would you be able to prioritize it? Based on the posting it is blocking many people working on real world enterprise implementations to move to V 3.

zbajbutovic avatar Apr 03 '18 19:04 zbajbutovic

Unfortunately, given the work load that we have - I can't prioritize it at the moment. I'm sure this is not what you'd want to hear, but there is a solution we provide as a company in our commercial tool (whether free or paid versions).

This is not to say that that's the reason this is not being implemented here. The way it's implemented there is very different than what we could just migrate to the open source, otherwise we would have done it by now. We have limited resources and our to do list is (very) long - someone's going to not be happy with the results eventually. As stated above though, we'd review related PRs should anyone submit them.

webron avatar Apr 03 '18 21:04 webron

I know it's getting a bit annoying... but +1 here as well.

xhanka00 avatar Apr 05 '18 13:04 xhanka00

Not 100% sure what the complain is, the latest 3.5.2 swagger editor DO support external JSON reference. Even works with the online version, using the following URL:

http://editor.swagger.io/?url=http://localhost:8080/specs/a.yaml

The local setup is a simple NodeJS express server serving content from a folder, with CORS middleware to allow access from editor.swagger.io

We don't usually use the online editor, instead, we run a local copy hosted on the same express server. The local copy uses the swagger-editor-dist package and a custom index.html page, which adds an extra plug-in to save the changes from editor via HTTP (e.g. PUT /specs/a.yaml).

For the extra plug-in, see my reply on https://github.com/swagger-api/swagger-editor/issues/1241#issuecomment-342695807

jemerald avatar Apr 11 '18 02:04 jemerald

+1

It can be reproduced on both swagger.io editor and ui with the following Spec:

https://forge.etsi.org/rep/gitweb.cgi/NFV-SOL002-SOL003.git/blob_plain/HEAD:/src/SOL003/VNFLifecycleManagement/VNFLifecycleManagement.yaml

If I understand correctly the loader within Swagger-UI should be configured with the right value of basePath by the Editor.

Maybe this could be done using the loaded url and stripping of the latest segment?

ghost avatar Jul 02 '18 15:07 ghost

Any updates on this? This issue is around for over one year now and hasn't been approached yet. Not being able to reference relative paths i.e. organizing the config into multiple files, is a massive drawback for me and my team.

micbra avatar Sep 12 '18 12:09 micbra

@carignani You need to open the editor with url query parameter, like this:

http://editor.swagger.io/?url=https://forge.etsi.org/rep/gitweb.cgi/NFV-SOL002-SOL003.git/blob_plain/HEAD:/src/SOL003/VNFLifecycleManagement/VNFLifecycleManagement.yaml

I have tried the above and the relative file references are loaded properly as you expand the method in swagger UI, see screenshot of the network traffic:

image

Note: the 404 error is caused by a bad reference in SOL002SOL003_resp.yaml line 95 (extra slash at the beginning):

      $ref: "/../definitions/SOL002SOL003_def.yaml#/definitions/ProblemDetails"

jemerald avatar Sep 12 '18 20:09 jemerald

+1

This should be a bug. $ref is supported in the 3.0 spec.

SuryanathanP avatar Nov 07 '18 21:11 SuryanathanP

+1

I just started describing my API with OpenAPI and I encoutered this bug. IMHO, if this worked in v2 it is a bug in v3. Please fix it. I want to use this feature. :)

obstschale avatar Dec 05 '18 13:12 obstschale