【问题标题】:Add a root object to NJsonSchema schema generator将根对象添加到 NJsonSchema 模式生成器
【发布时间】:2021-03-29 23:14:03
【问题描述】:

我正在使用 NJsonSchema 从 c# 类生成 JasonSchema。我可以创建这个架构:

{
    "title": "SchemaModel",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "caseId": {
            "title": "Case Id",
            "type": [
                "null",
                "string"
            ],
            "description": "desc.",
        }
    }
}

通过使用:

var settings = new JsonSchemaGeneratorSettings
            {
                DefaultPropertyNameHandling = PropertyNameHandling.CamelCase
            };
            var generator = new JsonSchemaGenerator(settings);
            var schema = generator.Generate(typeof(SchemaModel));

但我需要将其包装在一个名为 schema 的对象中:

{
   "schema": {
       "title": "SchemaModel",
       "type": "object",
       "additionalProperties": false,
       "properties": {
           "caseId": {
               "title": "Case Id",
               "type": [
                   "null",
                   "string"
               ],
               "description": "desc.",
            }
       }
   }
}

如何通过 NJsonSchema c# 模式生成器做到这一点?

【问题讨论】:

    标签: c# njsonschema


    【解决方案1】:

    不确定这是否是最好的解决方案,但我最终这样做了:

    return new JObject
                {
                    { "schema", JToken.Parse(schema.ToJson()) }
                };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多