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

Can't fetch local file #149

Closed
yspotts opened this issue Nov 30, 2016 · 26 comments
Closed

Can't fetch local file #149

yspotts opened this issue Nov 30, 2016 · 26 comments

Comments

@yspotts
Copy link

yspotts commented Nov 30, 2016

My apologies if this is spelled out somewhere -- I looked through the issues.

I can't seem to direct ReDoc to load my OpenAPI spec from the local filesystem -- only via http. I have tried the following:

<redoc spec-url='swagger.json'></redoc>
<redoc spec-url='file:///Full/Path/swagger.json'></redoc>

in addition to other permutations. However, they all result in this error:

Unable to resolve $ref pointer "file:///<pathname to my spec file>"

Is there any way to have ReDoc load the spec locally?

@RomanHotsiy
Copy link
Member

RomanHotsiy commented Nov 30, 2016

The reason is that the lib I use (json-schema-ref-parser) doesn't support file:/// protocol.

I will fix this but this is not on the next-to-do queue coz this can be workarounded by simply running webserver

@yspotts
Copy link
Author

yspotts commented Nov 30, 2016

@RomanGotsiy thanks for the quick reply! got it

@visheshCuriousDev
Copy link

@RomanGotsiy Can you tell me how it can be worked around ? Sorry for asking this but i am new to this !!

@yspotts How else did you mention your json file ?

@RomanHotsiy
Copy link
Member

@vishesh1king just serve your spec and html page via any http-server (e.g. python -m SimpleHTTPServer) so url to it will be: http://127.0.0.1:<someport>/swagger.json not file:///<path>/swagger.json

@visheshCuriousDev
Copy link

visheshCuriousDev commented Dec 27, 2016

Got it thanks ...
But do you have any format on the json sytax ??
I am uable to load my custom json file ... Its says
" Oops... ReDoc failed to render this spec
this._schema.info is undefined "

This is my main.json file:
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/articles/1"
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}

@RomanHotsiy
Copy link
Member

@vishesh1king your json file is not in OpenAPI (fka Swagger) format. ReDoc is OpenAPI/Swagger-generated API Reference Documentation. It works only with OpenAPI specs

@visheshCuriousDev
Copy link

visheshCuriousDev commented Dec 27, 2016

Ohh, I get it now !!! Thanks ...
I saw the file u mentioned in the example "http://petstore.swagger.io/v2/swagger.json" but its in single line ... Could not understand
Do you have any json file that is cleanly indented and works ? I am having trouble at this ? Link to any json file on net that works ?
Thanks again!!

@RomanHotsiy
Copy link
Member

ReDoc demo spec in yaml format

If you are not familiar with OpenAPI you can start from template spec (direct link). You should also check series of tutorials by API Handyman: http://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-1-introduction/

@nthx
Copy link

nthx commented Apr 12, 2017

workaround to have a web server will not work if one needs to send a static html/js documentation to a person that only has a browser ...

@hmasila
Copy link

hmasila commented Jul 26, 2017

Here is what worked for me.

  1. Create OpenAPI/Swagger in swagger.io
  2. Download OpenAPI as JSON
  3. Create a Node.js app with Express. - or any server you are comfortable with
  4. Set an endpoint in the server.js that points to a middleware that mocks the json
  5. Add the newly created endpoint to the <redoc>"http://localhost:5555/api/v1/test</redoc>
  6. Fire up your server
  7. Go to the route where you added redoc
  8. Sit back, relax and enjoy!!

@nthx
Copy link

nthx commented Jul 31, 2017

@hmasila the reporter clearly stated that the "http://" works for him (and for others too).

This ticket is about "file://" protocol support.
So that we can send a "ZIP" to a person that doesn't have knowledge to run a web server on their computer, or simply has no time to do it.

@hmasila
Copy link

hmasila commented Jul 31, 2017

@nthx I came across this thread when I was working with a local json file. It took me a while to pass it to the redoc tag as http. That is why I wrote my comment. To help any other person who might be in the same situation.

@fredbi
Copy link

fredbi commented Oct 29, 2017

Same issue here.
@RomanGotsiy I have seen you fixed the issue in (json-schema-ref-parser) and asked the pull request to be merged. Thank you so much. Keep us posted.

Fred

@DavidMCook
Copy link

Hi guys, any news on this merge? I'm one of those people that doesn't want to/have time to set up a local server just to add a local file ref.

@fredbi
Copy link

fredbi commented Dec 6, 2017

I used json-schema-ref-parser recently and it works fine with local refs, even without the "file://" url prefix.

However json-schema-ref-parser.Bundle() is not perfect from resolving remote $refs for swagger, from a documentation point of view. But at least it shoudl work on local refs.

@RomanHotsiy
Copy link
Member

I have investigated this issue and it seems loading local swagger spec is impossible without running web-server due to same-origin policy and security reasons.

I found other projects have the same issue not resolved, e.g.: https://threejs.org/docs/#manual/introduction/How-to-run-thing-locally.

workaround to have a web server will not work if one needs to send a static html/js documentation to a person that only has a browser ...

I absolutely understand the use case though. There are two possible solutions to this use case:

  • upload both html and spec to any static hosting so a person with a browser can access it
  • inject spec directly into html so ReDoc doesn't have to do any network requests (the spec should be bundled beforehand). This variant should be pretty easy after React rewrite is finalized: WIP: Initial React rewrite #357. I think it may be a good idea for an open-source tool. If anyone is willing to write it, let me know and will try to get you started.

If anyone though does know how to load local swagger spec, let me know!

I'm not closing this issue until F.A.Q. is created and this is included there.

@philsturgeon
Copy link
Contributor

This would be another argument for rolling redocup into v2.0 as a cli feature. Then the FAQ can read:

Local files will not work, they need a file server to run due to [bla]. If you are working locally and want to see what your redoc will look like, simply use the cli tool: yarn run redoc serve.

Or something. :)

@7hibault
Copy link

7hibault commented Feb 2, 2018

Hi folks, this would be a very useful feature indeed.

Spectacle features this static generation. I'm not sure how it works in detail but maybe it'd be worth checking it out. I think they generate the html on the fly. I'm not familiar enough with ReDoc to know how much this approach is applicable

@RomanHotsiy
Copy link
Member

redoc-cli has an ability to bundle spec into zero-dependency HTML file.
Docs here: https://github.com/Rebilly/ReDoc/tree/master/cli

@monolithed
Copy link

monolithed commented Sep 7, 2018

@RomanGotsiy how to bundle spec using redoc-cli with custom options and local static files?

<link href="./static/fonts.css" rel="stylesheet" />  <!-- 404 -->
<script src="./static/redoc.standalone.js"> </script>  <!-- 404 -->

<script>
Redoc.init('./scheme/methods.yaml', { // 404
	theme: {
		noAutoAuth: true,
		requiredPropsFirst: true,
		breakpoints: {
			medium: 2
		}
	}
}, document.getElementById('container'))
</script>

@monolithed
Copy link

monolithed commented Sep 7, 2018

These problems for development environment were solved by serve command:

{
	"scripts": {
		"start": "serve"
	},
	"dependencies": {
		"redoc": "^2.0.0-alpha.38",
		"serve": "^10.0.0"
	}
}

But how to build these files into one file — that's a problem!

@RomanHotsiy
Copy link
Member

From redoc-cli help:

$ redoc-cli bundle --help

redoc-cli bundle <spec>

bundle spec into zero-dependency HTML-file

Positionals:
  spec  path or URL to your spec                                      [required]

Options:
  --help          Show help                                            [boolean]
  --version       Show version number                                  [boolean]
  -t, --template  Path to handlebars page template, see https://git.io/vh8fP
                  for the example                                       [string]
  --options       ReDoc options, use dot notation, e.g. options.nativeScrollbars
  -o, --output    Output file            [string] [default: "redoc-static.html"]
  --title         Page Title           [string] [default: "ReDoc documentation"]
  --cdn           Do not include ReDoc source code into html page, use link to
                  CDN instead                         [boolean] [default: false]

Use --options to provide options:

redoc-cli ... --options.requiredPropsFirst --options.theme.breakpoints.medium=2

or

 redoc-cli ... --options="{\"requiredPropsFirst": true }"

For template use --template, check the default template for the reference: https://git.io/vh8fP

@okeyokoro
Copy link

okeyokoro commented Mar 16, 2020

passing the local spec_file path to the redoc docker container is also a really convenient route (if you have docker installed)

docker run -it --rm -p 81:80 \
  -v $(pwd)/<SPEC_FILE>:/usr/share/nginx/html/<SPEC_FILE> \
  -e SPEC_URL=<SPEC_FILE> redocly/redoc

# visit localhost:81 in your browser

source https://hub.docker.com/r/redocly/redoc

^ for future viewers

@cah-alexsukhodolsky
Copy link

cah-alexsukhodolsky commented Apr 3, 2020

I just found a neat little web server that runs in your browser. It can easily point to a local folder and serve up your http file with redoc standalone with a reference to a local swagger file:

https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb?hl=en

@harsh4870
Copy link

passing the local spec_file path to the redoc docker container is also a really convenient route (if you have docker installed)

docker run -it --rm -p 81:80 \
  -v $(pwd)/<SPEC_FILE>:/usr/share/nginx/html/<SPEC_FILE> \
  -e SPEC_URL=<SPEC_FILE> redocly/redoc

# visit localhost:81 in your browser

source https://hub.docker.com/r/redocly/redoc

^ for future viewers

how to access multi ple local files ?

@volbrene
Copy link

@harsh4870 This repo would help you: https://github.com/volbrene/redoc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests