We are making an upgrade of our webapi to enable OAS3.0 in our dotnet Core application and from the documentation it seems that it is only supported in SwashBuckle.Aspnetcore 5.4.1 + versions. As mentioned here : https://github.com/domaindrivendev/Swashbuckle.AspNetCore
So we upgraded our SwashBuckle.Aspenetcore to 5.4.1. When we ran the applicaiton though, it still does produce swagger 2.0 instead of OAS 3.01. Now, the documentation on the same page does not specify anything special that we have todo when you upgrade from swagger2.0-->OAS3.0
Currently my little configuration looks like below:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/myapp/swagger/v1/swagger.json", "API name");
c.RoutePrefix = "myapp/swagger";
c.DisplayRequestDuration();
c.DisplayOperationId();
});
The documentation on the page also point to a page mentioning the support for OAS3.0 in a link here: https://swagger.io/specification/
But again not code to reference to. Can someone point me to specifics of how to upgrade my API from swagger 2.0 to OAS 3.0?
It looks like you have c.SerializeAsV2 = true somewhere in the code. To output OpenAPI 3.0, this option must be false.
Related
Currently I am using .netcore 3.1 project. Started migrating it to .net6.0
Below code is currently implemented in .netcore3.1
app.UseMvc(b =>
{
b.MapVersionedODataRoutes("odata-versioned", "odata", edmModels);
});
After migrating the framework to .net6.0 and I am getting error that MapVersionedODataRoutes is not available.
Are there a breaking changes? What is the new way of implementing the same.
Got stuck as the MapVersionedODataRoutes was not available
Do you want to implement API versioning? In OData 8.0.11, you can implement it by AddRouteComponents:
builder.Services.AddControllers()
.AddOData(opt => opt.AddRouteComponents("v{version}", edmModel));
For more details, you can refer to this link.
In addition, Microsoft.AspNetCore.OData.Versioning contains a MapVersionedODataRoute property, which may help you.
I have a .net core 2.2 Class Library.
I have installed the VS Studio 2017 "OpenAPI (Swagger) Connected Service" extension.
I have attempted to use this extension to generate a c# client for the following API:
https://skybox.vividseats.com/services/openapi.json
The extension runs and builds a number of files:
But, when I build the project I have 1640 errors:
It appears to have generated all the functions and named then as 1Async, 2Async etc....
Can anyone see anything wrong that I am doing? Or suggest another method to generate a client from the url?
Any help would be greatly appreciated!
You may want to try OpenAPI Generator to generate C# .NET Core client:
Download latest stable version v4.1.3: http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.1.3/openapi-generator-cli-4.1.3.jar, and rename it as openapi-generator-cli.jar
java -jar openapi-generator-cli.jar generate -g csharp-netcore -i https://skybox.vividseats.com/services/openapi.json -o /var/tmp/ --skip-validate-spec
I can build the project without issue. Please test it to see if it works for you.
There are spec validation errors so I use --skip-validate-spec to skip those errors. Please review those errors when you've time.
There are other ways to install OpenAPI Generator: https://github.com/OpenAPITools/openapi-generator#1---installation
UPDATE: we've added a csharp-netcore client generator. Please check out the latest master of v5.0.0 release to give it a try.
I've finally found a solution to this problem.
The solution is to define OperationId for every endpoint.
You can do this in different ways, I think it's the best described here.
I do this in the SwaggerDefaultValues:
var controllerActionDescriptor = (ControllerActionDescriptor)apiDescription.ActionDescriptor;
var controllerName = controllerActionDescriptor.ControllerName;
var actionName = controllerActionDescriptor.ActionName;
operation.OperationId ??= $"{controllerName}{actionName}";
It's important to note that operation id SHOULD NOT contain underscores.
I'm writing a console application in dotnet core 3.1 and using the Microsoft.Identity.Client library 4.14, and I have the following code:
result = await App.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.WithProofOfPossession()
.ExecuteAsync();
But I get Cannot resolve symbol 'WithProofOfPossession'. I can access it in an Net45 app, but not in the netcoreapp3.1 app. Why is that, and how can I fix it?
It's not supported yet
From their repository wiki (bold are mine):
This is a new feature introduced in MSAL 4.8. Currently it is supported only in .net 45 and for public client flows.
Also, not sure if this will also apply to you (because the method should be there anyway), but there was a bug (however fixed in 4.1 supposedly) which didn't expose the method:
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/1715
I've just upgraded my ASP web API project from .Net core 2.0 to 3.0. I was using
services.AddMvc()
.AddJsonOptions(options =>options.SerializerSettings.ContractResolver
= new DefaultContractResolver());
previously to ensure lower-casing of the serialized JSON.
After the upgrade to 3.0 I get this error:
Error CS1061 'IMvcBuilder' does not contain a definition for
'AddJsonOptions' and no accessible extension method 'AddJsonOptions'
accepting a first argument of type 'IMvcBuilder' could be found (are
you missing a using directive or an assembly reference?)
According to AddJsonOptions for MvcJsonOptions in Asp.Net Core 2.2 the AddJsonOptions extension method is/was provided by the Microsoft.AspNetCore.Mvc.Formatters.Json nuget package. I have tried installing/reinstalling this but still can't resolve the method. Interestingly, intellisense only shows Microsoft.AspNetCore.Mvc.Formatters.Xml when I try to add the using statement even though I added the Json nuget package.
Any ideas what is going on? The documentation for AddJsonOptions only goes up to .Net 2.2 so perhaps the method has been deprecated in 3.0 in favor of some other configuration mechanism?
As part of ASP.NET Core 3.0, the team moved away from including Json.NET by default. You can read more about that in general in the announcement on breaking changes to Microsoft.AspNetCore.App.
Instead of Json.NET, ASP.NET Core 3.0 and .NET Core 3.0 include a different JSON API that focuses a bit more on performance. You can learn about that more in the announcement about “The future of JSON in .NET Core 3.0”.
The new templates for ASP.NET Core will no longer bundle with Json.NET but you can easily reconfigure the project to use it instead of the new JSON library. This is important for both compatibility with older projects and also because the new library is not supposed to be a full replacement, so you won't see the full feature set there.
In order to reconfigure your ASP.NET Core 3.0 project with Json.NET, you will need to add a NuGet reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson, which is the package that includes all the necessary bits. Then, in the Startup’s ConfigureServices, you will need to configure MVC like this:
services.AddControllers()
.AddNewtonsoftJson();
This sets up MVC controllers and configures it to use Json.NET instead of that new API. Instead of controllers, you can also use a different MVC overload (e.g. for controllers with views, or Razor pages). That AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
This worked for me, while using .Net Core 3:
services.AddMvc().AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
Make sure that you installed the Microsoft.AspNetCore.Mvc.NewtonsoftJson package.
It's work for me, Install the NewtonsoftJson package from NuGet "dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 3.1.0" version 3.1.0 working for ASP.NET Core 3.0 and use the Following Code-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(opt => {
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
Hope it's Working Fine, Thanks.
This would help
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(options=> { options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.DictionaryKeyPolicy = null;
});
services.AddDbContext<PaymentDetailContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
}
This would help try Installing the Nuget Package
Microsoft.AspNetCore.Mvc.NewtonsoftJson
This worked for me, while using .Net Core 3:
click here
I am able to integrate the Swagge UI in my web api using Swashbuckle. I also want to explore the swagger codegen feature. Can somebody help in - how I can integrate swagger codegen into my web api project? Or do I need to download any tool? I want to be able it to host the codegen and pass the json/raml form specs to generate client in .net core.
I am not able to find enough docs on above.
EDIT : I want to know how I can host codegen in my WEBAPI.
Thanks!
Now, you can use Nswag. There are several code generator utilities - UI, Console, msbuild.
You should install "Swashbuckle.AspNetCore.Swagger" nuget package by right click your project and click manage nuget packages.
Then you should add these codes into your project startup place eg. Program.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
app.UseMvc();
}
It seems like you just want to generate C# from the OpenApi specification (your Swagger implementation provides the input) of your API.
To generate code (e.g. C#) from the OpenApi spec file of your API, you can do something like this:
java -jar .\openapi-generator-cli-5.0.0-beta3.jar generate -i https://localhost:xxxx/api/v1/swagger.json -g csharp
You have to download the OpenApi Generator Jar. Alternatively you can upload your code to a web generator. But I would always run this locally; you never know where your code ends up.