Skip to content

Commit

Permalink
Merge pull request #66 from xm-online/feature/swgr-v3-meta
Browse files Browse the repository at this point in the history
Add swagger v3 definition to /swagger-resources
  • Loading branch information
amedvedchuk committed Jul 13, 2023
2 parents b2751b6 + 73775b0 commit 1b23591
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rootProject.name=gate
profile=dev
version=2.1.9
version=2.1.10

# Build properties
node_version=12.13.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

import io.github.jhipster.config.JHipsterConstants;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.annotation.Primary;
Expand All @@ -20,35 +25,50 @@
@Component
@Primary
@Profile(JHipsterConstants.SPRING_PROFILE_SWAGGER)
@RequiredArgsConstructor
@Slf4j
public class GatewaySwaggerResourcesProvider implements SwaggerResourcesProvider {

private final String defaultSwaggerVersion = "2.0";
private final String defaultApiDocsPath = "v2/api-docs";
private final String SWAGGER_V3 = "v3";

private final RouteLocator routeLocator;

public GatewaySwaggerResourcesProvider(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}
private final DiscoveryClient discoveryClient;


@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();

//Add the default swagger resource that correspond to the gateway's own swagger doc
resources.add(swaggerResource("default", "/v2/api-docs"));
resources.add(swaggerResource("default", defaultApiDocsPath, defaultSwaggerVersion));

//Add the registered microservices swagger docs as additional swagger resources
List<Route> routes = routeLocator.getRoutes();
routes.forEach(route -> {
resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs")));
List<ServiceInstance> instances = discoveryClient.getInstances(route.getId());
log.debug("route {} instances.size={}", route.getId(), instances.size());
String swaggerVersion = defaultSwaggerVersion;
String apiDocsPath = defaultApiDocsPath;
if (CollectionUtils.isNotEmpty(instances)) {
if (SWAGGER_V3.equalsIgnoreCase(instances.get(0).getMetadata().get("swagger"))) {
swaggerVersion = "3.0";
apiDocsPath = "v3/api-docs";
}
}
resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", apiDocsPath), swaggerVersion));
});

return resources;
}

private SwaggerResource swaggerResource(String name, String location) {
private SwaggerResource swaggerResource(String name, String location, String swaggerVersion) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("2.0");
swaggerResource.setSwaggerVersion(swaggerVersion);
return swaggerResource;
}
}

0 comments on commit 1b23591

Please sign in to comment.