240 lines
7.2 KiB
ReStructuredText
Raw Normal View History

Frequently Asked Questions (FAQ)
================================
2018-05-07 10:05:03 +02:00
Sharing parameter configuration
-------------------------------
Q: I use the same value in multiple endpoints. How can I avoid duplicating the descriptions?
A: You can configure ``schemas`` in the nelmio_api_doc configuration and then reference them:
2018-05-07 10:05:03 +02:00
.. code-block:: yaml
2022-05-12 23:48:13 +02:00
# config/nelmio_api_doc.yaml
2018-05-07 10:05:03 +02:00
nelmio_api_doc:
documentation:
components:
schemas:
NelmioImageList:
description: "Response for some queries"
type: object
properties:
total:
type: integer
example: 42
2018-05-07 10:05:03 +02:00
items:
type: array
items:
$ref: "#/components/schemas/ImageMetadata"
2018-05-07 10:05:03 +02:00
.. code-block:: php
// src/App/Controller/NelmioController.php
/**
* @OA\Response(
2018-05-07 10:05:03 +02:00
* response=200,
* description="List of image definitions",
* @OA\JsonContent(@OA\Schema(
2018-05-07 10:05:03 +02:00
* type="object",
* title="ListOperationsResponse",
* additionalProperties={"$ref": "#/components/schemas/NelmioImageList"}
* ))
2018-05-07 10:05:03 +02:00
*/
Optional Path Parameters
------------------------
Q: I have a controller with an optional path parameter. In swagger-ui, the parameter is required - can I make it
optional? The controller might look like this::
2018-05-07 10:05:03 +02:00
/**
* Get all user meta or metadata for a specific field.
*
* @Route("/{user}/meta/{metaName}",
* methods={"GET"},
* name="get_user_metadata"
* )
*
* @OA\Response(
2018-05-07 10:05:03 +02:00
* response=200,
* description="Json object with all user meta data or a json string with the value of the requested field"
* )
*/
public function getAction(string $user, string $metaName = null)
{
...
}
2018-05-08 08:08:20 +02:00
A: Optional path parameters are not supported by the OpenAPI specification. The solution to this is to define two
separate actions in your controller. For example::
2018-05-07 10:05:03 +02:00
/**
* Get all user meta data.
*
* @Route("/{user}/meta",
* methods={"GET"},
* name="get_user_metadata"
* )
*
* @OA\Response(
2018-05-07 10:05:03 +02:00
* response=200,
* description="Json hashmap with all user meta data",
* @OA\JsonContent(@OA\Schema(
2018-05-07 10:05:03 +02:00
* type="object",
* example={"foo": "bar", "hello": "world"}
* ))
2018-05-07 10:05:03 +02:00
* )
*/
public function cgetAction(string $user)
{
return $this->getAction($user, null);
}
/**
* Get user meta for a specific field.
*
* @Route("/{user}/meta/{metaName}",
* methods={"GET"},
* name="get_user_metadata_single"
* )
*
* @OA\Response(
2018-05-07 10:05:03 +02:00
* response=200,
* description="A json string with the value of the requested field",
* @OA\JsonContent(@OA\Schema(
2018-05-07 10:05:03 +02:00
* type="string"
* ))
2018-05-07 10:05:03 +02:00
* )
*/
public function getAction(string $user, string $metaName = null)
{
...
}
2018-05-08 08:08:20 +02:00
The first action is redundant for Symfony, but adds all the relevant documentation for the OpenAPI specification.
2018-05-07 10:05:03 +02:00
Asset files not loaded
----------------------
Q: How do I fix 404 or 406 HTTP status on NelmioApiDocBundle assets files (css, js, images)?
2018-05-08 08:08:20 +02:00
A: The assets normally are installed by composer if any command event (usually ``post-install-cmd`` or
``post-update-cmd``) triggers the ``ScriptHandler::installAssets`` script.
If you have not set up this script, you can manually execute this command:
2018-05-07 10:05:03 +02:00
.. code-block:: bash
2022-05-12 23:48:13 +02:00
$ php bin/console assets:install --symlink
Re-add Google Fonts
-------------------
2018-07-16 14:15:27 +02:00
Q: How can I change the font for the UI?
2018-07-16 14:15:27 +02:00
A: We removed the google fonts in 3.3 to avoid the external request for GDPR reasons. To change the font, you can :doc:`customize the template <customization>` to add this style information:
.. code-block:: twig
{# templates/bundles/NelmioApiDocBundle/SwaggerUi/index.html.twig #}
2018-07-16 14:15:27 +02:00
{#
To avoid a "reached nested level" error an exclamation mark `!` has to be added
See https://symfony.com/blog/new-in-symfony-3-4-improved-the-overriding-of-templates
#}
{% extends '@!NelmioApiDoc/SwaggerUi/index.html.twig' %}
{% block stylesheets %}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700">
{{ parent() }}
<style type="text/css" rel="stylesheet">
#formats {
font-family: Open Sans,sans-serif;
}
.swagger-ui .opblock-tag,
.swagger-ui .opblock .opblock-section-header label,
.swagger-ui .opblock .opblock-section-header h4,
.swagger-ui .opblock .opblock-summary-method,
.swagger-ui .tab li,
.swagger-ui .scheme-container .schemes>label,
.swagger-ui .loading-container .loading:after,
.swagger-ui .btn,
.swagger-ui .btn.cancel,
.swagger-ui select,
.swagger-ui label,
.swagger-ui .dialog-ux .modal-ux-content h4,
.swagger-ui .dialog-ux .modal-ux-header h3,
.swagger-ui section.models h4,
.swagger-ui section.models h5,
.swagger-ui .model-title,
.swagger-ui .parameter__name,
.swagger-ui .topbar a,
.swagger-ui .topbar .download-url-wrapper .download-url-button,
.swagger-ui .info .title small pre,
.swagger-ui .scopes h2,
.swagger-ui .errors-wrapper hgroup h4 {
font-family: Open Sans,sans-serif!important;
}
</style>
{% endblock stylesheets %}
Endpoints grouping
------------------
Q: Areas feature doesn't fit my needs. So how can I group similar endpoints of one or more controllers in a separate section in the documentation?
A: Use ``@OA\Tag`` annotation.
.. code-block:: php
/**
* Class BookmarkController
*
* @OA\Tag(name="Bookmarks")
*/
class BookmarkController extends AbstractFOSRestController implements ContextPresetInterface
{
2022-05-12 23:48:13 +02:00
// ...
}
Disable Default Section
-----------------------
Q: I don't want to render the "default" section, how do I do that?
A: Use ``disable_default_routes`` config in your area.
.. code-block:: yaml
nelmio_api_doc:
areas:
default:
disable_default_routes: true
Stop Model Property Description When a Schema Type or Ref is Already Defined (#1978) * Return a Result Object from AnnotationsReader::updateDefinition This is so we can make a decision on whether or not a schema's type or ref has been manually defined by a user via an `@OA\Schema` annotation as something other than an object. If it has been defined, this bundle should not read model properties any further as it causes errors. I put this in AnnotationReader as it seemed the most flexible in the long run. It could have gone in `OpenApiAnnotationsReader`, but then any additional things added to `updateDefinition` could be left out of the decision down the road. This is also a convenient place to decide this once for `ObjectModelDescriber` and `JMSModelDescriber`. * Stop Model Describer if a Schema Type or Ref Has Been Defined Via the result object added in the previous commit. This lets user "short circuit" the model describers by manually defining the schema type or ref on a plain PHP object or form. For example, a collection class could be defined like this: /** * @OA\Schema(type="array", @OA\Items(ref=@Model(type=SomeEntity::class))) */ class SomeCollection implements \IteratorAggregate { } Previously the model describer would error as it tries to merge the `array` schema with the already defiend `object` schema. Now it will prefer the array schema and skip reading all the properties of the object. * Add a Documentation Bit on Stopping Property Description * Mark UpdateClassDefinitionResult as Internal
2022-04-30 13:28:05 -05:00
Overriding a Form or Plain PHP Object Schema Type
-------------------------------------------------
Q: I'd like to define a PHP object or form with a type other any ``object``, how
do I do that?
A: By using the ``@OA\Schema`` annotation or attribute with a ``type`` or ``ref``.
Note, however, that a ``type="object"`` will still read all a models properties.
.. code-block:: php
<?php
use OpenApi\Annotations as OA;
use Nelmio\ApiDocBundle\Annotation\Model;
/**
* @OA\Schema(type="array", @OA\Items(ref=@Model(type=SomeEntity::class)))
*
* or define a `ref`:
* @OA\Schema(ref="#/components/schemas/SomeRef")
*/
class SomeCollection implements \IteratorAggregate
{
// ...
}