RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.4k stars 535 forks source link

Support generating 'null-and-missing-properties-tolerant' C# classes from non-tolerant contracts #135

Open slawomir-brzezinski-at-interxion opened 8 years ago

slawomir-brzezinski-at-interxion commented 8 years ago

In order to choose the way of the TolerantReader.

I have a swagger.json with

...
      "properties": {
        "propertyICareAbout": {
          "type": "string",
          "required": true
        },
        "propertyIDontCareAbout1": {
          "type": "string",
          "required": true
        },
        "propertyIDontCareAbout2": {
          "type": "string",
          "required": true
        },
        ...
        ...
        ...
        "propertyIDontCareAbout99999": {
          "type": "string",
          "required": true
        }
     }
....

It's fine that the author of the service made these properties required and non-nullable and I don't want to change that.

Still, the service is going to be evolving, possibly rearranging the properties I don't care about, therefore I would like there to be an option for me to decide to only fail with NullReferenceException when I actually consume, and not when trying to deserialize the JSON.

In essence, I'd like an option to altogether disable adding the [Required] and [JsonProperty(... Required = Required.DisallowNull)] in the clients I generate (or always use Required.Default, whichever is the right way to do it).

slawomir-brzezinski-at-interxion commented 8 years ago

Currently, the workaround I found was to expose a fake Required.DisallowNull field that changes it to Required.Default and a moot RequiredAttribute like:

    public static class Required
    {
        public const Newtonsoft.Json.Required DisallowNull = Newtonsoft.Json.Required.Default;
    }

    public class RequiredAttribute : Attribute {}

but I think that writing a tolerant reader is sometimes a true necessity, so it's a shame that this library cannot be used with this approach.

RicoSuter commented 8 years ago

Have you tried to set RequiredPropertiesMustBeDefined to false. Isnt this what you are looking for?

https://github.com/NJsonSchema/NJsonSchema/wiki/CSharpGenerator

slawomir-brzezinski-at-interxion commented 8 years ago

Thanks @rsuter . I'll give it a go soon.

slawomir-brzezinski-at-interxion commented 8 years ago

@rsuter actually, I already tried it but it didn't give me what I want, because the wire payload has actually sent the property, but it sent it as null. The setting will not help in such situation.

(it's actually due to a bug in the other endpoint that its generated swagger doesn't say it will send null values).

RicoSuter commented 8 years ago

Btw: type: [array, null] is not allowed in swagger.. (big problem). Is the property not required and still null is not allowed?

Try http://nswag.org :)

slawomir-brzezinski-at-interxion commented 7 years ago

Yes, If I could control that requestor :)