springdoc / springdoc-openapi-demos

Demo for OpenAPI 3 with spring-boot
http://springdoc.org
Apache License 2.0
493 stars 267 forks source link

SwaggerUrl abnormal parameter setting #45

Closed 1455668754 closed 1 year ago

1455668754 commented 2 years ago

hello,I have a question This is what I built SwaggerUrl object,I mean i will got this url image

but like this, I ended up with this, After carefully comparing the documentation with your source code, I can't find out why this is the case, Could you give me an answer, please ? thanks image

1455668754 commented 2 years ago

i use the gateway ,but why the front will automatically add "/v3/api-docs" emmm, I'm a little skeptical about life

qinxingnet commented 3 months ago

I have also encountered similar error messages. In versions after Spring Gateway+Spring boot 3.0, the same path is also the opposite, or the hostUrl I provided is not used at all

 @Bean
    @Lazy(false)
    public Set<AbstractSwaggerUiConfigProperties.SwaggerUrl> apis(RouteDefinitionLocator locator) {
        Set<AbstractSwaggerUiConfigProperties.SwaggerUrl> urls = new HashSet<>();
        List<RouteDefinition> routeDefList = locator.getRouteDefinitions().collectList().block();

        final List<RouteDefinition> definitionList = new ArrayList<>(gatewayProperties.getRoutes());
        definitionList.removeIf(svc -> svc.getId().contains("DiscoveryClient") || svc.getId().contains("ReactiveComposite")
                || svc.getId().contains(selfName)
        );

        for (RouteDefinition routeDefinition : definitionList) {
            String svcName = routeDefinition.getId();

            final Optional<PredicateDefinition> pathFound = routeDefinition.getPredicates().stream().filter(s -> s.getName().equalsIgnoreCase("Path")).findFirst();
            if (pathFound.isPresent()) {
                final PredicateDefinition predicateDefinition = pathFound.get();
                final String pathUrl1 = predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0");
                final String pathUrl2 = pathUrl1.replace("**", DEFAULT_API_DOCS_URL);
                var hostUrl = "http://localhost:8989/" + pathUrl2 +"/";

                AbstractSwaggerUiConfigProperties.SwaggerUrl swaggerUrl = new AbstractSwaggerUiConfigProperties.SwaggerUrl(svcName, hostUrl, null);
                urls.add(swaggerUrl);
            }
        }

        swaggerUiConfigParameters.setUrls(urls);
        swaggerUiConfigParameters.setDisplayRequestDuration(Boolean.TRUE);
        swaggerUiConfigParameters.setQueryConfigEnabled(Boolean.TRUE);
        return urls;
    }

is print result, Not specified by me http://localhost:8989/A -Service/v3/api-docs/: /v3/api-docs/A-service /v3/api-docs/B-service.....

qinxingnet commented 3 months ago

But when I use the URL configuration under Swagger ui, I can correctly read it. I want to read the addresses of various microservices for Swagger ui to use when the gateway is running.

springdoc:
  version: '@springdoc.version@'
  enabled: true
  swagger-ui:
    enabled: true
    use-root-path: true
    urls:
      - name: a-service
        url: https://apitmp.mc1024.com.cn/aService/v3/api-docs/
      - name: wework1
        url: http://localhost:8989/wework1/v3/api-docs/

According to the configuration manual, it is possible, but dynamically writing the URL address does not work. I don't know which boss can help me solve the problem.

qinxingnet commented 3 months ago

I have found a solution by using Java's classLoader principle to override the addUrl (String URL) method in the Swagger UiConfigParameters code,

   public void addUrl(String url) {
        this.urls.forEach(elt ->
                {
                    //2024-04-03 StarQin: HOT BUG FIX: IF EMPTY URL,INIT A DEFAULT URL
                    if (!isSwaggerUrlDefined(elt.getName()) && StringUtils.isEmpty(elt.getUrl())) {
                        elt.setUrl(url + DEFAULT_PATH_SEPARATOR + elt.getName());
                    }
                }
        );
    }

The given URL is as follows http://localhost:8989/wework1/v3/api -Docs will not be rewritten. Problem solving. Please fix this method under the springdoc openapi-starter common: 2.5.0 package.