Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ bool IsNullableSchema(JsonSchemaExporterOptions options)

if (propertyInfo is not null)
{
return propertyInfo.IsGetNullable || propertyInfo.IsSetNullable;
return (propertyInfo.Get is not null && propertyInfo.IsGetNullable) ||

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eiriktsarpalis I'm thinking more. Is this whole idea even correct? A property isn't necessarily writeable only via a setter. It could be writeable via a constructor.

public class C
{
    public C(string? s) => S = s ?? string.Empty;

    // get-only property. Non-nullable.
    // But a null is okay to get deserialized.
    // It's not produced by serialization, though.
    public string S { get; }
}

(propertyInfo.Set is not null && propertyInfo.IsSetNullable);
Comment thread
Youssef1313 marked this conversation as resolved.
}

if (typeInfo.IsNullable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ public void TreatNullObliviousAsNonNullable_True_MarksReferenceTypesAsNonNullabl
Assert.Equal(expectedType, (string)schema["type"]!);
}

[Fact]
public void GetOnlyProperties_DoNotUseSetterNullabilityInSchema()
{
JsonNode schema = Serializer.DefaultOptions.GetJsonSchemaAsNode(typeof(PocoWithGetOnlyProperties));
const string ExpectedJsonSchema = """
{
"type": ["object", "null"],
"properties": {
"Values": {
"type": "array",
"items": { "type": ["string", "null"] }
},
"SingleValueGetOnly": { "type": "string" },
"NullableGetOnly": { "type": ["string", "null"] },
"SingleValueGetSet": { "type": "string" },
"NonNullableReadonlyField": { "type": "string" },
"NullableReadonlyField": { "type": ["string", "null"] }
}
}
""";

AssertValidJsonSchema(typeof(PocoWithGetOnlyProperties), ExpectedJsonSchema, schema);
}

[Theory]
[InlineData(typeof(Type))]
[InlineData(typeof(MethodInfo))]
Expand Down Expand Up @@ -262,6 +286,21 @@ public void LegacySchemaExporter_CanAccessReflectedMembers()

record PocoWithProperty(int Value);

sealed class PocoWithGetOnlyProperties
{
public IEnumerable<string> Values => [];
public string SingleValueGetOnly { get; } = "value";
public string? NullableGetOnly { get; }
public string SingleValueGetSet { get; set; } = "value";
[JsonInclude]
public readonly string NonNullableReadonlyField = "value";

#pragma warning disable CS0649 // field never assigned to
[JsonInclude]
public readonly string? NullableReadonlyField;
#pragma warning restore CS0649
}

[JsonSerializable(typeof(PocoWithProperty))]
partial class PocoWithPropertyContext : JsonSerializerContext;

Expand Down
Loading