Api平台,在文档中显示可能的查询参数。

huangapple go评论60阅读模式
英文:

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
&lt;?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: &quot;deleted&quot;,
                    in: &quot;query&quot;,
                    schema: [
                        &quot;type&quot; =&gt; &quot;string&quot;,
                        &quot;enum&quot; =&gt; [&quot;show&quot;, &quot;hide&quot;],
                    ],
                ),
                new ModelParameter(
                    name: &quot;user&quot;,
                    in: &quot;query&quot;,
                    schema: [&quot;type&quot; =&gt; &quot;integer&quot;]
                )
            ],
        ),
    ),
)]
class MyEntity {
    // ...
}

The &quot;in&quot;: &quot;query&quot; 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.

huangapple
  • 本文由 发表于 2023年6月22日 18:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定