C# JsonSchemaGenerator未将字段设置为必需=false
我正在对一系列模型使用C# JsonSchemaGenerator未将字段设置为必需=false,c#,json,json.net,jsonschema,C#,Json,Json.net,Jsonschema,我正在对一系列模型使用JSON.NET中的JsonSchemaGenerator将各自的JSON模式输出到一个字典中,如下所示 JsonSchemaGenerator generator = new JsonSchemaGenerator() { UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName, }; List<Type> modelTypes = Assembly.GetExecutingA
JSON.NET
中的JsonSchemaGenerator
将各自的JSON模式输出到一个字典中,如下所示
JsonSchemaGenerator generator = new JsonSchemaGenerator()
{
UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName,
};
List<Type> modelTypes = Assembly.GetExecutingAssembly()
.GetTypes()
.ToList()
.Where(t => t.Namespace == "MyApp.Models")
.ToList();
foreach (Type type in modelTypes)
{
JsonSchema schema = generator.Generate(type, jsonSchemaResolver, false);
schemaDictionary.Add(type, schema);
}
但是,在代码中,我的模型是这样装饰的:
[JsonProperty(Required = Required.Default)]
public string FirstName { get; set; }
查看Json.Net,设置为Required.Default
会导致架构中不需要该属性:
“默认值-0-属性是而不是必需的。默认状态。”
是否知道我做得不正确,需要进行更改,以便在架构中将
FirstName
属性输出为“required”:false
?我不希望必须生成并手动处理所有这些模式。必需的枚举控制属性可以具有哪些值:是否允许空值。要控制json模式中的“required”
属性,即json字符串是否必须包含实际属性,您需要在生成模式时使用DefaultValueHandling
或NullValueHandling
枚举。假设我们有以下课程:
public class Person
{
[JsonProperty(Required = Required.Default)]
public string FirstName { get; set; }
}
使用JSON.NET为此类生成的架构如下所示:
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"required": true,
"type": [
"string",
"null"
]
}
}
}
public class Person
{
[JsonProperty(Required = Required.Default, DefaultValueHandling = DefaultValueHandling.Ignore)]
public string FirstName { get; set; }
}
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"type": [
"string",
"null"
]
}
}
}
此模式表示json字符串必须具有属性FirstName
,并且允许此属性具有空值
通过将Required
属性从Default
更改为Always
,我们将得到以下模式:
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"required": true,
"type": "string"
}
}
}
此架构表示json字符串必须具有属性FirstName
,并且不允许此属性具有空值
要获得所需的内容,需要包含DefaultValueHandling
或NullValueHandling
枚举。大概是这样的:
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"required": true,
"type": [
"string",
"null"
]
}
}
}
public class Person
{
[JsonProperty(Required = Required.Default, DefaultValueHandling = DefaultValueHandling.Ignore)]
public string FirstName { get; set; }
}
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"type": [
"string",
"null"
]
}
}
}
从此类生成的架构如下所示:
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"required": true,
"type": [
"string",
"null"
]
}
}
}
public class Person
{
[JsonProperty(Required = Required.Default, DefaultValueHandling = DefaultValueHandling.Ignore)]
public string FirstName { get; set; }
}
{
"id": "MyApp.Models.Person",
"type": "object",
"properties": {
"FirstName": {
"type": [
"string",
"null"
]
}
}
}
此模式表示json字符串中不需要FirstName
属性,但如果存在该属性,则该属性可能具有空值。如果使用DefaultValueHandling.IgnoreAndPopulate
enum值,或者切换到NullValueHandling
属性而不是DefaultValueHandling
属性,并将其值设置为NullValueHandling.Ignore
,则可以实现相同的效果