我正在使用asp.net core 3和swashbuckle,它们大多数都是默认配置,我有一个dto参数,上面带有一个字符串,我希望它不能为空。我该如何实现?注意,必需性和可空性是大张旗鼓的独立关注点。
它也在使用C#8和不可为空的东西,因此编译器应该已经将该属性注释为不可为空。可以理解的是,尚未对Swashbuckle进行更新以将其考虑在内(也许不能),但是我希望能够以某种方式覆盖生成的元数据。
class MyDto {
// I want this to show as non-nullable in the swagger documentation (and ideally also be non-nullable in the binding)
public string TestProp { get; set; }
}
[HttpPost]
public void Post([FromBody] MyDto requestModel) {
}
我已经尝试将其设置为必需,并将以下内容添加到启动类中。我也尝试添加newtonsoft批注,但似乎都没有这样做。
options.JsonSerializerOptions.IgnoreNullValues = true;
阅读RequiredAttribute
的文档:
指定需要数据字段值。