swagger-api / swagger-api/swagger-core

Bootstrapped servlet returns empty JSON as of 2.2.24

Open
#4,854 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backlog
Dominant language
Java
Stars
7.5k
Forks
2.3k
Avg merge
18h 1m
Merged PRs (30d)
10

Description

Sorry if I've missed something in the docs, but our apps use a bootstrapped HTTP servlet to configure OpenAPI using JAX-RS and when we upgraded to Swagger 2.2.24 the servlet started producing an empty OpenAPI JSON payload.

Original code for our servlet is here (nothing proprietary about this):

// @formatter:off
@OpenAPIDefinition(
         info = info(title = "NextGen Web Services", description = "Web services for the NextGen application."),
         tags = {@Tag(name = OpenApiBootstrap.TAG_API, description = "Services to communicate with NextGen")},
         servers = @Server(url = "/nextgen"))
// @formatter:on
@WebServlet(name = "OpenAPI", description = "OpenAPI Spec Servlet", loadOnStartup = 2)
public final class OpenApiBootstrap extends HttpServlet {
   private static final long serialVersionUID = 1L;

   /** Tag for API web services. */
   public static final String TAG_API = "API";

   @Override
   public void init(final ServletConfig servletConfig) throws ServletException {
      try {
         new JaxrsOpenApiContextBuilder<>().servletConfig(servletConfig)
                  .openApiConfiguration(
                           new SwaggerConfiguration().resourcePackages(Collections.singleton("com.pjm.nextgen.rest")))
                  .buildContext(true);
      } catch (final OpenApiConfigurationException e) {
         throw new ServletException(e);
      }
   }
}

After upgrading to Swagger 2.2.24, the JSON file returned from the servlet is essentially empty:

{"openapi":"3.0.1"}

I noticed that the examples do not show using the OpenAPIDefinition annotation, so I tried translating that to just setting up the configurations in the Java code like this:

@WebServlet(name = "OpenAPI", description = "OpenAPI Spec Servlet", loadOnStartup = 2)
public final class OpenApiBootstrap extends HttpServlet {
   private static final long serialVersionUID = 1L;

   /** Tag for API web services. */
   public static final String TAG_API = "API";

   @Override
   public void init(final ServletConfig servletConfig) throws ServletException {
      try {
         final Info info = new Info().title("NextGen Web Services")
                  .description("Web services for the NextGen application.");
         final OpenAPI oas = new OpenAPI().info(info)
                  .tags(List.of(new Tag().name(TAG_API).description("Services to communicate with NextGen")))
                  .servers(List.of(new Server().url("/nextgen")));
         oas.info(info);
         final SwaggerConfiguration oasConfig = new SwaggerConfiguration().openAPI(oas).prettyPrint(true)
                  .resourcePackages(Stream.of("com.pjm.nextgen.rest").collect(Collectors.toSet()));

         new JaxrsOpenApiContextBuilder<>().servletConfig(servletConfig).openApiConfiguration(oasConfig)
                  .buildContext(true);
      } catch (final OpenApiConfigurationException e) {
         throw new ServletException(e);
      }
   }
}

This advances things a bit farther but the JSON payload still contains no definitions for the services. So I'm guessing that JAX-RS is somehow not finding the services anymore?

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "NextGen Web Services",
    "description" : "Web services for the NextGen application."
  },
  "servers" : [ {
    "url" : "/nextgen"
  } ],
  "tags" : [ {
    "name" : "API",
    "description" : "Services to communicate with NextGen"
  } ]
}

Any ideas on what we might be missing here?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OpenApiBootstrap servlet and the JaxrsOpenApiContextBuilder configuration shown in the report, then compare behavior before and after Swagger 2.2.24. Investigate resource-package scanning for com.pjm.nextgen.rest and verify the servlet response includes the service definitions as well as the OpenAPI metadata.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.