Skip to content

Tags: vbegin/NJsonSchema

Tags

v11.6.1

Toggle v11.6.1's commit message

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
required string? incorrectly loses nullable and gains MinLength=1 (Ri…

…coSuter#1919)

* Fix required string? incorrectly losing nullable and gaining MinLength=1

Commit ec1f9c3 correctly wired up RequiredMemberAttribute (C# 11 required)
to add properties to the required array in the schema. The mistake was
folding it into the existing hasRequiredAttribute boolean, which is also
used in both reflection services to suppress nullability and in
JsonSchemaGenerator.cs:1216 to add MinLength=1 to strings.

For required string?, propertyTypeDescription.IsNullable is true but
hasRequiredAttribute is now also true (due to RequiredMemberAttribute), so
isNullable becomes false. nullable:true is dropped and MinLength=1 is added.
Both are wrong.

The semantic distinction is:

  [Required] / [JsonRequired]: "this value must be non-null and non-empty"
    -- suppressing nullability and adding MinLength=1 are correct
  C# 11 required keyword: "this property must be present in object
    initializers / JSON deserialization, but the value can still be null"
    -- should only add to the required array; nullability and MinLength
    come from the type alone

Fix: introduce hasSemanticRequiredAttribute (excludes RequiredMemberAttribute)
for the isNullable and MinLength decisions in both reflection services.

  C# property                  | required array | nullable | minLength
  -----------------------------|----------------|----------|----------
  string name                  | no             | no       | -
  string? name                 | no             | yes      | -
  [Required] string name       | yes            | no       | 1
  [Required] string? name      | yes            | no       | 1
  required string name         | yes            | no       | -
  required string? name        | yes            | yes      | -
  [JsonRequired] string? name  | yes            | no       | -

The Newtonsoft path has no hasJsonRequiredAttribute concept, so it only
needs the requiredAttribute == null guard. The System.Text.Json path
keeps [JsonRequired] in the semantic bucket since [JsonRequired] explicitly
states the JSON value must not be null, unlike the language-level required
keyword.

Fixes RicoSuter#1918

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* Treat [JsonRequired] as a presence marker, not a value constraint

[JsonRequired] in System.Text.Json indicates the JSON property must be
present during deserialization, not that the value must be non-null.
Treat it the same as the C# required keyword: add the property to the
schema's required array, but preserve the declared nullability and do
not add MinLength = 1 for strings.

Only [Required] from DataAnnotations carries the 'non-null value
required' semantics that suppress nullability and set MinLength = 1.

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Rico Suter <mail@rsuter.com>

v11.5.1

Toggle v11.5.1's commit message

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Prepare v11.5.1

v11.5.0

Toggle v11.5.0's commit message
Update Namotion.Reflection to v3.4.3

v11.4.0

Toggle v11.4.0's commit message

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Optimize camel-casing logic (RicoSuter#1850)

v11.3.2

Toggle v11.3.2's commit message
v11.3.2 (revert PR)