Skip to content

SupportNonNullableReferenceTypes not working for query parameters #2019

@Eneuman

Description

@Eneuman

Using v6.0.5 with .net Core 5 with non nullable reference types enabled.

It looks like c.SupportNonNullableReferenceTypes(); is not working for querystring parameters.

In a controller method I have the following parameter defined:

    [SwaggerOperation(OperationId = "GetGrid")]
    public async Task<ActionResult<ViewModels.Result>> GetGridAsync(string sorteringskolumn)

This generates the following json:

            "name": "sorteringsordning",
            "in": "query",
            "schema": {
              "type": "string"
            }

If I try and call this method without the parameter, the .Net Core Model Validation will give me back a HTTP 400 Bad Request and tell me that "sorteringskolumn" is required.

The problem here is that when generating a client from the above json, the client will not understand that the parameter is required since normaly, a string query parameter cannot be null and is by default "", but since non nullable reference type is enabled, the model validation behavior changes.

I think when using c.SupportNonNullableReferenceTypes () a more correct representation would be:

        "name": "sorteringsordning",
        "in": "query",
        "required": true,
        "schema": {
          "type": "string"
        }

The workaround is to add a [Required] infront of the parameter name in the controller, but this is awkvard since [Required] is not used when using non nullable reference types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    p1High priority

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions