6

Setup:

I am using the Java library springdoc-openapi-ui in version 1.4.0 (via Maven) without any customization in a simple spring-boot project.

The Swagger page is generated under https://my-url.com/my-context-path/swagger-ui/index.html

and the api-docs under https://my-url.com/my-context-path/v3/api-docs/

both of these work and I can reach them. So far so good!

Now the problem:

When simply navigating to https://my-url.com/my-context-path/swagger-ui.html I am getting a HTTP Status 302 and a location attribute set in the response header that is supposed to redirect me to the swagger page from above (I assume).

However, the URL in the location attribute misses the context path! It looks like this: https://my-url.com/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

It redirects to a page that does not exist and I am getting a 404 error code. Note, that the configUrl also seems to be missing the context-path.

Any ideas why this occurs and how it can be fixed?

This Github Issue seemed to be the same problem, but in the end it is stated that the problem is fixed: https://github.com/springdoc/springdoc-openapi/issues/37 and that is for a previous version than mine.

Community
  • 1
  • 1
schrobe
  • 767
  • 2
  • 8
  • 29

3 Answers3

6

Okay so the issue is that springdoc-openapi-ui is unaware of your app context path unless it is defined in spring boot, which may not be possible for everybody.

Hopefull it does support the non-standard header X-Forwarded-Prefix that can be sent by your gateway.

I my case (Kubernetes), the Ingress can be configured in your chart by simply adding nginx.ingress.kubernetes.io/x-forwarded-prefix: "true"

And in your application config you also need to specify

server:
  forward-headers-strategy: framework

to use Spring's support for handling forwarded headers.

Sources:

https://github.com/kubernetes/ingress-nginx/issues/3670

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#x-forwarded-prefix-header

https://github.com/springdoc/springdoc-openapi/issues/607

petronius
  • 451
  • 3
  • 11
  • In the link https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#x-forwarded-prefix-header, the acceptable value for the property nginx.ingress.kubernetes.io/x-forwarded-prefix should be a string and not a boolean. Are you sure that it will work with 'true'? – Jijo Mathew Mar 02 '21 at 09:57
1

In order to configure a swagger-ui correctly when an external context-path is configured use the follow configuration.

springdoc.swagger-ui.config-url=/context-path/api-docs/swagger-config
springdoc.swagger-ui.url=/context-path/api-docs
springdoc.api-docs.path=/api-docs
0

There are no know issues about context-path usage. As you can #37 is resolved and that reported it has confirmed that!

Just make sure you follow the instructions of setting context-path on standard spring-boot application.

You can test the configuration of your context path, in the different demos samples:

If you have any problem, you can log an issue by provinding a minimal/reproducible sample or with unit tests that reproduces the problem.

brianbro
  • 4,141
  • 2
  • 24
  • 37
  • 3
    It seems that my problem is due to the fact the context-path is actually not part of my application (`server.servlet.context-path` is not set) but rather due a "gateway" where the application is deployed. When I run my service locally, there is no context-path and everything works find, but once deployed, the service is reachable under /my-context-path. Setting `server.servlet.context-path` is therefore not an option for me as the application does not actually has a context-path. Would you know a workaround for that? – schrobe Jun 04 '20 at 07:32
  • 1
    @scrobe you are right, I face the same problem in Kubernetes with the context path set by the ingress. – petronius Sep 17 '20 at 08:09