Skip to content

Support C#11 required keyword in schema generation#1908

Merged
lahma merged 1 commit intoRicoSuter:masterfrom
lahma:fix/required-keyword-1879
Mar 8, 2026
Merged

Support C#11 required keyword in schema generation#1908
lahma merged 1 commit intoRicoSuter:masterfrom
lahma:fix/required-keyword-1879

Conversation

@lahma
Copy link
Copy Markdown
Collaborator

@lahma lahma commented Mar 8, 2026

Summary

Test plan

  • Added STJ test: required string Name → property is in RequiredProperties
  • Added STJ test: [JsonRequired] string Name → property is in RequiredProperties
  • Added Newtonsoft test: required string Name → property is in RequiredProperties
  • All tests pass (459 passed + 2 Newtonsoft, 7 skipped, 0 failed)

🤖 Generated with Claude Code

…icoSuter#1672)

Recognize RequiredMemberAttribute (C# required keyword) and
JsonRequiredAttribute (STJ) when determining if a property should
be marked as required in the generated schema. Also guard against
null requiredAttribute in the MinLength logic.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lahma lahma force-pushed the fix/required-keyword-1879 branch from 5f384d8 to 4cfeb3f Compare March 8, 2026 17:59
@lahma lahma merged commit ec1f9c3 into RicoSuter:master Mar 8, 2026
3 of 4 checks passed
@lahma lahma deleted the fix/required-keyword-1879 branch March 8, 2026 18:41
@TheENTsAreGoingToWar
Copy link
Copy Markdown

Thanks for doing that!

@killergege
Copy link
Copy Markdown

Hello, this change seem to have introduced an issue RicoSuter/NSwag#5359

@RicoSuter
Copy link
Copy Markdown
Owner

RicoSuter commented Apr 13, 2026

Yeah just was thinking about this, and was wondering whether this PR is really correct. C# required is not about nullable or JSON required but only says that the property needs to be set at creation time... maybe its conceptually wrong to assume required == property required (aka not nullable). These are the changes which needs to be done with extreme care...

Not sure, maybe we need to revert this or make it opt-in

/cc @lahma

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.

JSON Schema Generation required modifier

4 participants