【发布时间】:2019-03-26 00:42:06
【问题描述】:
假设我有一个名为Estimate 的模型。我有一个 Vapor 3 API,我想返回这些模型的列表,按查询参数过滤。这样做当前会返回一个Future<[Estimate]>,这会导致 API 返回如下所示的 JSON:
[{estimate object}, {estimate object}, ...]
相反,我想让它返回以下内容:
{"estimates": [{estimate object}, {estimate object}, ...]}
所以,和以前一样,但是包装在一个带有单个键 "estimates" 的 JSON 对象中。
According to the documentation,任何时候我想返回一些非默认的东西,我都应该为它创建一个新的类型;这对我来说建议我应该创建一个像这样的类型:
final class EstimatesResponse: Codable {
var estimates: [Estimate]?
}
但是,过滤后我得到一个 Future<[Estimate]> 而不是纯 [Estimate] 数组,这意味着我无法将它分配给我的 EstimatesResponse estimates 属性。将estimates 的类型设为Future<[Estimate]> 似乎很奇怪,我不确定结果如何。
那么,我怎样才能返回正确格式的 JSON?
【问题讨论】: