NJsonSchema icon indicating copy to clipboard operation
NJsonSchema copied to clipboard

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

Open alpoi opened this issue 1 year ago • 4 comments

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 avatar Jun 30 '24 17:06 alpoi

@RicoSuter ping!

alpoi avatar Jul 29 '24 15:07 alpoi

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...

amis92 avatar Jul 29 '24 16:07 amis92

@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

alpoi avatar Jul 29 '24 17:07 alpoi

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

amis92 avatar Jul 29 '24 17:07 amis92

Lgtm, thanks for the PR.

RicoSuter avatar Mar 29 '25 13:03 RicoSuter