【问题标题】:JsonSchemaGenerator Not Setting Fields to required = falseJsonSchemaGenerator 未将字段设置为必需 = false
【发布时间】:2014-10-15 08:23:21
【问题描述】:

我对一系列模型使用来自JSON.NETJsonSchemaGenerator,以将相应的 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);
}

除了为required 属性设置的值之外,这可以正常工作。无论我如何修饰模型属性,字段总是显示为"required":true,如下所示:

"FirstName": {
   "required": true,
   "type": "string"
}

但是,在代码中,我的模型是这样装饰的:

[JsonProperty(Required = Required.Default)]
public string FirstName { get; set; }

查看 Json.Net documentation,设置为 Required.Default 应该会导致架构中不需要属性

“默认 - 0 - 该属性不需要。默认状态。”

知道我做错了什么并且需要更改以便FirstName 属性在架构中输出为"required": false?我确实想要生成并手动按摩所有这些模式。

【问题讨论】:

    标签: c# json json.net jsonschema


    【解决方案1】:

    Required 枚举控制属性可以具有哪些值:是否允许空值。要控制 json 架构中的 "required" 属性,即 json 字符串是否必须包含实际属性,您需要在生成架构时使用 DefaultValueHandlingNullValueHandling 枚举。假设我们有以下类:

    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"
                ]
            }
        }
    }
    

    此架构表示 json 字符串必须具有属性 FirstName,并且允许此属性具有空值。

    通过将Required 属性从Default 更改为Always,我们将获得以下架构:

    {
        "id": "MyApp.Models.Person",
        "type": "object",
        "properties": {
            "FirstName": {
                "required": true,
                "type": "string"
            }
        }
    }
    

    此架构表示 json 字符串必须具有属性 FirstName,并且该属性不允许具有空值。

    要获得您想要的,您需要包含DefaultValueHandlingNullValueHandling 枚举。像这样的:

    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 属性,但如果存在,它可能具有 null 值。如果使用DefaultValueHandling.IgnoreAndPopulate枚举值或者切换到NullValueHandling属性而不是DefaultValueHandling属性并将其值设置为NullValueHandling.Ignore,则可以达到相同的效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 2021-08-13
      • 2019-07-31
      • 1970-01-01
      相关资源
      最近更新 更多