fix: prepend context-path to TsfApiMetadataGrapher OpenAPI paths

Apply the same context-path fix to TsfApiMetadataGrapher as was done
for PolarisContractReporter. When server.servlet.context-path or
spring.webflux.base-path is configured, the OpenAPI paths are now
prefixed before serialization to the $api_metas system property.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Haotian Zhang <928016560@qq.com>
pull/1803/head
Haotian Zhang 2 weeks ago
parent b10dcb667e
commit 2433d3aa41

@ -17,12 +17,15 @@
package com.tencent.cloud.polaris.contract.tsf;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.cloud.common.util.GzipUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.api.AbstractOpenApiResource;
@ -45,15 +48,18 @@ public class TsfApiMetadataGrapher implements SmartLifecycle {
private final ObjectMapperProvider springdocObjectMapperProvider;
private final ApplicationContext applicationContext;
private final String groupName;
private final String contextPath;
public TsfApiMetadataGrapher(org.springdoc.webmvc.api.MultipleOpenApiResource multipleOpenApiWebMvcResource,
org.springdoc.webflux.api.MultipleOpenApiResource multipleOpenApiWebFluxResource,
String groupName, ApplicationContext applicationContext, ObjectMapperProvider springdocObjectMapperProvider) {
String groupName, ApplicationContext applicationContext, ObjectMapperProvider springdocObjectMapperProvider,
String contextPath) {
this.applicationContext = applicationContext;
this.multipleOpenApiWebMvcResource = multipleOpenApiWebMvcResource;
this.multipleOpenApiWebFluxResource = multipleOpenApiWebFluxResource;
this.groupName = groupName;
this.springdocObjectMapperProvider = springdocObjectMapperProvider;
this.contextPath = contextPath;
}
@Override
@ -84,6 +90,14 @@ public class TsfApiMetadataGrapher implements SmartLifecycle {
if (openApiResource != null) {
openAPI = AbstractOpenApiResourceUtil.getOpenApi(openApiResource);
}
if (openAPI != null && contextPath != null && !contextPath.isEmpty()
&& openAPI.getPaths() != null) {
Paths newPaths = new Paths();
for (Map.Entry<String, PathItem> entry : openAPI.getPaths().entrySet()) {
newPaths.addPathItem(contextPath + entry.getKey(), entry.getValue());
}
openAPI.setPaths(newPaths);
}
String jsonValue;
if (springdocObjectMapperProvider != null && springdocObjectMapperProvider.jsonMapper() != null) {
jsonValue = springdocObjectMapperProvider.jsonMapper().writeValueAsString(openAPI);

@ -28,7 +28,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
@Configuration
@ConditionalOnTsfConsulEnabled
@ -39,8 +41,16 @@ public class TsfSwaggerAutoConfiguration {
@ConditionalOnBean(OpenAPI.class)
public TsfApiMetadataGrapher tsfApiMetadataGrapher(@Nullable MultipleOpenApiWebMvcResource multipleOpenApiWebMvcResource,
@Nullable MultipleOpenApiWebFluxResource multipleOpenApiWebFluxResource, ApplicationContext context,
PolarisContractProperties polarisContractProperties, ObjectMapperProvider springdocObjectMapperProvider) {
PolarisContractProperties polarisContractProperties, ObjectMapperProvider springdocObjectMapperProvider,
Environment environment) {
String contextPath = environment.getProperty("server.servlet.context-path", "");
if (!StringUtils.hasText(contextPath)) {
contextPath = environment.getProperty("spring.webflux.base-path", "");
}
if (contextPath.endsWith("/")) {
contextPath = contextPath.substring(0, contextPath.length() - 1);
}
return new TsfApiMetadataGrapher(multipleOpenApiWebMvcResource, multipleOpenApiWebFluxResource,
polarisContractProperties.getGroup(), context, springdocObjectMapperProvider);
polarisContractProperties.getGroup(), context, springdocObjectMapperProvider, contextPath);
}
}

Loading…
Cancel
Save