swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Same swagger.json for multiple servlets

Open puneet-warikoo opened this issue 9 years ago • 4 comments

I am having 2 servlets in my Jersey application at 2 different url patterns

V1: api/v1 V2: api/v2

I also have swagger config for both of them per the web.xml below

<display-name>demo</display-name>

<servlet>
   <servlet-name>apiv1</servlet-name>
   <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        
    <init-param>
       <param-name>jersey.config.server.provider.packages</param-name>
     <param-value>io.swagger.jaxrs.listing,
     com.puneetw.services.controllers.v1
     </param-value>
    </init-param>
 <init-param>
  <param-name>swagger.api.basepath</param-name>
  <param-value>JaxRS/api/v1</param-value>
</init-param>
 <init-param>
  <param-name>swagger.use.path.based.config</param-name>
  <param-value>true</param-value>
</init-param>
<init-param>
  <param-name>swagger.config.id</param-name>
  <param-value>JaxRS-api-v1</param-value>
</init-param>
<init-param>
  <param-name>swagger.scanner.id</param-name>
  <param-value>JaxRS-api-v1</param-value>
</init-param>    
    <load-on-startup>1</load-on-startup>
</servlet>

 <servlet-mapping>
   <servlet-name>apiv1</servlet-name>
   <url-pattern>/api/v1/*</url-pattern>
</servlet-mapping>
apiv2 org.glassfish.jersey.servlet.ServletContainer
    <init-param>
       <param-name>jersey.config.server.provider.packages</param-name>
     <param-value>io.swagger.jaxrs.listing,
     com.puneetw.services.controllers.v2
     </param-value>
    </init-param>
    <init-param>
  <param-name>swagger.api.basepath</param-name>
  <param-value>JaxRS/api/v2</param-value>
</init-param>
swagger.config.id JaxRS-api-v2 swagger.scanner.id JaxRS-api-v2
  <init-param>
     <param-name>swagger.use.path.based.config</param-name>
  <param-value>true</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>
   <servlet-name>apiv2</servlet-name>
   <url-pattern>/api/v2/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DefaultJaxrsConfig</servlet-name>
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig </servlet-class>
<init-param>
  <param-name>api.version</param-name>
  <param-value>1.0.0</param-value>
</init-param>
<init-param>
  <param-name>swagger.api.title</param-name>
  <param-value>Swagger Petstore 1</param-value>
</init-param>
<init-param>
  <param-name>swagger.api.basepath</param-name>
  <param-value>JaxRS/api/v1</param-value>
</init-param>
 <init-param>
  <param-name>swagger.use.path.based.config</param-name>
  <param-value>true</param-value>
</init-param>
<init-param>
  <param-name>swagger.config.id</param-name>
  <param-value>JaxRS-api-v1</param-value>
</init-param>
<init-param>
  <param-name>swagger.scanner.id</param-name>
  <param-value>JaxRS-api-v1</param-value>
</init-param>    
<load-on-startup>2</load-on-startup>
DefaultJaxrsConfig2 io.swagger.jaxrs.config.DefaultJaxrsConfig api.version 2.0.0 swagger.api.title Swagger Petstore 1 swagger.api.basepath JaxRS/api/v2 swagger.use.path.based.config true
<init-param>
  <param-name>swagger.config.id</param-name>
  <param-value>JaxRS-api-v2</param-value>
</init-param>
<init-param>
  <param-name>swagger.scanner.id</param-name>
  <param-value>JaxRS-api-v2</param-value>
</init-param>    
<load-on-startup>2</load-on-startup>

Everytime i hit the swagger.json endpoint at either api/v1 or api/v2 both of them give same response (The first one hit gets kind of stuck in the system)

Below are my dependencies: dependencies { compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.1-m03'

 compile "org.glassfish.jersey.core:jersey-server:2.22.1"

 compile "org.glassfish.jersey.containers:jersey-container-servlet:2.22.1"
 compile "org.glassfish.jersey.media:jersey-media-json-jackson:2.22.1"
 compile "joda-time:joda-time:2.9.7"
 compile "io.swagger:swagger-jersey2-jaxrs:1.5.0"  

}

puneet-warikoo avatar Feb 14 '17 08:02 puneet-warikoo

Same issue happening for me. Maybe SwaggerContextService.USE_PATH_BASED_CONFIG needs to be used.

ustaDAH avatar Mar 02 '17 02:03 ustaDAH

Same issue here. i already tried setting usePathBasedConfig. But there wasn't any noticeable change. The Problem occurs not only on nested root-contexts. same error comes along when removing same root context. e.G. "/v1" and "/v2" without the "/api". are there any other tipps?

EDIT: My fault. The Solution is like @ustaDAH said... make sure you set following properties using annotations, web.xml or any other mechanism.

  • swagger.scanner.id
  • swagger.config.id
  • swagger.use.path.based.config

In my case the web.xml was the solution due the lack of a newer Version of jboss-jaxrs-api which supports annotations. I think this is also the reason why BeanConfig in Application never worked out for me.

I'm now pretty sure, that you @puneet-warikoo might have a problem with your dependencies and/or servlet and servlet config setup. I would recommend downloading the sources and start debugging BaseApiListingResource to check which Config is beeing loaded.

Some further information..

Example of web.xml for one instance

<servlet>
	<servlet-name>inumbers-application</servlet-name>
	<servlet-class>de.[..].YourApplicationOrServletClass</servlet-class>
	<init-param>
		<param-name>swagger.scanner.id</param-name>
		<param-value>inumbers</param-value>
	</init-param>
	<init-param>
		<param-name>swagger.config.id</param-name>
		<param-value>inumbers</param-value>
	</init-param>
	<init-param>
		<param-name>swagger.use.path.based.config</param-name>
		<param-value>true</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>inumbers-application</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>

The Reason for all of this can be found in io.swagger.jaxrs.listing.BaseApiListingResource. Swagger "Caches" the Data in an ConcurrentHashMap which is accessed in following ways.

if (SwaggerContextService.isScannerIdInitParamDefined(sc)) {
	if (!initializedScanner.containsKey(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc))) {
		swagger = scan(app, servletContext, sc, uriInfo);
	}
} else {
	if (SwaggerContextService.isConfigIdInitParamDefined(sc)) {
		if (!initializedConfig.containsKey(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc))) {
			swagger = scan(app, servletContext, sc, uriInfo);
		}
	} else if (SwaggerContextService.isUsePathBasedConfigInitParamDefined(sc)) {
		if (!initializedConfig.containsKey(sc.getServletName() + "_" + ctxService.getBasePath())) {
			swagger = scan(app, servletContext, sc, uriInfo);
		}
	} else if (!initialized) {
		swagger = scan(app, servletContext, sc, uriInfo);
	}
}

full source

atbostudio avatar Jul 12 '17 11:07 atbostudio

@ATBO Thanks a lot for the tip on debugging BaseApiListingResource. By doing that I got to see that I had some dependencies problems. My code accessed the 1.5.0 version that didn't had scanner.id, config.id etc. I was driving me mad, thanks!!! :)

ghost avatar Sep 18 '17 10:09 ghost

Closing this issue as part of our backlog cleanup effort.
This issue is outdated and/or inactive for a long time, and we’re not able to revisit it meaningfully.

If this problem is still relevant, please open a new issue and reference this one for context.
Thank you for your understanding!

MichakrawSB avatar Mar 08 '26 19:03 MichakrawSB