英文:
Api platfrom, show possible query parameters in documentation
问题
我有一个GET端点,该端点只能返回单个记录,因此它不是一个集合端点,但此外,我还需要能够使用查询参数应用不同的可选过滤器,例如:
- deleted - 显示/隐藏
- user - 通过用户ID进行额外搜索
因此,URI 将类似于 /product/{$is}?deleted=show&user=1
但如何描述它们以在API文档UI中可见呢?
英文:
I have a GET endpoint, that endpoint can return only a single record, so it's not a collection endpoint, but additionally, I need to be able to apply different optional filters with query parameters, for example:
- deleted - show/hide
- user - search additionally by user id
Som uri will be something like /product/{$is}?delted=show&user=1
but how can I describe them to be visible in API documentation UI?
答案1
得分: 2
使用API-Platform 3和PHP 8属性:
// src/Entity/MyEntity.php
<?php
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\OpenApi\Model\Operation as ModelOperation;
use ApiPlatform\OpenApi\Model\Parameter as ModelParameter;
#[ApiResource(
new Get(
openapi: new ModelOperation(
parameters: [
new ModelParameter(
name: "deleted",
in: "query",
schema: [
"type" => "string",
"enum" => ["show", "hide"],
],
),
new ModelParameter(
name: "user",
in: "query",
schema: ["type" => "integer"]
)
],
),
),
)]
class MyEntity {
// ...
}
"in": "query"
表示参数位于查询字符串中(即GET参数集)。
当使用API-Platform 2时,请使用 openapi_context
而不是 openapi
,并传递一个数组的数组而不是 ModelParameter
对象的数组。
有关 OpenApi 的更多信息在此。
英文:
With API-Platform 3, using PHP 8 attributes:
// src/Entity/MyEntity.php
<?php
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\OpenApi\Model\Operation as ModelOperation;
use ApiPlatform\OpenApi\Model\Parameter as ModelParameter;
#[ApiResource(
new Get(
openapi: new ModelOperation(
parameters: [
new ModelParameter(
name: "deleted",
in: "query",
schema: [
"type" => "string",
"enum" => ["show", "hide"],
],
),
new ModelParameter(
name: "user",
in: "query",
schema: ["type" => "integer"]
)
],
),
),
)]
class MyEntity {
// ...
}
The "in": "query"
indicates that the parameter is located inside the query string (i.e GET parameters set).
When using API-Platform 2, use openapi_context
instead of openapi
and passes an array of array instead of an array or ModelParameter
objects.
More information about OpenApi here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论