如何使用Json.NET用$ref反序列化Swagger文档



我需要反序列化以下Json:

{
    "swagger": "2.0",
    "info": {
        "version": "0.0.0",
        "title": "Simple API"
    },
    "paths": {
        "/": {
            "get": {
                "parameters": [
                    {
                        "$ref": "#/parameters/MyParameter"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/MyDefinition"
                        }
                    }
                }
            }
        }
    },
    "parameters": {
        "MyParameter": {
            "name": "MyParameter",
            "in": "query",
            "type": "string"
        }
    },
    "definitions": {
        "MyDefinition": {
            "type": "string"
        }
    }
}

这些是我用Json定义的契约。净

    public class SwaggerDocument
    {
        public IDictionary<string, Schema> definitions;
        public Info info;
        public IDictionary<string, Parameter> parameters;
        public IDictionary<string, PathItem> paths;
        public readonly string swagger;
        public Dictionary<string, object> vendorExtensions;
    }
    public class Schema
    {
        [JsonProperty("$ref", IsReference = true)]
        public string @ref;
        public string type;
    }
    public class Info
    {
        public string title;
        public Dictionary<string, object> vendorExtensions;
        public string version;
    }
    public class Parameter
    {
        public string name;
        public string @in;
        public string type;
        public Dictionary<string, object> vendorExtensions;
    }
    public class PathItem
    {
        public Operation get;
        public Dictionary<string, object> vendorExtensions;
    }
    public class Operation
    {
        public IList<Parameter> parameters;
        public IDictionary<string, Response> responses;
    }
    public class Response
    {
        public string description;
        public Schema schema;
    }

我希望下面的内容可以通过,但是parameter和definition都是null:

    [TestMethod]
    public void ShouldDeserializeReferences()
    {
        var swagger = JsonConvert.DeserializeObject<SwaggerDocument>(SwaggerWithRefs);
        var operation = swagger.paths.Values.First().get;
        var parameter = operation.parameters.First();
        var definition = operation.responses.First().Value.schema;
        Assert.AreEqual("MyParameter", parameter.name);
        Assert.AreEqual("string", definition.type);
    }
    private static readonly string SwaggerWithRefs = "{rn    "swagger": "2.0",rn    "info": {rn        "version": "0.0.0",rn        "title": "Simple API"rn    },rn    "paths": {rn        "/": {rn            "get": {rn                "parameters": [rn                    {rn                        "$ref": "#/parameters/MyParameter"rn                    }rn                ],rn                "responses": {rn                    "200": {rn                        "description": "OK",rn                        "schema": {rn                            "$ref": "#/definitions/MyDefinition"rn                        }rn                    }rn                }rn            }rn        }rn    },rn    "parameters": {rn        "MyParameter": {rn            "name": "MyParameter",rn            "in": "query",rn            "type": "string"rn        }rn    },rn    "definitions": {rn        "MyDefinition": {rn            "type": "string"rn        }rn    }rn}";

是否有可能以一种使测试中的断言为真的方式定义契约?请注意,http://editor.swagger.io/解析这个json没有问题。

编辑:引用的答案只提到反序列化为jobobject。有没有一种方法可以让合同产生同样的行为?

public class SwaggerDocument
{
    public string host { get; set; }
    public IDictionary<string, IDictionary<string, PathItem>> paths;
}
public class PathItem
{
    public string summary { get; set; }
    public string operationId { get; set; }
    public object parameters { get; set; }
    public object responses { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新