gpt4 book ai didi

json - 分页 jsonschema/hyperschema 时保留查询参数

转载 作者:行者123 更新时间:2023-12-02 07:37:27 29 4
gpt4 key购买 nike

我有一个 REST API,它有很多潜在的查询参数。

可通过类似 http://example.com/api/object?someParam=10&someOtherParam=20 的 URL 访问 API

存在大量潜在参数的地方。

响应定义如下:

{
"title": "Object Collection",
"type": "object",
"properties": {
"collection": {
"title": "Collection",
"type": "array",
"items": {
"$ref": "/schema/object.json"
}
},
"currPage": {
"title": "Current Page",
"type": "int"
},
"nextPage": {
"title": "Next Page",
"type": "int"
},
"prevPage": {
"title": "Previous Page",
"type": "int"
},
"perPage": {
"title": "Per Page",
"type": "int"
},
"totalCount": {
"title": "Total Count",
"type": "integer"
}
},
"links": [
{
"title": "Get object collection",
"rel": "self",
"method": "GET",
"href": "/api/object?page={currPage}&perPage={perPage}"
},
{
"title": "Get next page",
"rel": "next",
"method": "GET",
"href": "/api/object?page={nextPage}&perPage={perPage}"
},
{
"title": "Get prev page",
"rel": "prev",
"method": "GET",
"href": "/api/object?page={prevPage}&perPage={perPage}"
}
]
}

当然,当前定义的问题在于,当尝试通过链接转到另一个页面时,它会抛出查询参数。

是否有一些好方法来解释任意数量的参数?

理论上,我可以将所有可能性添加到我的响应中,例如

"properties": {
...
"someParam" : {
"description": "Some Param"
},
"someOtherParam" : {
"description": "Another param"
}
}

并使我的链接看起来像:

{
"title": "Get prev page",
"rel": "prev",
"method": "GET",
"href": "/api/object?page={prevPage}&perPage={perPage}&someParam={someParam}&someOtherParam={someOtherParam}"
}

但这很快就会变得很麻烦,特别是考虑到大量的查询参数。

URL 将会爆炸,而且每次添加新的查询参数时都需要更新架构。

在我看来,这是一个非常常见的用例,但经过大量谷歌搜索后,我无法找到任何相关内容。

最佳答案

因此,我选择了一种似乎效果很好的方法。很可能有更好的解决方案,但这是我们能想出的唯一一个不会让人感觉很恶心的解决方案。

具体来说,请注意 queryString 的介绍及其在链接中的使用。

通过从传入的查询字符串中删除“page”和“perPage”字段来填充queryString,以免重复这些字段。

还值得注意的是 queryString 之前的 +,它可以防止对其进行 URL 编码。

{
"title": "Object Collection",
"type": "object",
"properties": {
"collection": {
"title": "Collection",
"type": "array",
"items": {
"$ref": "/schema/object.json"
}
},
"currPage": {
"title": "Current Page",
"type": "int"
},
"nextPage": {
"title": "Next Page",
"type": "int"
},
"prevPage": {
"title": "Previous Page",
"type": "int"
},
"perPage": {
"title": "Per Page",
"type": "int"
},
"totalCount": {
"title": "Total Count",
"type": "integer"
},
//queryString is all of the GET parameters, in their URL form
// e.g. "someParam=10&anotherParam=20"
"queryString": {
"title": "String representing the rest of the query params",
"type": "string"
}
},
"links": [
//Added queryString to the end of the hrefs. + sign prevents URL encoding
{
"title": "Get object collection",
"rel": "self",
"method": "GET",
"href": "/api/object?page={currPage}&perPage={perPage}&{+queryString}"
},
{
"title": "Get next page",
"rel": "next",
"method": "GET",
"href": "/api/object?page={nextPage}&perPage={perPage}&{+queryString}"
},
{
"title": "Get prev page",
"rel": "prev",
"method": "GET",
"href": "/api/object?page={prevPage}&perPage={perPage}&{+queryString}"
}
]
}

希望其他人发现这很有用。

关于json - 分页 jsonschema/hyperschema 时保留查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18476601/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com