相关疑难解决方法(0)

如何配置Swashbuckle忽略模型上的属性

我正在使用Swashbuckle为webapi2项目生成swagger文档\ UI.我们的模型与一些传统接口共享,因此我想在模型上忽略一些属性.我不能使用JsonIgnore属性,因为旧版接口也需要序列化为JSON,所以我不想全局忽略这些属性,只是在Swashbuckle配置中.

我在这里找到了一种记录方法:

https://github.com/domaindrivendev/Swashbuckle/issues/73

但这似乎与目前的Swashbuckle版本已经过时了.

为旧版Swashbuckle推荐的方法是使用IModelFilter实现,如下所示:

public class OmitIgnoredProperties : IModelFilter
{
    public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
    {
        var ignoredProperties = … // use reflection to find any properties on 
                                  // type decorated with the ignore attributes

        foreach (var prop in ignoredProperties) 
            model.Properties.Remove(prop.Name);

    }
}

SwaggerSpecConfig.Customize(c => c.ModelFilter<OmitIgnoredProperties>());
Run Code Online (Sandbox Code Playgroud)

但我不确定如何配置Swashbuckle在当前版本中使用IModelFilter?我正在使用Swashbuckle 5.5.3.

c# asp.net-web-api swagger swashbuckle

32
推荐指数
12
解决办法
3万
查看次数

使用 Newtonsoft 在 C# 中使用 JSON Schema 验证 JSON

使用 JSON Schema 验证 JSON 始终返回 true。Newtonsoft 用于验证并在此处使用模式和数据进行测试。它总是返回“未发现错误。JSON 根据架构进行验证。

请找到我的 JSON 架构。


{
  "schema": {
    "definitions": {
    },
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "widget": { "formlyConfig": { "type": "accordion" } },
    "title": "The Root Schema",
    "required": [
      "accordion1",
      "accordion2",
      "accordion3"
    ],
    "properties": {
      "accordion1": {
        "$id": "#/properties/accordion1",
        "type": "object",
        "title": "The Accordion1 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion1/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstname pvr1"
          },
          "age": {
            "$id": …
Run Code Online (Sandbox Code Playgroud)

c# json json.net jsonschema

4
推荐指数
1
解决办法
5348
查看次数

标签 统计

c# ×2

asp.net-web-api ×1

json ×1

json.net ×1

jsonschema ×1

swagger ×1

swashbuckle ×1