Skip to content

Microsoft.AspNetCore.OpenApi specifies non-nullable get-only properties as nullable #58192

@ptffr

Description

@ptffr

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If nullable reference types are enabled and a class has a get only non-nullable property, Microsoft.AspNetCore.OpenApi will incorrectly specify the property as nullable.

I looked at the source code and it seems like JsonNodeSchemaExtensions.ApplyNullabilityContextInfo(JsonNode, JsonPropertyInfo) is the culprit. Specifically this part propertyInfo.IsGetNullable || propertyInfo.IsSetNullable.

Should it even check for IsSetNullable when the property does not have a setter?

A temporary workaround for the library consumer would be to introduce an unused setter, eg

public IEnumerable<string> Values
{
  get => [];
  private set {}
}

Expected Behavior

Microsoft.AspNetCore.OpenApi generates correct nullability information for schema properties.

Steps To Reproduce

https://github.com/keenjus/OpenApiStuff/tree/2e83b32a316500018eb216be015c677933cfa73b

Launch the project and inspect the generated OpenApi document at /openapi/v1.json. The property values is specified as nullable for EntityCompany/EntityPerson schemas.

Exceptions (if any)

No response

.NET Version

9.0.100-rc.1.24452.12

Anything else?

Microsoft.AspNetCore.OpenApi 9.0.0-rc.1.24452.1

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions