api-platform icon indicating copy to clipboard operation
api-platform copied to clipboard

Swagger UI docs 404

Open henryfoster opened this issue 4 years ago • 15 comments

API Platform version(s) affected: 2.6

Description
Some Swagger UI docs resources like javascript and css are not loaded from the correct path which you configured in "config/routes/api_platform.yaml" resulting in a 404.

How to reproduce

  1. Setup an ingress blocking everything that does not start with /api/users
  2. configure the api_platform prefix to /api/users
  3. Some resources for displaying the Swagger UI don't pass the ingress as they are coming from host/bundles

Example: expected: https://localhost:8000/api/users/public/bundles/apiplatform/swagger-ui/swagger-ui.css actual: https://localhost:8000/bundles/apiplatform/swagger-ui/swagger-ui.css The ingress is blocking https://localhost:8000/bundles/....

There should be a way to change the path from where these resources are served.

Possible Solution
I don't know maybe serve the files for the UI from the same URL that is setup in config/routes/api_platform.yaml

This is how the swagger ui docs looks like Additional Context

most important settings of my app:

Dockerfile

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

WORKDIR /app
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony
#RUN symfony server:ca:install

ADD composer.json composer.lock ./
RUN composer install
ADD . .
RUN ./bin/console cache:clear

CMD ["symfony", "server:start", "--no-tls"]

k8s/ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: 'true'
    #nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
    - host: ticketing.dev
      http:
        paths:
          - path: /api/users
            backend:
              serviceName: user-srv
              servicePort: 8000

config/routes/api_platform.yaml

api_platform:
    resource: .
    type: api_platform
    prefix: /api/users

Screenshot from 2021-02-06 16-56-19

Screenshot from 2021-02-06 17-53-46

Screenshot from 2021-02-06 17-54-53

henryfoster avatar Feb 06 '21 16:02 henryfoster

I've got exactly the same problem, and it is really annoying because I have to give feedback about the documentation to the front-end everytime. Any help ?

hurcle avatar Feb 11 '21 17:02 hurcle

I had this issue and solved it by using the X-Forwarded-Prefix header. I don't know how to set that up with k8s, but traefik sets it automatically if a prefix is set up and stripped before it reaches the server.

So just set that header with the prefix you are using and add 'x-forwarded-prefix' to trusted_headers in config/packages/framework.yaml and you're good to go. Make sure your framework bundle is updated to version 5.2.5, otherwise you'll get an error that 'x-forwarded-prefix' is not supported for trusted_headers.

jpjonte avatar Mar 27 '21 16:03 jpjonte

Hi, I'm currently having the same issue and unfortunately the above solution didn't work for me. Anyone else have any ideas as to how to solve this problem? I've been trying to fix it for hours now.

umaraziz0 avatar Mar 31 '21 06:03 umaraziz0

Hi, we also do have this issue. Have you found a way to solve it after 2 months? The thing is, it is running via symfony's build in webserver but on our staging system, where we are using an apache, its not working.

kisse91 avatar May 11 '21 09:05 kisse91

Unfortunately, I couldn't find clean solution. Because it's just about give the opportunity to the front to get access to the current documentation, I just updated directly the bundle even if it's not clean. If I do update and so delete it, I will come back on it, it's not like it's a huge change. Juste change directly the template inside the vendor : vendor_change It does work now, I can't afford to spend more than half a day on it.

hurcle avatar May 11 '21 09:05 hurcle

Go to your docker and run -> bin/console asset:install :)

numericks-yann avatar Jun 18 '21 15:06 numericks-yann

Go to your docker and run -> bin/console asset:install :)

This does not change from where the files are served. They still come from /bundels...

henryfoster avatar Sep 03 '21 14:09 henryfoster

I've the same problem on a Kubernetes deployment. I solved it by adding the Pod IP of nginx-ingress-controller to the list of trusted_proxies.

Without it, symfony will ignore X-Forwarded-* header. Have you a traefik or another load-balancer/proxy in front of your docker-compose ?

ghost avatar Sep 27 '21 13:09 ghost

API Platform version(s) affected: 2.6

Description Some Swagger UI docs resources like javascript and css are not loaded from the correct path which you configured in "config/routes/api_platform.yaml" resulting in a 404.

How to reproduce

  1. Setup an ingress blocking everything that does not start with /api/users
  2. configure the api_platform prefix to /api/users
  3. Some resources for displaying the Swagger UI don't pass the ingress as they are coming from host/bundles

Example: expected: https://localhost:8000/api/users/public/bundles/apiplatform/swagger-ui/swagger-ui.css actual: https://localhost:8000/bundles/apiplatform/swagger-ui/swagger-ui.css The ingress is blocking https://localhost:8000/bundles/....

There should be a way to change the path from where these resources are served.

Possible Solution I don't know maybe serve the files for the UI from the same URL that is setup in config/routes/api_platform.yaml

This is how the swagger ui docs looks like Additional Context

most important settings of my app:

Dockerfile

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

WORKDIR /app
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony
#RUN symfony server:ca:install

ADD composer.json composer.lock ./
RUN composer install
ADD . .
RUN ./bin/console cache:clear

CMD ["symfony", "server:start", "--no-tls"]

k8s/ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: 'true'
    #nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
    - host: ticketing.dev
      http:
        paths:
          - path: /api/users
            backend:
              serviceName: user-srv
              servicePort: 8000

config/routes/api_platform.yaml

api_platform:
    resource: .
    type: api_platform
    prefix: /api/users

Screenshot from 2021-02-06 16-56-19

Screenshot from 2021-02-06 17-53-46

Screenshot from 2021-02-06 17-54-53

I had a similar issue, but able to resolve it by running composer require api-platform/api-pack and everything seems to be ok. If you don't have public in your root folder, create one and run composer require api-platform/api-pack. Thank me later

abdojulari avatar Nov 28 '21 13:11 abdojulari

Go to your docker and run -> bin/console asset:install :)

Awesome you got me done. thank you

sidymbengue25 avatar Jun 17 '22 19:06 sidymbengue25

I'm using Docker with PHP-8.1.9 and API Platform 3.0 and the same render on Front ! it is a new project API Platform. The symfony website work correctly but not API Platform !

ramineifar avatar Oct 04 '22 07:10 ramineifar

You can try in the framework.yaml to add the base url for asset. It worked for me

api/config/packages/framework.yaml

    assets:
        base_path: '/api'

This is depending how you configure nginx. In mine, I only search for /api location, so I found this way to relocate the asset path.

ghost avatar Feb 18 '23 12:02 ghost

Hello, I have had the same problem. I am working with PHP 8.2.7, Symfony 5.4.25 and API Platform 2.7.5 (I know I can use version 6 but personally I just haven't found it comfortable, yet). Additionally, I'm using an Nginx 1.19 server.

My solution was drastic but I was desperate. As I am working with Docker, I decided to try and downgrade my PHP version to 7.4.6, which I had previously worked with. Obviously it crashed spectacularly (which I expected, but idk). So then I switched my PHP version back to 8.2.7, I did a rebuild of my docker-compose, ran composer install and like magic, the SwaggerUI started to work. It's one of those things that I'll never find a logical explanation for why doing something so absurd worked, because logically I had previously deleted the vendor folder, etc, etc. I don't know if my experience will help someone, because the solution was strange.

eocana avatar Jul 17 '23 19:07 eocana

I'm also having the same problem. I have tried the followings:

  1. Ran bin/console asset:install --symlink from docker.

  2. I use traefik and set the forwardedHeaders like below (somebody suggested this):

entryPoints:
  web:
    address: ":80"
    forwardedHeaders:
      insecure: true
  1. Even the below is not the best practice but tried to see if it has any impact at all:
framework:
    trusted_headers: [ 'x-forwarded-for', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-port', 'x-forwarded-prefix' ]

All my assets are under public/bundles/nelmioapidoc.

shoosah avatar Nov 07 '23 15:11 shoosah

Hi, in my case i have fixed it bay adding some conf: https://api-platform.com/docs/core/openapi/#using-a-custom-asset-package-in-swagger-ui

My assetPackage was null, my problem was only on prod env (app_debug=0)

TheBarber82 avatar Mar 25 '24 11:03 TheBarber82