Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 267
schrobe Avatar asked Jun 02 '20 13:06

schrobe


1 Answers

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

like image 55
petronius Avatar answered Sep 24 '22 21:09

petronius