Skip to content

Question about OpenAPI and nullable value types #65280

@marty-shields

Description

@marty-shields

Hi there I was wondering if I could get some guidance on how I would best get around a situation I can't seem to find an answer to.

I have the following property which allows we to ensure the user provides a value and that it is between the range:

[Required, Range(0, int.MaxValue)]
    public int? TotalDurationMinutes { get; init; }

The problem with this comes into play with when viewing the open api document which is generated, because the type is a nullable type the openapi document is showing that this property can be null and required which is weird to me.

I have tried different things such as:

[Required, Range(0, int.MaxValue)]
public int TotalDurationMinutes { get; init; }

which gives me the expected result for the openapi generation however because binding happens between model validation it means the Required property is useless and I have no way to determine if the user has actually provided a property.

Another question for this would be for other types should as DateTime, how would I enforce the user has provided a value if the type is not the nullable version?

Or would this be going down somehow doing something within the openapi generation which would allow me to continue to use the nullable type but ignore that when it comes to the generation of the doc?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapiquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions