I'm using NJsonSchema v2.6 for generating the JSON Schema for the following class:
[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; }
}
The generated 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"
}
}
}
]
}
Even though the MessageDate property is not marked as a DataMember, it is always included in the schema, also the generated schema includes two schema paths when it should only include one, it seems that the parser is not flattening the properties.
UPDATE
This fixes the issue with multiple schema paths being created
new JsonSchemaGeneratorSettings
{
FlattenInheritanceHierarchy = true
}
GitHub Issue: https://github.com/NJsonSchema/NJsonSchema/issues/53
I'm the author of the library NJsonSchema.
Ignored properties
There was a bug in the library and now (v2.7+) property ignore works as follows:
A property is ignored when either
- The property is marked with the
JsonIgnoreAttribute
property- The class has an
DataContractAttribute
attribute and the property has noDataMemberAttribute
and noJsonPropertyAttribute
https://github.com/NJsonSchema/NJsonSchema/wiki/JsonSchemaGenerator
Flatten inheritance hierarchy
As you already found out, you can flatten the inheritance hierarchy via the FlattenInheritanceHierarchy
setting...
The library is mainly used for code generation, and thus the inheritance is usually needed.