How to expose swagger.json with go-swagger server? - swagger

I'm using go-swagger to generate the API server. I noticed that the json generated from swagger.yml is being kept in restapi/embedded_spec.go.
What's the best way to expose that JSON spec so my ReactJS client can access it?
So far I've had to use swagger serve swagger.yml --port=50000 and point javacript client to localhost:50000/swagger.json. I'm looking for a way to serve that JSON straight from restapi/embedded_spec.go via my API.

Maybe this is from an old code point of view, but currently when running a server the swagger.json file is provided as well.
$ go run cmd/swagger-petstore-server/main.go --port=50000
2018/09/20 12:48:35 Serving swagger petstore at http://127.0.0.1:50000
$ curl http://127.0.0.1:50000/swagger.json
{
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
...

Related

How to render the response body in Swagger UI?

I have a simple GET API. When I click "Try it out" and then "Execute" in Swagger UI, I can see the request reaching the server and getting processed successfully with HTTP status code 200 - ascertained by looking into the server logs.
I would like to see the response body in Swagger UI. What addition or change needs to be done to the OpenAPI YAML spec? I'm using OpenAPI 3.0.3.
By enabling CORS in my flask app, I have managed to overcome the issue ...
From shell prompt ...
pip install -U flask-cors
Inside app.py ...
...
from flask_cors import CORS
...
app = Flask(__name__)
CORS(app)
...

Swagger doesn't found the yaml file (404)

I am using swagger to show my open API YAML file, it suddenly gives me "response status is 404 /openapi.yaml" when I run the spring boot application.
Note : I can open the /api-docs/ in swagger and it shows the apis, also when I hit the swagger config URL (http://localhost:8080/api-docs/swagger-config), it gives me the below response, which include the openapi.yaml file
{"configUrl":"/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8080/swagger-ui/oauth2-redirect.html","url":"/openapi.yaml","validatorUrl":""}
The problem happened only in my machine, other colleagues' machines work fine with the same codebase.
Deps:
org.springdoc:springdoc-openapi-ui:1.6.11
org.springdoc:springdoc-openapi-javadoc:1.6.11
org.springdoc:springdoc-openapi-data-rest:1.6.11
org.springdoc:springdoc-openapi-security:1.6.11
io.swagger:swagger-annotations:1.6.6
org.openapitools:jackson-databind-nullable:0.2.2
io.swagger.parser.v3:swagger-parser:2.0.32
org.openapi.generator version "5.4.0" // open API generator plugin

Django rest framework swagger missing port when run in docker

I am using rest framework swagger for the first time in my Django application. When I run it locally from PyCharm it works just fine. My app runs on port 1337 and when I Try Out my restful API endpoint and click Execute, the curl command works and the URL includes the port.
The issue is when I run my Django app in a Docker. In this case, the URL in the curl command does not include the port. Do I have to add any swagger specific configuration to my docker compose file? I have not changed my Dockerfile nor my docker-compose file at all.
What do I need to do to get this to work properly?
It seems like you have set the url parameter for the get_schema_view(...)--(DRF Doc) function. The url is used to set the canonical base URL for the schema.
If you didn't set the value, DRF will use the "requested host" as the default URL.
Ref
url = self.url
if not url and request is not None:
url = request.build_absolute_uri()
SO, you can set the url to any value or you can exclude the parameter to get the default behavior.
from rest_framework.schemas import get_schema_view
urlpatterns = [
path('openapi', get_schema_view(
title="Your Project",
description="API for all things …",
version="1.0.0"
), name='openapi-schema'),
]
Please check the file urls.py which is the place you are setting URL for SWAGGER.
Don't set url in get_schema_view function.
Also, when you are running the app by using docker, the URL in the curl command does not include the port -> That's right!
I think that your file is using multiple settings for swagger by checking like this:
if getattr(settings, "SETTINGS_ENV", None) in ["local", "dev", "draft", "production"]:

Nuxt environment variables exposed in client when uploaded to Zeit/Now

I am deploying a Nuxt App with Zeit/Now. In the development phase I was using a .env file to store the secrets to my Contentful CMS, exposing the secrets to process.env with the nuxt-dotenv package. To do that, at the top of the nuxt.config I was calling require('dotenv').config().
I then stored the secrets with Zeit/Now and created a now.json to set them up for build and runtime like so:
{
"env": {
"DEMO_ID": "#demo_id"
},
"build": {
"env": {
"DEMO_ID": "#demo_id"
}
}
}
With that setup, the build was only working for the index page and all of the Javascript did not function. Only when I added the env-property to the nuxt.config.jsfile, the app started working properly on the Zeit-server.
require('dotenv').config()
export default {
...
env: {
DEMO_ID: process.env.DEMO_ID
},
...
modules: [
'#nuxtjs/dotenv'
],
...
}
BUT: When I then checked the uploaded Javascript files, my secrets were exposed, which I obviously don't want.
What am I doing wrong here? Thanks for your help.
You aren't necessarily doing anything wrong here, this is just how Nuxtjs works.
Variables declared in the env property are used to replace instances of process.env.MY_ENV, but because Nuxt is isomoorphic, this can be both on the server and client.
If you want these secrets accessible only on the server, then the easiest way to solve this is to use a serverMiddleware.
As serverMiddleware is decoupled from the main Nuxt build, env variables defined in nuxt.config.js are not available there.
This means your normal ENV variables should be accessible, since the server middleware are run on Node.
Obviously, this means these secrets won't be available client side, but this works if you have something like a Stripe secret key that you need to make backend requests with.
We had a similar problem in our project. Even, We created a nuxt project from scratch and checked to see if there was a situation we skipped. We noticed that, while nuxt building, it copies the .env variables into the utils.js in the nuxt folder. Through the document here, we changed the modules section in nuxt.config.js as follows,
modules: ['# nuxtjs / apollo', '# nuxtjs / axios', ['# nuxtjs / dotenv', { only: ['']}]],
Then we noticed that .env variables are not exposed.
I hope it helped.
Our nuxt version is "nuxt": "^ 2.13.0".
Also, some discussion over here.

swagger-tools on node: How to load swaggerUi?

When I hit http://localhost:3001/api-docs loads the swagger json docs.
{
swagger: "2.0",
info: {
version: "1.0.0",
title: "Auth-gateway services",
contact: {
name: "swagger docs",
url: "https://www.google.com"
}
},
host: "127.0.0.1:3001",
basePath: "/",
...
}
But how do I load UI like http://petstore.swagger.io/ for my APIs.
To view you api through swagger-ui, do one of the following.
Option 1: Using online swagger-ui
Go to this.
On the dialog-box on the top of the page, provide the url for swagger-json. In your case, insert http://localhost:3001/api-docs instead of http://petstore.swagger.io/v2/swagger.json (which can be seen in default) and click Explore.
Now you can see swagger-ui generated for your api.
Option 2: Setting up swagger-ui project locally
You have to set up swagger-ui. You can clone the project set up that with the below instructions provided.
Windows Users: Please install Python before follow below guidelines
for node-gyp rebuild to run.
1. npm install
2. npm run build
3. You should see the distribution under the dist folder. Open ./dist/index.html to launch Swagger UI in a browser
Development
Use npm run serve to make a new build, watch for changes, and serve the result at http://localhost:8080/.
Now you should be able to see something exactly like online swagger-ui.
Do the same as option 1 to provide swagger-json url and see swagger-ui generated.

Resources