Skip to content
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

configUrl cache issue when using Swagger-UI #1712

Closed
abenjell opened this issue Jun 17, 2022 · 12 comments
Closed

configUrl cache issue when using Swagger-UI #1712

abenjell opened this issue Jun 17, 2022 · 12 comments
Labels
bug Something isn't working

Comments

@abenjell
Copy link

Describe the bug
Hello,
Our users access swagger using direct access to api : http://localhost:8080/api/v0/swagger-ui/index.html
And other users access the swagger UI behind nginx proxy : http://localhost/api/v0/myservice/swagger-ui/index.html
The problem is the config url is calculated just the first time and used for both access.
The issue is well known and described here

To Reproduce
Steps to reproduce the behavior:
Call swagger ui using nginx proxy : http://localhost/api/v0/myservice/swagger-ui/index.html
Call swagger UI without the proxy : http://localhost:8080/api/v0/swagger-ui/index.html
In the second call we have error message : Failed to load remote configuration.
It's because it makes a call to http://localhost:8080/api/v0/myservice/v3/api-docs/swagger-config that is built the first time when calling the swagger via nginx.

Screenshots
If applicable, add screenshots to help explain your problem.
image
image

Additional context
Add any other context about the problem here.

Thank you for your help
Best Regards,

@rhu25
Copy link

rhu25 commented Jul 15, 2022

how this is been solved, i am facing same issue

my api
can visit swagger via https://localhost:8081/swagger-ui/index.html

but on my production which is a ui visit swagger via a proxy https://localhost:8082/services/swagger-ui/index.html

how do i handle make both works

@antechrestos
Copy link

@abenjell I referenced your issue from mine

@antechrestos
Copy link

antechrestos commented Jul 21, 2022

@abenjell

I made a hack on my side to fix the issue (does not work when two requests from different context path arrives at the same time

public class AlwaysComputeConfigUrlSwaggerTransformer extends SwaggerIndexPageTransformer {

    public AlwaysComputeConfigUrlSwaggerTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerWelcomeCommon swaggerWelcomeCommon, ObjectMapperProvider objectMapperProvider) {
        super(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, swaggerWelcomeCommon, objectMapperProvider);
    }

    @Override
    public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException {
        this.swaggerUiConfigParameters.setConfigUrl(null); // force parent class to compute data
        return super.transform(request, resource, transformerChain);
    }
}

All you have to do is instanciate it from your @Configuration as the spring doc one is ConditionnalOnMissingBean

This is quite dirty yet, we see that the config url which is an attribute of a Singleton bean is computed for the first request. Hence we are populating application scope data with request scope data.... 😢

Of course I use server.forward-headers-strategy: FRAMEWORK

@bnasslahsen
Copy link
Contributor

should be fixed by #1748

@bnasslahsen bnasslahsen added the bug Something isn't working label Jul 30, 2022
@abenjell
Copy link
Author

Hello @bnasslahsen,
This issue was not fixed by #1748
I reproduce the same issue even if i update springdoc-openapi to 1.6.11

@abenjell
Copy link
Author

Thank you @antechrestos for your feedback.
but I'm still facing the same issue with springdoc-openapi version = 1.6.11

@licenziato
Copy link

licenziato commented Aug 24, 2023

The problem is still present even in the springdoc-openapi version = 2.2.0. The only working solution for me is the one proposed by @antechrestos with a SwaggerIndexPageTransformer bean. Any updates on this. It should be reopen disabling the cache cannot be the right solution. For now I fixed also the problem of concurrency using a request scoped bean:

@Bean
@RequestScope
public SwaggerIndexPageTransformer computeConfigUrlSwagger(SwaggerUiConfigProperties swaggerUiConfig,
        SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters,
        SwaggerWelcomeCommon swaggerWelcomeCommon, ObjectMapperProvider objectMapperProvider) {
    return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters,
            swaggerWelcomeCommon, objectMapperProvider) {
 
        @Override
        public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain)
                throws IOException {
            this.swaggerUiConfigParameters.setConfigUrl(null);
            return super.transform(request, resource, transformerChain);
        }
    };
}

@kerner1000
Copy link

The workaround by @antechrestos does not work for me, problem is still present. springdoc-openapi-starter-webmvc-ui version 2.2.0.

@robindijkhof
Copy link

@bnasslahsen can this be reopened?

@bnasslahsen
Copy link
Contributor

@robindijkhof,

This can be reopened if an issue is confirmed.
You can share a minimal reproducible example , or a failing test in the springdoc project that highlights the issue and will be happy to reopen it.

@yqx1110
Copy link

yqx1110 commented Mar 15, 2024

I can confirm this issue still exists with springdoc 2.3.0, when accessing /xxx/swagger-ui/index.html, it uses the cached configUrl, however when accessing /xxx/swagger-ui.html the cached url will be updated and it works, but it redirects to /xxx/swagger-ui/index.html so people will bookmark this address and it will use cached url.

@Flyer-90
Copy link

Flyer-90 commented Mar 19, 2024

Issue still exists on springdoc 2.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants