9

need some help with springdoc-openapi-ui!

I am using springdoc-openapi-ui to render my API schema. This is the version details of it.

enter image description here

Now, i have done some configuration in my spring boot application like below enter image description here

Now, i was expecting that when i hit localhost:15041/swagger-ui.html, it should take me to http://localhost:15041/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config by default this and render my API document but unfortunately it only renders the Petstore. Even if i directly go to http://localhost:15041/swagger-ui.html?configUrl=/v3/api-docs/swagger-config, it doesn't render using the swagger-config. enter image description here

Although if i go to localhost:15041/v3/api-docs i get my json API documentation and localhost:15041/v3/api-docs/swagger-config, i get the swagger-config. Also, if i enter /v3/api-docs in the Explore search area of the petstore page, it shows my API documentation.

I have spent more than 1.5 days to fix it but it just doesn't work. Really appreciate if someone can help.

Arpit Agrawal
  • 1,180
  • 1
  • 10
  • 21

4 Answers4

5

Found the issue. The problem is with how the springdoc library uses the swagger-ui. They have written some custom code to patch the index.html file returned by swagger-ui jar on the fly to inject the configUrl query param in the response.

Now when swagger-ui moved from 4.8.1 to 4.9.0, they changed the index.html file format which broke the custom code written in springdoc.

To fix the issue, currently we need to downgrade the org.webjars:swagger-ui to 4.8.1 where index.html file is as per expectation of springdoc which will then honour the configUrl sent in the query params.

Arpit Agrawal
  • 1,180
  • 1
  • 10
  • 21
  • 1
    thanks Arpit..that was really helpful....I think u meant downgrade org.webjars:swagger-ui to 4.8.1 :) – Amol Kshirsagar Apr 12 '22 at 06:28
  • It didnt help but I downgraded springdoc-openapi-webflux-ui to 1.5.13 from 1.6.6 and seems to have worked. I will get back with the exact reason as too which transitive dependency has changed asap but that solution seems to be working – Amol Kshirsagar Apr 12 '22 at 06:40
  • Yes, i meant `org.webjars:swagger-ui`. thanks for correcting. – Arpit Agrawal Apr 12 '22 at 14:29
  • 2
    Thanks, this answer help me a lot. The property disable-swagger-default-url seems doesn't work on 1.6.9. While I downgrade to 1.6.6, it works. – Ron Jun 09 '22 at 03:50
1

It works for me with this configuration (note also springdoc.swagger-ui.url config)

springdoc.swagger-ui.disable-swagger-default-url=true
springdoc.swagger-ui.configUrl=/openapi/swagger-config
springdoc.swagger-ui.url=/openapi
springdoc.api-docs.path=/openapi
Lacan
  • 11
  • 2
0

Theoretically you shouldn't have to define any options if you use the default stuff, my config tho look like this:

springdoc:
  swagger-ui:
    disable-swagger-default-url: true
    urls[0]:
      name: 'name'
      url: '/v3/api-docs'

For full reference and other useful settings look here: https://springdoc.org/#swagger-ui-properties

Also you mention that it not work if you call http://localhost:15041/swagger-ui.html?configUrl=/v3/api-docs/swagger-config this is the wrong url. The correct one is the url you mentioned first: http://localhost:15041/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config. But localhost:15041/swagger-ui.html should you route correctly.

If you use spring-security you should also add the following dependcy: springdoc-openapi-security

If it's not working it must have something to do with your deps I think.

Perry45
  • 66
  • 7
  • 1
    I didn't work :( Still shows the same Petstore page. – Arpit Agrawal Apr 03 '22 at 06:07
  • Strange, especially if you disable the default url. That means something general must be wrong if the settings have no effect. You said if you enter /v3/api-doc manually it renders right? Which url is then displayed? Are you sure the yaml is applied were you set the options. Are you 100% sure? – Perry45 Apr 03 '22 at 06:43
  • 1
    Yeah, i already tried disabling the default url but it doesn't work for some reason. I had the same doubt, so i set the `springdoc.api-docs.enabled: false` in yaml file to see if it is getting picked up correctly and yes it worked. It disable the api doc. If i use `localhost:/v3/api-doc` it renders the json document and not the swagger-ui. If i use `localhost:/v3/api-doc/swagger-config`, it also renders the swagger config json. – Arpit Agrawal Apr 03 '22 at 07:19
  • 1
    I event change the `springdoc.swagger-ui.path : /swagger-ui-custom.html` to see if it works and yes it changed the swagger path but still renders petstore. I even tried clearing browser cache, incognito mode, changed from chrome to firefox. Deployed my changes to server and checked on server but nothing seems to work. – Arpit Agrawal Apr 03 '22 at 07:22
  • And if you enter /v3/api-doc in the explorer search it renders correctly? Your doc is based on OpenApi 3.0 right? Did you used SpringFox or so before? Only I can think of that another lib interfieres somehow with the config. So maybe double check your deps. Or provide somekind of minimal example. I could easily show you that the config work in a minimal example project. – Perry45 Apr 03 '22 at 08:09
  • 1
    Yes, if i enter `/v3/api-doc` in explorer search, it works. Yes doc is based on OpenApi-3. Yes we used to use springFox earlier but not we migrated to SpringDoc-openapi. I will check if some dependency is causing this issue or not but. I did `mvn dependency:tree` and searched for any `SpringFox` leftover but i couldn't fine any SpringFox leftover dependency. Let me see if i can share a minimal example project. – Arpit Agrawal Apr 03 '22 at 08:23
  • BTW thanks for help :) Really appreciate quick replies. – Arpit Agrawal Apr 03 '22 at 08:23
  • Well, I migrated myself from SpringFox to Springdoc. I can just tell you my SetUp: I use Springdoc version 1.5.12. I pinned swagger-annotations from io.swagger to 1.6.2 and the swagger-annotations from io.swagger.core.v3 to 2.1.11. But I think I only did this because some other dep still imported SpringFox stuff. I also defined a @OpenApiDefinition but it's not needed I think. And I make sure only import annotions from io.swagger.v3. And don't use your old SwaggerConfig! – Perry45 Apr 03 '22 at 08:56
  • 1
    hi @ArpitAgrawal : Did you find a solution for this? I am facing the exact same issue. – Amol Kshirsagar Apr 10 '22 at 12:05
  • @AmolKshirsagar added the new answer which contains the root cause and the temporary fix for the issue. – Arpit Agrawal Apr 10 '22 at 14:09
0

I find a way to disable default URL on springdoc 1.6.9 with springboot 2.6.6.

I just got the same issue on the latest springdoc version 1.6.9, that it always points to that default petstore swagger URL even the springdoc properties (disable default URL) have been configured. Thanks to Arpit Agrawal, I find that springdoc change this default URL with the Transformer, in other words, it matches the request URL of the SWAGGER_INITIALIZER_JS = "swagger-initializer.js"js file which contains the default petstore URL, and then replace the default URL to empty string as html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);in the Transformer.

But on 1.6.9 this snippet code doesn't work since the swagger default URL has been moved into the webjars/swagger-ui/index.html in that swagger-initializer.js file has been merged into index.html, and this js file is removed, then that replacement will never happen.

I have to use 1.6.9 for that it added the feature I need, so I change the code to let the default URL is disabled by overriding the SwaggerIndexTransformer Bean. The config URL better to set due to the original code can't work.

    @Bean
    SwaggerIndexTransformer indexPageTransformer(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 {
        if (swaggerUiConfigParameters.getConfigUrl() == null){
                System.out.println("You should set config url in case geting into this block");
                // swaggerWelcomeCommon.buildFromCurrentContextPath(request);
                //buildFromCurrentContextPath is not allowed to be invoked.
        }
 
        final AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean isIndexFound = antPathMatcher.match("**/swagger-ui/index.html", resource.getURL().toString());

        if (isIndexFound) {
            String html = defaultTransformations(resource.getInputStream());
            return new TransformedResource(resource, html.getBytes());
        }
        else
            return resource;
    }
            
        };
    }

Put this code in a @Configuration class, it can override that Bean in springdoc jar. And add those properties in application.properties file:

spring.main.allow-bean-definition-overriding=true

springdoc.swagger-ui.disable-swagger-default-url=true

springdoc.swagger-ui.configUrl=/{contextpath}/v3/api-docs/swagger-config

springdoc.swagger-ui.url=/{contextpath}/v3/api-docs
Ron
  • 903
  • 2
  • 11
  • 20