I'm trying to open my self generated swagger specification file my.json
with swagger-ui on my local computer.
So I downloaded the latest tag v2.1.8-M1 and extracted the zip. Then I went inside the sub folder dist
and copied the file my.json
into it. Now I opened the index.html
and want to explore my.json
. And here the problem begins:
If I enter a local path, it always will be prefixed by the current url containing the index.html
. And therefor I can't open my file. I tried all following combinations without success:
my.json
leads tofile:///D:/swagger-ui/dist/index.html/my.json
file:///D:/swagger-ui/dist/my.json
leads tofile:///D:/swagger-ui/dist/index.html/file:///D:/swagger-ui/dist/my.json
I could not get Adam Taras's answer to work (i.e. using the relative path ../my.json
).
Here was my solution (pretty quick and painless if you have node installed):
- With Node, globally install package http-server
npm install -g http-server
- Change directories to where my.json is located, and run the command
http-server --cors
(CORS has to be enabled for this to work) - Open swagger ui (i.e. dist/index.html)
- Type
http://localhost:8080/my.json
in input field and click "Explore"
Use the spec parameter.
Instructions below.
Create spec.js file containg Swagger JSON
Create a new javascript file in the same directory as index.html (/dist/)
Then insert spec
variable declaration:
var spec =
Then paste in the swagger.json file contents after. It does not have to be on the same line as the =
sign.
Example:
var spec =
{
"swagger": "2.0",
"info": {
"title": "I love Tex-Mex API",
"description": "You can barbecue it, boil it, broil it, bake it, saute it. Dey's uh, Tex-Mex-kabobs, Tex-Mex creole, Tex-Mex gumbo. Pan fried, deep fried, stir-fried. There's pineapple Tex-Mex, lemon Tex-Mex, coconut Tex-Mex, pepper Tex-Mex, Tex-Mex soup, Tex-Mex stew, Tex-Mex salad, Tex-Mex and potatoes, Tex-Mex burger, Tex-Mex sandwich..",
"version": "1.0.0"
},
...
}
}
Modify Swagger UI index.html
This is a two-step like Ciara.
Include spec.js
Modify the /dist/index.html file to include the external spec.js
file.
<script src='spec.js' type="text/javascript"></script>
Example:
<!-- Some basic translations -->
<!-- <script src='lang/translator.js' type='text/javascript'></script> -->
<!-- <script src='lang/ru.js' type='text/javascript'></script> -->
<!-- <script src='lang/en.js' type='text/javascript'></script> -->
<!-- Original file pauses -->
<!-- Insert external modified swagger.json -->
<script src='spec.js' type="text/javascript"></script>
<!-- Original file resumes -->
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://petstore.swagger.io/v2/swagger.json";
}
Add spec parameter
Modify the SwaggerUi instance to include the spec
parameter:
window.swaggerUi = new SwaggerUi({
url: url,
spec: spec,
dom_id: "swagger-ui-container",
After a bit of struggle, I found a better solution.
create a directory with name: swagger
mkdir C:\swagger
If you are in Linux, try:
mkdir /opt/swagger
get swagger-editor with below command:
git clone https://github.com/swagger-api/swagger-editor.git
go into swagger-editor directory that is created now
cd swagger-editor
now get swagger-ui with below command:
git clone https://github.com/swagger-api/swagger-ui.git
now, copy your swagger file, I copied to below path:
./swagger-editor/api/swagger/swagger.json
all setup is done, run the swagger-edit with below commands
npm install npm run build npm start
You will be prompted 2 URLs, one of them might look like:
http://127.0.0.1:3001/
Above is swagger-editor URL
Now browse to:
http://127.0.0.1:3001/swagger-ui/dist/
Above is swagger-ui URL
Thats all.
You can now browse files from either of swagger-ui or swagger-editor
It will take time to install/build, but once done, you will see great results.
It took roughly 2 days of struggle for me, one-time installation took only about 5 minutes.
Now, on top-right, you can browse to your local file.
best of luck.
In a local directory that contains the file ./docs/specs/openapi.yml
that you want to view, you can run the following to start a container and access the spec at http://127.0.0.1:8246
.
docker run -t -i -p 8246:8080 -e SWAGGER_JSON=/var/specs/openapi.yml -v $PWD/docs/specs:/var/specs swaggerapi/swagger-ui
What works, is to enter a relative path or an absolute without the file://
-protocol:
../my.json
leads tofile:///D:/swagger-ui/dist/index.html/../my.json
and works/D:/swagger-ui/dist/my.json
leads tofile:///D:/swagger-ui/dist/my.json
and works
HINT
This answer works with Firefox on Win7. For Chrome-Browser, see comments below:
I had that issue and here is much simpler solution:
- Make a dir (eg: swagger-ui) in your public dir (static path: no route is required) and copy dist from swagger-ui into that directory and open localhost/swagger-ui
- You will see swagger-ui with default petstore example
Replace default petstore url in dist/index.html with your localhost/api-docs or to make it more generalized, replace with this:
location.protocol+'//' + location.hostname+(location.port ? ':' + location.port: '') + "/api-docs";
Hit again localhost/swagger-ui
Voila! You local swagger implementation is ready
My environment,
Firefox 45.9 ,
Windows 7
swagger-ui ie 3.x
I did the unzip and the petstore comes up fine in a Firefox tab. I then opened a new Firefox tab and went to File > Open File and opened my swagger.json file. The file comes up clean, ie as a file.
I then copied the 'file location' from Firefox ( ie the URL location eg: file:///D:/My%20Applications/Swagger/swagger-ui-master/dist/MySwagger.json ).
I then went back to the swagger UI tab and pasted the file location text into the swagger UI explore window and my swagger came up clean.
Hope this helps.
LINUX
I always had issues while trying paths and the spec parameter.
Therefore I went for the online solution that will update automatically the JSON on Swagger without having to reimport.
If you use npm to start your swagger editor you should add a symbolic link of your json file.
cd /path/to/your/swaggerui where index.html is
ln -s /path/to/your/generated/swagger.json
You may encounter cache problems. The quick way to solve this was to add a token at the end of my url...
window.onload = function() {
var noCache = Math.floor((Math.random() * 1000000) + 1);
// Build a system
const editor = SwaggerEditorBundle({
url: "http://localhost:3001/swagger.json?"+noCache,
dom_id: '#swagger-editor',
layout: 'StandaloneLayout',
presets: [
SwaggerEditorStandalonePreset
]
})
window.editor = editor
}
Instead of opening swagger ui as a file - you put into browser file:///D:/swagger-ui/dist/index.html you can: create iis virtual directory which enables browsing and points to D:/swagger-ui
- open mmc, add iis services, expand Default Web Site add virtual directory, put alias: swagger-ui, physical path:(your path...) D:/swagger-ui
- in mmc in the middle pane double click on "directory browsing"
- in mmc in the right pane click "enable"
- after that in browser put url to open your local swagger-ui http://localhost/swagger-ui/dist/
- now you can use ../my.json if you copied file into dist folder or you can created separate forlder for samples, say D:/swagger-ui/samples and use ../samples/my.json or http://localhost/swagger-ui/samples/my.json
This is how I worked with local swagger JSON
- Have tomcat running in local machine
- Download Swagger UI application and unzip it into tomcat's /webapps/swagger folder
- Drop local swagger json file inside /webapps/swagger folder of tomcat
- Start up tomcat (/bin/sh startup.sh)
- Open a browser and navigate to http://localhost:8080/swagger/
- type your swagger json file in the Swagger Explore test box and this should render the APIs.
Hope this works for you
Yet another option is to run swagger using docker, you can use this docker image:
https://hub.docker.com/r/madscientist/swagger-ui/
I made this ghetto little BASH script to kill running containers and rebuild, so basically each time you make a change to your spec and want to see it, just run the script. Make sure to put the name of your application in the APP_NAME variable
#!/bin/bash
# Replace my_app with your application name
APP_NAME="my_app"
# Clean up old containers and images
old_containers=$(docker ps -a | grep $APP_NAME | awk '{ print $1 }')
old_images=$(docker images | grep $APP_NAME | awk '{ print $3 }')
if [[ $old_containers ]];
then
echo "Stopping old containers: $old_containers"
docker stop $old_containers
echo "Removing old containers: $old_containers"
docker rm $old_containers
fi
if [[ $old_images ]];
then
echo "Removing stale images"
docker rmi $old_images
fi
# Create new image
echo "Creating new image for $APP_NAME"
docker build . -t $APP_NAME
# Run container
echo "Running container with image $APP_NAME"
docker run -d --name $APP_NAME -p 8888:8888 $APP_NAME
echo "Check out your swaggery goodness here:
http://localhost:8888/swagger-ui/?url=http://localhost:8888/swagger-ui/swagger.yaml"
With Firefox, I:
- Downloaded and unpacked a version of Swagger.IO to C:\Swagger\
- Created a folder called Definitions in C:\Swagger\dist
- Copied my
swagger.json
definition file there, and - Entered "Definitions/MyDef.swagger.json" in the Explore box
Be careful of your slash directions!!
It seems you can drill down in folder structure but not up, annoyingly.
来源:https://stackoverflow.com/questions/30400477/how-to-open-local-files-in-swagger-ui