1

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

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
MeTitus
  • 3,390
  • 2
  • 25
  • 49

2 Answers2

4

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

  1. The property is marked with the JsonIgnoreAttribute property
  2. The class has an DataContractAttribute attribute and the property has no DataMemberAttribute and no JsonPropertyAttribute

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.

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
1

Since NJsonSchema has a dependency on Newtonsoft.Json, have you tried this from the Newtonsoft.Json documentation?

Conditional Property Serialization

To conditionally serialize a property, add a method that returns boolean with the same name as the property and then prefix the method name with ShouldSerialize. The result of the method determines whether the property is serialized. If the method returns true then the property will be serialized, if it returns false then the property will be skipped.

Patrick
  • 5,526
  • 14
  • 64
  • 101
  • It's a good one but that didn't fix it. Newtonsoft.Json also works with DataContracts but I gave it a try still and the result is the same. Maybe they just to a Type scanning and include all the properties...Thanks anyway :) – MeTitus Apr 19 '16 at 23:36
  • The JSON Schema generation is actually not a serialization process and thus not implemented in Json.NET but in NJsonSchema. The process only uses reflection to build up the JSON Schema... – Rico Suter Apr 20 '16 at 07:51