MeTitus
MeTitus

Reputation: 3428

How to make sure that NJsonSchema only includes required fields?

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

Upvotes: 2

Views: 4734

Answers (3)

rufreakde
rufreakde

Reputation: 642

For me since I had a library component where my class derived from. The solution was to have a wrapper in between and annotate that wrapper class.

[NJsonSchema.Annotations.JsonSchemaIgnore]
public class NonSchemaComponentWrapper : Component
{

}

original class:

[Serializable]
public class Unit : NonSchemaComponentWrapper, IUpdatable, IPathProvider, IInteractive, IAttack
{

Upvotes: 0

Rico Suter
Rico Suter

Reputation: 11858

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.

Upvotes: 5

Patrick
Patrick

Reputation: 5856

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.

Upvotes: 1

Related Questions