How to integrate swagger-ui in my application - swagger

I am trying to integrate swagger with camel project
following this example https://github.com/smparekh/camel-example-servlet-rest-tomcat
How do i access swagger-ui using this example project ?
I delopyed the war file in tomcat.
and access http://localhost:8080/camel-example-servlet-rest-tomcat/api-docs i get this ...
{"apiVersion":"1.2.3","swaggerVersion":"1.2","apis":[{"path":"/user","description":"User rest service"}],"info":{"title":"User Services","description":"Camel Rest Example with Swagger that provides an User REST service"}}
BUT MY QUESTION IS - how do i access swagger-ui/index.html?
what is the exact URL to access swagger-UI?

You must copy the contents of the dist folder of swagger-ui into your project's webapp folder.
In index.html,
window.swaggerUi = new SwaggerUi({
url: "http://petstore.swagger.wordnik.com/api/api-docs",
dom_id: "swagger-ui-container",
you must replace url with this
http://localhost:8080/camel-example-servlet-rest-tomcat/api-docs
For details, Follow this link to integrate swagger-ui.
https://github.com/swagger-api/swagger-ui

You should use http://localhost:${port}/${contextPath}/swagger/index.html

http://localhost:8080/camel-example-servlet-rest-tomcat/{basepath}/dist/index.html if you have copied dist folder as is. If you have renamed dist folder, use the new name instead of dist. replace basepath with basepath you have configured in web.xml. The code snippet for that looks like this:
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/rest</param-value>
</init-param>

To access swagger2 it is
http://localhost:${port}/${contextPath}/swagger-ui.html

These are your Swagger Docs:
{"apiVersion":"1.2.3","swaggerVersion":"1.2","apis":[{"path"...
Now you need Swagger-UI to consume them. You can install it anywhere. There is no hard requirement that you put Swagger-UI in your project. You just need to edit the index.html file to point to your docs path (the JSON output above.)

Related

connexion serve swagger ui at another url

We are using connexion to serve the swagger ui. We are using the openapi 3.0.0 specification. Here is a small part of our swagger.yml:
openapi: 3.0.0
servers:
- url: /
paths:
/resource:
...
/resource2:
...
in this case the ui is served at /ui. We are however using nginx to redirect all requests to /resource into this container. We would like swagger-ui to be served at /some-subdir/ui instead of at /ui, in order to be able to redirect the requests to the right container.
trial 1
openapi: 3.0.0
servers:
- url: /app
paths:
/resource:
...
/resource2:
...
which works, except that the resources are now served at /app/resource etc, while the same resource might in the future be served by another app, so we don't want the app name to appear in the URL of the resources (while it might be acceptable just for the swagger-ui).
trial 2
I found that, when constructing the connexion app, I could specify the swagger_url option:
options = {
'swagger_url': '/app/ui'
}
connexion_app = connexion.App(__name__, specification_dir='./', options=options)
now the swagger-ui is served at /app/ui, but the ui is trying to serve /openapi.json which is not reachable since not under /app (or any other subdir).
Almost there, there is another (well hidden) option to change the path to the openapi.json, the combination with swagger_url works:
options = {
'swagger_url': '/app/ui',
'openapi_spec_path': '/app/openapi.json'
}
connexion_app = connexion.App(__name__, specification_dir='./', options=options)

SpringDoc/Swagger behind an nginx proxy

We are running a service behind an nginx proxy so that:
http://service-post:8080/swagger-ui.html is routed to public address https://host.com/services/post/swagger-ui.html
Or to define from the other way:
When nginx receives request on https://host.com/services/post/swagger-ui.html, it strips the /services/post/ prefix and passes the request to the post service on /swagger-ui.html path.
Before setting up anything (with default SpringDoc configuration) I can correctly see the swagger docs on http://service-post:8080/swagger-ui.html.
To set the paths for the public address on host.com, I am using:
springdoc.api-docs.path: /services/post/api-docs
springdoc.swagger-ui.path: /services/post/swagger-ui.html
springdoc.swagger-ui.configUrl: /services/post/v3/api-docs/swagger-config
However it seems that this brakes it completely:
/swagger-ui.html, /api-docs and /v3/api-docs/swagger-config return 404 both for service-post:8080/* and https://host.com/services/post/*
Only thing that seems to work is https://host.com/services/post/swagger-ui/index.html which shows the petstore documentation.
We are not using Spring Boot, just Spring MVC of version 5.3.1.
So how do I set up to keep the handling of the original paths (eg. /api-docs), but performing the lookup on the prefixed path (/services/post/api-docs)?
In the end I completely ignore the default redirect:
swagger-ui.html -> `swagger-ui/index.html?url=/v3/api-docs
And implemented my own one:
docs -> swagger-ui/index.html?url=MY_PREFIX/v3/api-docs
This way I don't need to change anything and everything works with default settings.
It's all documented here:
https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-ui-behind-a-reverse-proxy
If you are not using spring-boot, you can add the ForwardedHeaderFilter bean:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/ForwardedHeaderFilter.html

context path is not getting in URL

Iam using spring-security-core 3.1.1 in my grails 3.2.7 application. I have added the following code in my application.yml
server:
contextPath: /LGP
port: 9090
So the appcode LGP will be appended to my URL. eg: http://localhost:9090/LGP/home
But the problem is when i'm using the Projects tag, the URL is becoming http://localhost:9090/home , the app code (LGP) getting skipped from the URL.
Why this is happening?
For the time being i update the tag as Projects but this gives the URL only as
lgp/projects
the http://localhost:9090/ section is getting missed.
Why this is like this?
If you're linking to a controller use:
<g:link controller="projects">Projects</g:link>
If you're linking to e.g. a static resource use:
<g:link uri="/projects">Projects</g:link>

Google Vision API authentication on heroku

What is a best, simple way to authenticate Vision API on heroku?
In development I just use:
#vision = Google::Cloud::Vision.new( project: "instacult",
keyfile: "path/to/keyfile.json" )
Where keyfile is a json produced by google after creating service account (https://cloud.google.com/vision/docs/common/auth).
But obviously I can't just upload the keyfile to github.
I tried saving whole json to Heroku's config vars and running:
Rails.env.production? ? ENV["GOOGLE_CREDENTIALS"] : path
But I got "is not a valid file" in heroku's logs. Seems logical since I'm not passing a file but an object. But how to get over it?
Cheers,
Kai
SOLVED:
Turns out you can provide a json object in environment variable, but there is a naming convention.
Here are the environment variables (in the order they are checked) for
credentials:
VISION_KEYFILE - Path to JSON file
GOOGLE_CLOUD_KEYFILE - Path to JSON file
VISION_KEYFILE_JSON - JSON contents
GOOGLE_CLOUD_KEYFILE_JSON - JSON contents
source: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-vision/v0.23.0/guides/authentication
So I ended up with calling:
#vision = Google::Cloud::Vision.new( project: "instacult")
Having set VISION_KEYFILE_JSON in my ~/.bashrc:
export VISION_KEYFILE_JSON='the_json_content'
and on heroku (https://devcenter.heroku.com/articles/config-vars#limits).

Mule HTTP Request Config with OAuth2

I am experimenting with OAuth2 on HTTP request connector. It is throwing the below exception always:
SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'oauth2:authorization-code-grant-type'. One of '{"http://www.mulesoft.org/schema/mule/core":annotations, "http://www.mulesoft.org/schema/mule/http":abstract-http-request-authentication-provider, "http://www.mulesoft.org/schema/mule/tcp":client-socket-properties, "http://www.mulesoft.org/schema/mule/tls":context, "http://www.mulesoft.org/schema/mule/http":raml-api-configuration, "http://www.mulesoft.org/schema/mule/http":proxy, "http://www.mulesoft.org/schema/mule/http":ntlm-proxy}' is expected
Here is my configuration:
<http:request-config name="SF_Authorize_Configuration" protocol="HTTPS" host="${login.host}" basePath="${oauth2.url}" port="80" doc:name="Authorize Configuration" >
<oauth2:authorization-code-grant-type clientId="my_client_id" clientSecret="my_client_secret" redirectionUrl="http://localhost:8081/oauth2callback">
<oauth2:authorization-request authorizationUrl="https://my.api.com/services/oauth2/authorize" localAuthorizationUrl="http://localhost:8082/authorization" scopes="access_user_details, read_user_files">
</oauth2:authorization-request>
<oauth2:token-request tokenUrl="https://my.api.com/services/oauth2/token"/>
</oauth2:authorization-code-grant-type>
</http:request-config>
This means that you have not provided the xml namespace for the xml tag.
If you have not used the UI to create this then please create using the design and then you can copy and paste your specific tag later by replacing it.
Edited answer,
It was similar for me for API kit. I re installed the Studio(Unzipped it again). This might work .
I have encountered the same issue. It was resolved by adding oauth2 namespace at the start mule tag, e.g.:
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
...
xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2"
http://www.mulesoft.org/schema/mule/oauth2
...
>

Resources