How to inject openapi specification file with Swashbuckle.AspNetCore - swagger

services.AddSwaggerGen(x =>
{
});
I have explored the AddSwaggerGen() method of NuGet Swashbuckle.AspNetCore. Unfortunately, I haven't found a way to use the path of the already generated open API specification file.
My APIs are related to file upload in .net core and I have already written the swagger specification file swagger.yaml and It is working fine with editor.swagger.io. Can anyone help me? How can I inject the generated open API specification file and use it with SwaggerUI in .net core with NuGet Swashbuckle.AspNetCore?
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/swagger.yaml", "My API V1");
});
I have created a static file /swagger/swagger.yaml in my project But Swagger UI still using the same file which is generated by internally.

I also stumbled upon this. One working solution is to host open-api/swagger file in "wwwroot/swagger/swagger.yaml" and use
the static file hosting middleware. You just need the SwaggerUI dependency, nothing more.
After that, this works.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/swagger.yaml", "My API V1");
});
Unfortunately, I encountered the issue that the .NET core doesn't host static YAML files by default. You can solve this by either configuring FileExtensionContentTypeProvider or enabling serve unknown content types.
Hope that this helps.

Related

NestJS codegen from Swagger API definition

Does anybody know of a way to automatically generate NestJS typescript code from Swagger?
Currently, there are no packages that build a Nest server based on a swagger.json file. Would be a cool project though
Just found a project that claims it can generate Nest.js code from a Swagger / OpenAPI spec file:
         swagger-nestjs-codegen on Github and on npmjs.com
Did not test it yet, though.

How do I use swagger-ui?

I'm using the Swashbuckle NuGet package in my web api project and I was trying to replace the default Swagger UI with this one
https://github.com/jensoleg/swagger-ui
but I keep getting this error message
<?xml version="1.0" encoding="ISO-8859-1"?>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Embedded resource not found - ReportsAPI.swagger-ui.Index.html</ExceptionMessage>
<ExceptionType>Swashbuckle.SwaggerUi.AssetNotFound</ExceptionType>
<StackTrace> at Swashbuckle.SwaggerUi.EmbeddedAssetProvider.GetEmbeddedResourceStreamFor(EmbeddedAssetDescriptor resourceDescriptor, String rootUrl) at Swashbuckle.SwaggerUi.EmbeddedAssetProvider.GetAsset(String rootUrl, String path) at Swashbuckle.Application.SwaggerUiHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)</StackTrace>
</Error>
I followed the advice from this post
Replace Swashbuckle UI completely
But was unable to get it to work
I have a basic ASP.Net Web API 2 (simple rest api with no website or index html)
So I downloaded the zip file from the GitHub page of swagger-ui and copied the dist folder to my project and included it as (tried both Content and Embedded resource) - and renamed dist folder to swagger-ui
So my root project folder now looks like
Then I changed my Swagger config to add the following
c.CustomAsset("index", thisAssembly, "ReportsAPI.swagger-ui.Index.html");
Is that correct? I'm not sure where to go from here
Do I have to include all other files as Embedded resource? Or just the Index.html? What do I mark the other files as, Content? Copy always?
Here is an alternative that does not require you to embed anything into your project:
https://raw.githack.com/jensoleg/swagger-ui/master/dist/index.html?url=https://swagger-net-test.azurewebsites.net/swagger/docs/V1
As you can see my swagger spec is provided with in the url parameter.
You could use that raw.githack.com or just copy the dist folder to a more convenient location.
Now I would like to point out that is a very old version of swagger-ui and does not look to be well maintained, maybe you should take a look at: ReDoc https://github.com/Rebilly/ReDoc/blob/master/README.md

Unable to inject the custom JavaScript in .Net Core 2.2 in swagger

I have written a JavaScript which I would like to inject in swagger with .Net Core 2.2. I would like to highlight that wwwroot folder wasn't created by default when I created web project (empty project option) with .Net Core 2.2. I then created wwwroot folder manually and added my JavaScript file inside that. When I am launching swagger, I am getting an error that file couldn't be loaded. I tried to create a folder "swagger" inside wwwroot and place a file there but that too didn't work.
Here is my sample code:
SwaggerUIOptions uiConfig;
uiConfig.InjectJavascript("mycustomfile.js");
Also I would like to highlight what I am trying to achieve here. I got a drop-down in my header for a database provided i.e. Oracle/MSSQL etc. Now based on the selection, I want to change the value of another header i.e. connection string. I have written a JavaScript for this purpose but let me know if that can be achieved in some other and simplistic way as well with .Net Core along withSwashbuckle.
The issue got resolved after using app.UseStaticFiles(); in Configure() method.

ASPNetBoilerPlate and Swagger UI All API Calls Return 400 Error

I have created a new application using the ASPNetBoilerplate MVC 5x template. I have integrated Swagger UI according to the link:
https://aspnetboilerplate.com/Pages/Documents/Swagger-UI-Integration
Again using the MVC 5x version
When I run the app and use http://localhost:/swagger to access the API and try to run any of the GetAll methods, I get a 400 error.
I have modified the .cs file and created the .js files by cutting the code directly from the documentation and made the required changes.
What am I missing?
Thanks
I hope you are not navigating to http://localhost:/swagger because there's a typo error. Correct url is http://localhost/swagger
There's no official support for bearer token for Swagger UI Api calls. But you can checkout the link below. A developer achieves what you want
https://github.com/aspnetboilerplate/module-zero-core-template/pull/87

Integrate the swagger ui into hammock

I am building up a CDI/REST Environment as basis for several projects by using hammock. What I would like to have besides CDI and REST is also json schema for generating payload classes and an automatically generated REST API documentation via swagger ui.
I am now at the point where everything works (Weld3, Resteasy, Undertow, Swagger Core, Json Schema). The only thing missing is the integration of swagger UI into my hammock stack.
In another project I already worked with swagger UI. As far as I know it is based on HTML + JS with an entry point index.hml. How do I integrate this into my hammock stack. How to tell the undertow that there is a index.html and where to find it ?
I think my question is not only related to swagger, but to the idea to have the hammock stack with additional static html content.
John Ament has added a swagger module for Swagger 2.0-rc3 to Hammock 2.1-SNAPHOT (will be released as part of Hammock 2.1):
https://github.com/hammock-project/hammock/tree/master/swagger
As for hosting Swagger UI inside a Hammock app, you can add a few files from swagger-ui/dist/* to Hammock's static resources path:
https://github.com/hammock-project/hammock/wiki/Native-Filters#static-resources

Resources