我正在使用NJsonSchema v2.6 为以下类生成 JSON Schema:
[DataContract(Name = "Message", Namespace = "")]
public class AMessageModel
{
[DataMember]
internal Guid MessageId { get; set; }
internal DateTime MessageDate { get; set; }
}
[DataContract(Name = "Message", Namespace = "")]
public class AddUserMessage : AMessageModel
{
[DataMember]
public string AccountName { get; set; }
[DataMember]
public string FistName { get; set; }
[Range(2, 5)]
[DataMember]
public string LastName { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Password { get; set; }
}
生成的 JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"typeName": "AddFitnessHubAccountMessage",
"additionalProperties": false,
"properties": {
"AccountName": {
"type": [
"null",
"string"
]
},
"FistName": {
"type": [
"null",
"string"
]
},
"LastName": {
"type": [
"null",
"string"
]
},
"Email": {
"type": [
"null",
"string"
]
},
"Password": {
"type": [
"null",
"string"
]
}
},
"allOf": [
{
"type": "object",
"typeName": "AMessageModel",
"additionalProperties": false,
"properties": {
"MessageId": {
"type": "string",
"format": "guid"
},
"MessageDate": {
"type": "string",
"format": "date-time"
}
}
}
]
}
尽管 MessageDate 属性未标记为 DataMember,但它始终包含在模式中,并且生成的模式包含两个模式路径,而它应该只包含一个,似乎解析器没有扁平化属性。
更新
这解决了创建多个模式路径的问题
new JsonSchemaGeneratorSettings
{
FlattenInheritanceHierarchy = true
}
GitHub问题:https ://github.com/NJsonSchema/NJsonSchema/issues/53