Swashbuckle.aspnetcore: <NonBodyParameter> equivalent

Created on 14 Nov 2019  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Hey, I updated .netcore3 and swashbuckle v5 so now I have problem with Microsoft.AspNetCore.Mvc.Versioning project.My filter look like this :

 public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            var apiDescription = context.ApiDescription;

            operation.Deprecated |= apiDescription.IsDeprecated();

            if (operation.Parameters == null)
            {
                return;
            }

            // REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/412
            // REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/413
            foreach (var parameter in operation.Parameters.OfType<NonBodyParameter>())
            {
                var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);

                if (parameter.Description == null)
                {
                    parameter.Description = description.ModelMetadata?.Description;
                }

                if (parameter.Default == null)
                {
                    parameter.Default = description.DefaultValue;
                }

                parameter.Required |= description.

The type or namespace name 'NonBodyParameter' could not be found (are you missing a using directive or an assembly reference?

Is there any equivalent of this?

Most helpful comment

Same thing happened to me. I switched to OpenApiParameter and used the In = ParameterLocation.Header property. It's not exactly your usage, but hopefully this helps.

BEFORE

operation.Parameters?.Add( new NonBodyParameter
{
    Name = "Authorization",
    In = "header",
    Description = "Bearer access token",
    Required = true,
    Type = "string"
});

AFTER

operation.Parameters?.Add( new OpenApiParameter
{
    Name = "Authorization",
    In = ParameterLocation.Header,
    Description = "Bearer access token",
    Required = true,
    Schema = new OpenApiSchema { Type = "string" }
});

All 9 comments

Same thing happened to me. I switched to OpenApiParameter and used the In = ParameterLocation.Header property. It's not exactly your usage, but hopefully this helps.

BEFORE

operation.Parameters?.Add( new NonBodyParameter
{
    Name = "Authorization",
    In = "header",
    Description = "Bearer access token",
    Required = true,
    Type = "string"
});

AFTER

operation.Parameters?.Add( new OpenApiParameter
{
    Name = "Authorization",
    In = ParameterLocation.Header,
    Description = "Bearer access token",
    Required = true,
    Schema = new OpenApiSchema { Type = "string" }
});

EDIT: Nevermind, it seems v5 detects file uploads and annotates them automatically now, a good thing! Before I needed an IOperationFilter, seems I can ditch that now.

What is the new equivalent for file uploads? This works in v4:
operation.Parameters.Add(new NonBodyParameter
{
Name = "uploadedFile",
In = "formData",
Description = "Upload File",
Required = true,
Type = "file"
})

In v5 OpenApiParameter does not have a FormData type for In and there is not Type field at all.

Just to provide another example, I used this to add the following headers per-request:
image

operation.Parameters.Add(new NonBodyParameter
{
    Name = "Accept-Language",
    In = "header",
    Type = "string",
    Enum = new List<object> { "pt-BR", "en-US" },
    Default = "pt-BR"
});

Is there any way to replicate this behavior with the new OpenApiParameter, since it doesn't have the Type, Enum, and Default fields?

@CleytonGoncalves looks like it might have all been moved into the Schema property and wrapped:

operation.Parameters.Add(new OpenApiParameter
{
    Name = "Accept-Language"
    In = ParameterLocation.Header,
    Schema = new OpenApiSchema 
    { 
        Type = "string", 
        Enum = new List<IOpenApiAny>
        {
            new OpenApiString("pt-BR"), 
            new OpenApiString("en-US")
        }, 
        Default = new OpenApiString("pt-BR")
    }   
});

@joannayankova can this be closed?

Closing due to inactivity

@domaindrivendev please answer this question

EDIT: Nevermind, it seems v5 detects file uploads and annotates them automatically now, a good thing! Before I needed an IOperationFilter, seems I can ditch that now.

What is the new equivalent for file uploads? This works in v4:
operation.Parameters.Add(new NonBodyParameter
{
Name = "uploadedFile",
In = "formData",
Description = "Upload File",
Required = true,
Type = "file"
})

In v5 OpenApiParameter does not have a FormData type for In and there is not Type field at all.

First of all, you guys completely broke this for no reason. So there's that.

And then nobody answered this question - what did you break it to? Nobody can figure it out.

Why is it broken? Nobody knows. Stop breaking stuff.

Was this page helpful?
0 / 5 - 0 ratings