stevo
stevo

Reputation: 108

cannot convert from 'Microsoft.OpenApi.Models.OpenApiInfo' to 'Swashbuckle.AspNetCore.Swagger.Info'

I'm getting this error when tryign to run swashbuckle. Any ideas?

I have this in my ConfigureServices Class

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "testing", 
                Version = "v1" });
            });

And this in my Configure Class

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

Upvotes: 8

Views: 12297

Answers (4)

Anish Kutti
Anish Kutti

Reputation: 354

Here is a sample for swagger 5.5.0 &> for C#

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Title = "API Title is",
                    Version = "v1"
                });
            });
        }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
        //app.UseMvc();
        app.UseSwagger();
        app.UseSwaggerUI(c => {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 api test");
        });
    }

Upvotes: 2

Dude Pascalou
Dude Pascalou

Reputation: 3171

Before Swashbuckle.AspNetCore version 5.0.0 (which is not yet released, on november 2019), SwaggerDoc extension method requires a Swashbuckle.AspNetCore.Swagger.Info parameter. So, you have to write :

// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});

Especially, if you are coming from : Get started with Swashbuckle and ASP.NET Core

Beginning with version 5.0.0, SwaggerDoc extension method now requires a Microsoft.OpenApi.Models.OpenApiInfo parameter.

Upvotes: 10

Rahul Dhoundiyal
Rahul Dhoundiyal

Reputation: 81

You need to Upgrading the package Swashbuckle.AspNetCore to the latest version v 5.0.0-rc5 solved the problem.

in that package its already including Microsoft.OpenApi package assembly you don't need to addon additionally. you OpenApiInfo, OpenApiContract, and much more property are directly inherited with the upgradable package.

Upvotes: 1

user11825661
user11825661

Reputation:

I ran into this same issue today. After looking at the Microsoft documentation, it appears that SwaggerDoc is looking for a string and an Info parameter. At the top of your file make sure that you include using Swashbuckle.AspNetCore.Swagger; and replace OpenApiInfo with Info.

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "testing", 
            Version = "v1" });
        });

Upvotes: 16

Related Questions