Skip to content

v3/api-docs/swagger-config endpoint without 'url' value #1698

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

Closed
zacky-wang opened this issue Jun 7, 2022 · 9 comments
Closed

v3/api-docs/swagger-config endpoint without 'url' value #1698

zacky-wang opened this issue Jun 7, 2022 · 9 comments
Labels
question Further information is requested

Comments

@zacky-wang
Copy link

hi,developer

Describe the bug

i hava the problem with use springdoc and springboot.
springdoc work correctly in local env , but is not work on server.

local env:
http://localhost:8080/user/swagger-ui/index.html

server env
https://my-domain.com/**sup-user**/user/swagger-ui/index.html

the sup-user is a path mapping

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using?
  • What modules and versions of springdoc-openapi are you using?

springboot version:2.6
springdoc version:1.6.8

  • What is the actual and the expected result using OpenAPI Description (yml or json)?

json

  • Provide with a sample code (HelloController) or Test that reproduces the problem

config is following:

springdoc:
  swagger-ui:
    path: index.html
    enabled: true
    config-url: /sup-user/user/v3/api-docs/swagger-config
    disable-swagger-default-url: true          
    url: /sup-user/user/v3/api-docs

Expected behavior

  • A clear and concise description of what you expected to happen.
  • What is the expected result using OpenAPI Description (yml or json)?

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

local env:
image

image

server env:
image

image

while i insert '/sup-user/user/v3/api-docs' on top of swagger-ui page ,then click 'Explore', i can get all apis info.

image

Additional context
Add any other context about the problem here.

@bnasslahsen
Copy link
Collaborator

@Jaswine,

When you use, config url: springdoc.swagger-ui.configUrl:

  • It's a user provided : to fetch external configuration document from.

Either, set springdoc.swagger-ui.configUrl (where you can put the url value) or springdoc.swagger-ui.url. But not both, in the same application. It has no sense.

@bnasslahsen bnasslahsen added the question Further information is requested label Jun 7, 2022
@rhu25
Copy link

rhu25 commented Jul 15, 2022

@Jaswine
how is this been solved?
i am having same issue,

on our deployed app, we using proxy to call api,
so swagger-config always return 404

@SMore-Napi
Copy link

SMore-Napi commented Jul 15, 2022

on our deployed app, we using proxy to call api, so swagger-config always return 404

If you're using a reverse proxy sever, check it out:

@rhu25
Copy link

rhu25 commented Jul 18, 2022

@SMore-Napi i tried this, still no luck, it still called https://xyz.com/v3/api-docs/swagger-config in ui

@lnthai2002
Copy link

I have similar issue, my application is multitenant thus the url of swagger UI is https://{tennantName}.mycompany.com/{tenantId}/applicationName/swagger-ui/index.html. Because the tennant info is dynamic, we cannot statically define config-url in properties files. We though we can pass the configUrl as request params but it seem swagger-initializer.js always load it from a default place.

@ShubamVirdi
Copy link

is there any solution for this. i am also facing same issue as my application is deployed on kubernetes with istio and im not able to append UriPrefix to the request which is being made by swagger.

@lnthai2002
Copy link

I ended up customize the swagger-initializer.js to extract the context path from the url to form the correct api-docs (HTTPS://tenant.mycompany.com/tenantName/applicationName/v3/api-docs) then in the SwaggerUIBundle I set the url to that.

@jjatinggoyal
Copy link

@lnthai2002 can you please elaborate how you customized swagger-initializer.js, and SwaggerUIBundle?

@lnthai2002
Copy link

Here is my customized sawgger-initalizer.js:

window.onload = function() {

  const swaggerConfigPath = "/v3/api-docs/swagger-config"
  const swaggerUiPath = "/swagger-ui/index.html"
  var currentPath = window.location.pathname
  var contextPathEnd = currentPath.indexOf(swaggerUiPath);
  var contextPath = currentPath.substr(0, contextPathEnd);
  var configUrl = contextPath + swaggerConfigPath

  window.ui = SwaggerUIBundle({
    url: "",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl,
      //https://stackoverflow.com/questions/51115593/changing-swagger-ui-server-variable-at-runtime
      {
          statePlugins: {
              spec: {
                  wrapActions: {
                      updateJsonSpec: function(oriAction, system) {
                          return (spec) => {
                              // change spec.servers here to add new entry, use concat to put it as the first & default one
                              spec.servers = [{url: contextPath}]
                              return oriAction(spec)
                          }
                      }
                  }
              }
          }
      }
    ],
    layout: "StandaloneLayout" ,

  "configUrl" : configUrl,
  "docExpansion" : "none",
  "tagsSorter" : "alpha",
  "validatorUrl" : "",
  });

  //</editor-fold>
};

As you can see, I craft the configUrl using the current URL, the configUrl is formed by taking the context of the app which includes the tenant info and then affixed by a path to the location of the swagger spec file for the tenant. When the swagger UI starts, it will first fetch the spec from this configUrl and then render it.
Here is an example of the controller that serve the spec:

@GetMapping("/v3/api-docs/swagger-config")
    public Map<String, String> swaggerConfig(HttpServletRequest request) {
        Map<String, String> config = new HashMap<>();
        config.put("configUrl", request.getRequestURI());
        config.put("docExpansion","none");
        config.put("oauth2RedirectUrl",String.format("i dont use this", request.getServerName(), request.getServerPort()));
        config.put("tagsSorter","alpha");
        config.put("validatorUrl","");
//        config.put("supportedSubmitMethods","");

        String serviceName = getServiceName();
        String url;
        if (localhost) {
            url = String.format("/v3/api-docs");
        } else {
            url = String.format("/%s/%s/v3/api-docs", getTenantName(), serviceName);
        }
        config.put("url", url);
        return config;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants