RicoSuter / NJsonSchema

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

feat: option to use c# 11 required keyword for required properties #1711

Open alpoi-x opened 1 month ago

alpoi-x commented 1 month ago

This PR adds a UseRequiredKeyword option to the C# code generator. This will use the required modifier, added in C#11, for any properties that are required in the json schema.

alpoi-x commented 1 month ago

@RicoSuter ping!

amis92 commented 1 month ago

I'd love this feature!

I was just looking how to do that myself. I do have an additional require-ment (jk), namely I want nullable properties (both structs and NRTs) to not have that keyword. I see that there's already a similar setting GenerateOptionalPropertiesAsNullable - would it be possible to also have a SkipRequiredKeywordOnNullableProperties with preferably better name, or an opposite of it? Pretty pretty please...

alpoi-x commented 1 month ago

@amis92 Isn't this already the case, given that a property is only treated as nullable if GenerateOptionalPropertiesAsNullable is enabled and the property is not required? https://github.com/RicoSuter/NJsonSchema/blob/9bf8f695b373410e8b51e1363270c08dda7b8127/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs#L54-L55

amis92 commented 1 month ago

That, or the null is allowed by type JSON Schema keyword.