1

I have a json schema as follows:

{
  "properties": {
    "ArrayTypeProperty": {
      "type": "array",
      "items": { "$ref": "#/definitions/types" }
    }
  },
  "definitions": {
    "types": {
      "properties": {
        "material": {
          "type": "string",
          "enum": [
            "beds",
            "offscreen",
            "wind",
            "weather"
          ]
        },
        "invisible": {
          "type": "string",
          "enum": [
            "air",
            "atmosphere"
          ]
        },
        "music": {
          "type": "string",
          "enum": [
            "rock",
            "metal",
            "classic"
          ]
        }
      }
    }
  },
  "additionalProperties": false
}

I have following code to generate csharp class:

var schemaJson = await JsonSchema.FromJsonAsync(m_schema);
var generator = new CSharpGenerator(schemaJson);
var file = generator.GenerateFile("MyClass");

This generates following class:

//----------------------
// <auto-generated>
//     Generated using the NJsonSchema v10.1.24.0 (Newtonsoft.Json v12.0.0.0) (http://NJsonSchema.org)
// </auto-generated>
//----------------------

namespace MyNamespace
{
    #pragma warning disable // Disable all warnings

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
    public partial class Types 
    {
        [Newtonsoft.Json.JsonProperty("material", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        public TypesMaterial Material { get; set; }
    
        [Newtonsoft.Json.JsonProperty("invisible", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        public TypesInvisible Invisible { get; set; }
    
        [Newtonsoft.Json.JsonProperty("music", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        public TypesMusic Music { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }
    
    
    }
    
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
    public partial class MyClass 
    {
        [Newtonsoft.Json.JsonProperty("ArrayTypeProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public System.Collections.Generic.ICollection<Types> ArrayTypeProperty { get; set; }
    
    
    }
    
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
    public enum TypesMaterial
    {
        [System.Runtime.Serialization.EnumMember(Value = @"beds")]
        Beds = 0,
    
        [System.Runtime.Serialization.EnumMember(Value = @"offscreen")]
        Offscreen = 1,
    
        [System.Runtime.Serialization.EnumMember(Value = @"wind")]
        Wind = 2,
    
        [System.Runtime.Serialization.EnumMember(Value = @"weather")]
        Weather = 3,
    
    }
    
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
    public enum TypesInvisible
    {
        [System.Runtime.Serialization.EnumMember(Value = @"air")]
        Air = 0,
    
        [System.Runtime.Serialization.EnumMember(Value = @"atmosphere")]
        Atmosphere = 1,
    
    }
    
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
    public enum TypesMusic
    {
        [System.Runtime.Serialization.EnumMember(Value = @"rock")]
        Rock = 0,
    
        [System.Runtime.Serialization.EnumMember(Value = @"metal")]
        Metal = 1,
    
        [System.Runtime.Serialization.EnumMember(Value = @"classic")]
        Classic = 2,
    
    }
}

Now i want to add a custom attribute "MyCustomAttribute" to the "ArrayTypeProperty" of "MyClass" so that the generated class would look like this:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
    public partial class MyClass 
    {
        [Newtonsoft.Json.JsonProperty("ArrayTypeProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [MyCustom]
        public System.Collections.Generic.ICollection<Types> ArrayTypeProperty { get; set; }    
    }

Could someone please help me understand if I can achieve this behavior with NJSonSchema and if yes, how?

Pooja Arya
  • 121
  • 1
  • 11
  • They talk about it here: https://github.com/RicoSuter/NJsonSchema/issues/1125 maybe it will help – Andy Sep 04 '20 at 03:25
  • This tells me how i can modify generated schema. In my case, i am generating class from schema so I doubt if i can use the schema processors with my scenario – Pooja Arya Sep 04 '20 at 16:54
  • I believe i need to extend the Class.Property.Annotations CSharpTemplates to allow adding custom attribute on the generated c# class. Is that correct? if so, could you share an example here? i cannot find any examples of that on wiki – Pooja Arya Sep 04 '20 at 16:58
  • I think this is correct, yes. – Rico Suter Nov 23 '20 at 21:28
  • @PoojaArya - Did u figure out how to generate custom attributes? pls let me know. – CodeTruthSeeker Nov 23 '22 at 17:45

0 Answers0