Springdoc OpenAPI ui does not honor context-path in "location"

13k Views Asked by At

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.

3

There are 3 best solutions below

1
On

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

2
On

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.

1
On

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