Vladimir Shadrin
Vladimir Shadrin

Reputation: 376

How to add custom Springdoc resources to swagger-ui page

I'm not good at swagger and related libraries, so sorry if the title is confusing.

I have several services which provide their api as swagger documentation in json format and via swagger-ui. Next, I have a springboot service which is a proxy of the previously mentioned services and it provides combined api of all services (one can select exact service in a dropdown). It was implemented like this:

public class PropertyResourceProvider implements SwaggerResourcesProvider {
  @Autowired
  private SwaggerConfigProperties swaggerConfigProperties; // Just a mapping of config

  @Override
  public List<SwaggerResource> get() {
    // Doc for local (proxy) service
    val local = new SwaggerResource();
    local.setName(title);
    local.setUrl(swaggerLocal);
    local.setSwaggerVersion(version);

    // Add other services - specified in config
    List<SwaggerResource> services = swaggerConfigProperties.getServices().stream().map(service -> {
      String contextPath = service.get("contextPath");
      String externalPath = SWAGGER_DOC_BASE_PATH + "/" + contextPath;

      val resource = new SwaggerResource();
      resource.setName(service.get("name"));
      resource.setUrl(externalPath);
      resource.setSwaggerVersion(service.get("version"));

      return resource;
    }).collect(Collectors.toList());

    services.add(local);

    return services; // Return combined API
  }

Now, we're moving from springfox to springdoc and I can't use SwaggerResource and SwaggerResourcesProvider. How can I implements the same using springdoc?

Upvotes: 4

Views: 4268

Answers (1)

Andy Krylov
Andy Krylov

Reputation: 3

In springdoc it is replaced by AbstractSwaggerUiConfigProperties.SwaggerUrl class, although it is a bit different (e.g. there's no swaggerVersion property anymore).

public static class SwaggerUrl {

  @JsonProperty("url")
  private String url;

  @JsonIgnore
  private String name;


  @JsonProperty("name")
  private String displayName;

This is suitable for external APIs and it is functioning the same as SwaggerResource. Also in the application.properties by providing this property: springdoc.swagger-ui.urls-primary-name you can specify which spec will be first in the dropdown list.

One thing, however: bear in mind, that in springfox you could define security bean like this:

@Bean
public SecurityConfiguration security() { 
  return new SecurityConfiguration(CLIENT_ID, CLIENT_SECRET, REALM,
                                   APP_NAME, API_KEY_VALUE,
                                   ApiKeyVehicle.HEADER, API_TOKEN_HEADER,
                                   SCOPE_SEPARATOR);
}

and it was adding security context (an API header in this case) to all your outgoing requests from Swagger UI. However, springdoc does not support this feature, so you need to define security schemas in each and every API, so they appear in your proxy service Swagger. I faced this issue and found this a bit annoying the global requests security feature is not supported.

Upvotes: 0

Related Questions