api-platform
api-platform copied to clipboard
Swagger UI docs 404
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
- Setup an ingress blocking everything that does not start with /api/users
- configure the api_platform prefix to /api/users
- 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
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 ?
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.
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.
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.
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 :
It does work now, I can't afford to spend more than half a day on it.
Go to your docker and run -> bin/console asset:install :)
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...
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 ?
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
- Setup an ingress blocking everything that does not start with /api/users
- configure the api_platform prefix to /api/users
- 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
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
Go to your docker and run -> bin/console asset:install :)
Awesome you got me done. thank you
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 !
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.
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.
I'm also having the same problem. I have tried the followings:
-
Ran
bin/console asset:install --symlink
from docker. -
I use traefik and set the forwardedHeaders like below (somebody suggested this):
entryPoints:
web:
address: ":80"
forwardedHeaders:
insecure: true
- 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.
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)