0

I'm running NSwagStudio v13.15.10.0 to create C# client/contract files.

I want the ReadOnly model attribute to be including in the generated C# Contract. Is this possible?

Given a sample Person model containing:

    /// <summary>
    /// Person Actual Birthdate (yyyy-mm-dd)
    /// </summary>
    public DateOnly Birthdate { get; set; }

    /// <summary>
    /// Computed Next Birthday Relative to Today (yyyy-mm-dd)
    /// </summary>
    [ReadOnly(true)]
    public DateOnly NextBirthday { get; }`

The resulting swagger schema shows:

    Birthdate   string($date)
    NextBirthday    string($date)
                    readOnly: true`

And the JSON contains:

      "Birthdate": {
        "type": "string",
        "format": "date"
      },
      "NextBirthday": {
        "type": "string",
        "readOnly": true,
        "format": "date"
      },`

The resulting Contracts.cs contains:

    [Newtonsoft.Json.JsonProperty("Birthdate", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    [Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]
    public System.DateTimeOffset Birthdate { get; set; }

    [Newtonsoft.Json.JsonProperty("NextBirthday", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    [Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]
    public System.DateTimeOffset NextBirthday { get; set; }

I want the NextBirthday to include this additional attribute:

[System.ComponentModel.ReadOnly(isReadOnly : true)]

resulting in:

    [Newtonsoft.Json.JsonProperty("NextBirthday", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    [Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]
    [System.ComponentModel.ReadOnly(isReadOnly : true)]
    public System.DateTimeOffset NextBirthday { get; set; }

It looks like I'd need to modify this file: https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid

I'm not sure how to modify a "liquid" file or how to rebuild NSwagStudio but I don't want a custom version of NSwag. I'm looking for a better solution that doesn't involve me editing the Contracts.cs each time.

Bob Duffett
  • 63
  • 1
  • 5

0 Answers0