question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using swagger with multiple API applications, swagger.json documents get mixed up

See original GitHub issue

We are running two separate Jersey APIs in the same server context, il.e. two separate classes extending com.sun.jersey.api.core.PackagesResourceConfig are configured in Tomcat 7’s web.xml to be two accessible under two separate base paths. Simplified it’s:

<servlet>
   <servlet-name>Api1</servlet-name>
   <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
   <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>api1.server.config.Api1ServerConfig</param-value>
   </init-param>
</servlet>

<servlet-mapping>
   <servlet-name>Api1</servlet-name>
   <url-pattern>/api1/*</url-pattern>
</servlet-mapping>

<servlet>
   <servlet-name>Api2</servlet-name>
   <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
   <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>api2.server.config.Api2ServerConfig</param-value>
   </init-param>
</servlet>

<servlet-mapping>
   <servlet-name>Api2</servlet-name>
   <url-pattern>/api2/*</url-pattern>
</servlet-mapping>  

The two APIs do not share any model classes or other code and are completely separate in every aspect incl security, filter chains etc. There are two separate application.wadl files (and their corresponding XSD schema) generated by Jersey, each describing only the resources from their respective API.

We are using swagger in the first API, configuring it using BeanConfig:

BeanConfig bc = new BeanConfig();
bc.setVersion("1.0");
bc.setTitle(" API1 Documentation");
bc.setSchemes(new String[]{"https"});
bc.setBasePath("/appRoot/api1");
bc.setResourcePackage("api1.resource");
bc.setScan(true);

Now we try to add Swagger to the second API as well, by declaring this in our Api2ServerConfig:

BeanConfig bc = new BeanConfig();
bc.setVersion("1.0");
bc.setTitle(" API2 Documentation");
bc.setSchemes(new String[]{"https"});
bc.setBasePath("/appRoot/api2");
bc.setResourcePackage("api2.resource");
bc.setScan(true);

Unfortunately while this results in two separate swagger.json documents under /appRot/api1/swagger.json and /appRoot/api2/swagger.json, the two APIs are not treated separately as we had hoped.

The contents of whichever swagger document we access first after starting the server are fine. If we access /appRoot/api1/swagger.json, it contains all the paths and types of API1 and nothing from API2’s realm. If however we next access /appRoot/api2/swagger.json, it lists both its own resources AND every tag, path and type definition from API1. Vice versa, if /appRoot/api2/swagger.json is accessed first and then appRoot/api1/swagger.json, API2’s document is correct, but itself is now leaking into API1’s swagger.json. The title and basePath are correct in both documents.

Is there anything else that needs to be done to keep multiple APIs from mixing, or is it just not possible at all?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kriegerklcommented, Jan 26, 2017

The Problem that is described in the first comment from andi-livn is still not solved. Should I open a new Ticket?

1reaction
fehguycommented, Apr 28, 2016

Looking at BeanConfig, it looks like you can have multiple packages, separated by ,. Can you please try that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using swagger with multiple API applications ... - GitHub
We are running two separate Jersey APIs in the same server context, ... multiple API applications, swagger.json documents get mixed up #2099.
Read more >
use multiple api docs for swagger in node js? - Stack Overflow
This is the Known issue in the Swagger UI. Please use the following format to route the request: var swaggerHtml = swaggerUi.
Read more >
OpenAPI Specification - Version 2.0 - Swagger
Version 2.0 specification defines a set of files required to describe an API. These files can then be used by the Swagger-UI project...
Read more >
Multipart Requests - Swagger
Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file...
Read more >
Documentation Portals for Multiple APIs | Swagger Blog
Lets put the swaggerhub function to use and create a new getOrganizationData call. This will take a single input parameter, our organization's ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found