Skip to content

Bootstrapped servlet returns empty JSON as of 2.2.24 #4854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kpilandjr opened this issue Mar 10, 2025 · 0 comments
Open

Bootstrapped servlet returns empty JSON as of 2.2.24 #4854

kpilandjr opened this issue Mar 10, 2025 · 0 comments

Comments

@kpilandjr
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant