Skip to content

Conversation

alpoi
Copy link
Contributor

@alpoi alpoi commented Jun 30, 2024

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
Copy link
Contributor Author

alpoi commented Jul 29, 2024

@RicoSuter ping!

@amis92
Copy link

amis92 commented Jul 29, 2024

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
Copy link
Contributor Author

alpoi commented Jul 29, 2024

@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?

/// <summary>Gets a value indicating whether the property is nullable.</summary>
public override bool IsNullable => (_settings.GenerateOptionalPropertiesAsNullable && !_property.IsRequired) || base.IsNullable;

@amis92
Copy link

amis92 commented Jul 29, 2024

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

@RicoSuter RicoSuter merged commit a968a09 into RicoSuter:master Mar 29, 2025
@RicoSuter
Copy link
Owner

Lgtm, thanks for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants