swagger-api / swagger-editor

Swagger Editor
https://editor.swagger.io
Apache License 2.0
8.91k stars 2.25k forks source link

[docker] BASE_URL is not taken into account for the yml file, fails to load. #2672

Open luckydonald opened 3 years ago

luckydonald commented 3 years ago

Q&A (please complete the following information)

Should be irrevelant - OS: macos - Browser: chrome - Version: latest - Method of installation: docker - Swagger-Editor version: latest - Swagger/OpenAPI version: OpenAPI ~~3.1~~ 3.0.3

Content & configuration

docker-compose.yaml

swagger:
    image: swaggerapi/swagger-editor
    environment:
      BASE_URL: /api/docs/
    volumes:
      - ./docs/API/api.yaml:/etc/nginx/html/api.yaml.  # straight into the nginx folder, so that it's not ending up readonly by the `cp` operation in the startup script.
    environment:
      SWAGGER_FILE: /etc/nginx/html/api.yaml  

nginx reserve proxy:

# ... server block ...
    # swagger
    location /api/docs/ {
        resolver 127.0.0.11 valid=30s;
        set $swagger http://swagger:8080;
        # rewrite /api/docs/(.*)$ /$1 break;  # actually we solve that with BASE_URL in the `swaggerapi/swagger-editor` image.
        proxy_pass $swagger$uri?$query_string;
    }

Describe the bug you're encountering

To reproduce...

Steps to reproduce the behavior:

  1. add mount point, e.g. api/docs in nginx for that image
  2. set BASE_URL accordingly, to api/docs/
  3. open page at domain/$BASE_URL, here https://example.com/api/docs/.
  4. See that it tries to load from
    • https://example.com/api.yml (incorrect)
    • https://example.com/api/docs/api.yml would be correct

Expected behavior

The frontend can be put to a different route with BASE_URL, however that is ignored for the yaml file.

Screenshots

Additional context or thoughts

This should be easily fixed by changing The route is however not considered for the yaml file.

That is, https://github.com/swagger-api/swagger-editor/blob/be2b04b8d1719fdb5aa47dbc03e2fe91a11525b4/docker-run.sh#L17 not always point to the root but to the $BASE_URL.

- REL_PATH="/$(basename $SWAGGER_FILE)"
+ REL_PATH="${BASE_URL}/$(basename $SWAGGER_FILE)"
luckydonald commented 3 years ago

For a workaround (and proof of the bug fix):

In the folder where your docker-compose.yml is, create a Dockerfile like so:

FROM swaggerapi/swagger-editor:v3.16.3

# Fix this line:
# https://github.com/swagger-api/swagger-editor/blob/be2b04b8d1719fdb5aa47dbc03e2fe91a11525b4/docker-run.sh#L17
# BEFORE: REL_PATH="/$(basename $SWAGGER_FILE)"
# AFTER : REL_PATH="${BASE_URL}/$(basename $SWAGGER_FILE)"

RUN RUN_CMD_FILE=/usr/share/nginx/docker-run.sh \
 && sed -i 's|REL_PATH="/$(basename $SWAGGER_FILE)"|REL_PATH="${BASE_URL}/$(basename ${SWAGGER_FILE})"|g' ${RUN_CMD_FILE} \
 && cat $RUN_CMD_FILE

For that, in the yaml file change

-  image: swaggerapi/swagger-editor

to

build:
      context: .
      dockerfile: Dockerfile
luckydonald commented 3 years ago

Should I prepare a PR?

tim-lai commented 3 years ago

@luckydonald Yes, please create a PR for your proposed solution. Thanks!

a39172 commented 1 year ago

@tim-lai I encountered the same problem (still unsolved).