如何在asp.net core 3.0 swagger中将字符串标记为不可为空

问题描述 投票:0回答:1

我正在使用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;
c# asp.net-core swashbuckle c#-8.0
1个回答
0
投票

阅读RequiredAttribute的文档:

指定需要数据字段值。

© www.soinside.com 2019 - 2024. All rights reserved.