act*_*apz 6 .net c# json json.net jsonschema
我有一个 MVC 应用程序,它将我的模型序列化为 json 模式(使用 Newtonsoft json.net 模式)。问题是我的数组中的项目有 type ["string", "null"],但我需要的只是"string". 这是我班级的代码:
public class Form
{
[Required()]
public string[] someStrings { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是由 Json.net 架构制作的架构:
"someStrings": {
"type": "array",
"items": {
"type": [
"string",
"null"
]
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我期待这个:
"someStrings": {
"type": "array",
"items": {
"type": "string"
}
}
Run Code Online (Sandbox Code Playgroud)
请帮我摆脱那个“空”。
生成架构时尝试设置DefaultRequired为DisallowNull:
JSchemaGenerator generator = new JSchemaGenerator()
{
DefaultRequired = Required.DisallowNull
};
JSchema schema = generator.Generate(typeof(Form));
schema.ToString();
Run Code Online (Sandbox Code Playgroud)
输出:
{
"type": "object",
"properties": {
"someStrings": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)