API 以命名元组作为输入参数调用
API call with named tuple as input parameter
我的一个 API 端点需要一个包含命名元组列表的对象。输入对象的一个例子是:
public enum Countries { Afghanistan, Albania, Algeria, Andorra /*...*/ }
public class InputModel
{
public List<(Countries Country, bool Visited)> CountriesVisited { get; set; }
}
当我加载 Swagger 时,示例输入值 不 使用命名元组,而是 item1
和 item2
。例如:
"countriesVisited": [
{
"item1": "Afghanistan",
"item2": "false"
}
]
当我调用端点时,似乎我必须使用 item1
和 item2
才能识别已指定的值。使用命名元组(country
和 visited
)会导致忽略指定的值。
是否可以更改此设置,以便在我调用端点时可以使用命名的元组名称,而不是通用名称 item1
和 item2
?
我知道我可以通过创建一个 Country
和 Visited
作为属性的 class 来解决这个问题,但是我想避免创建自定义 class对于这样的事情,如果可能的话。
命名元组是一种语法糖,因此您所看到的是意料之中的。
更多信息:
我的一个 API 端点需要一个包含命名元组列表的对象。输入对象的一个例子是:
public enum Countries { Afghanistan, Albania, Algeria, Andorra /*...*/ }
public class InputModel
{
public List<(Countries Country, bool Visited)> CountriesVisited { get; set; }
}
当我加载 Swagger 时,示例输入值 不 使用命名元组,而是 item1
和 item2
。例如:
"countriesVisited": [
{
"item1": "Afghanistan",
"item2": "false"
}
]
当我调用端点时,似乎我必须使用 item1
和 item2
才能识别已指定的值。使用命名元组(country
和 visited
)会导致忽略指定的值。
是否可以更改此设置,以便在我调用端点时可以使用命名的元组名称,而不是通用名称 item1
和 item2
?
我知道我可以通过创建一个 Country
和 Visited
作为属性的 class 来解决这个问题,但是我想避免创建自定义 class对于这样的事情,如果可能的话。
命名元组是一种语法糖,因此您所看到的是意料之中的。
更多信息: