Skip to content

Discrepancy in nullability for parameters OptionalValues.OpenApi vs OptionalValues.Swashbuckle #36

Description

@espenrl

First of all thanks for a great library. Worthy of being part of STJ itself.

Using OptionalValues.Swashbuckle I found nullability to not being output for parameters (in swagger.json) . I then tried OptionalValues.OpenApi which do behave as expected.

Example code that reproduces the issue

public record UserUpdateRecord(Guid UserId)
{
    public OptionalValue<string?> Name { get; set; }
    public OptionalValue<string?> Email { get; set; }
}

[HttpPut]
public async Task<ActionResult<UserResponse>> UpdateUser(UserUpdateRecord userUpdateRecord)
{
    ...
}

Code difference OptionalValues.OpenApi vs OptionalValues.Swashbuckle

There is a difference in the source code with regards to handling of parameters and nullability. Perhaps that is the source of the issue, see code below.

OptionalValues.OpenApi

private static NullabilityInfo? GetNullabilityInfo(ICustomAttributeProvider memberInfo)
{
var nullabilityInfoContext = new NullabilityInfoContext();
return memberInfo switch
{
PropertyInfo propertyInfo => nullabilityInfoContext.Create(propertyInfo),
FieldInfo fieldInfo => nullabilityInfoContext.Create(fieldInfo),
ParameterInfo parameterInfo => nullabilityInfoContext.Create(parameterInfo),
_ => null,
};
}

OptionalValues.Swashbuckle

private static NullabilityInfo? GetNullabilityInfo(MemberInfo memberInfo)
{
var nullabilityInfoContext = new NullabilityInfoContext();
return memberInfo switch
{
PropertyInfo propertyInfo => nullabilityInfoContext.Create(propertyInfo),
FieldInfo fieldInfo => nullabilityInfoContext.Create(fieldInfo),
_ => null,
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions