Showing posts with label Swagger. Show all posts
Showing posts with label Swagger. Show all posts

Wednesday, February 27, 2019

Swagger UI : Can't read from file https:///swagger/docs/v1

I recently noticed that web api swagger UI contains Error notification on footer 
SchemaValidation Message
once I click on it and  it redirects to other page and contains below message


{"schemaValidationMessages":[{"level":"error","message":"Can't read from file https:///swagger/docs/v1"}]} 
Swagger UI use online validator to web api swagger document. If API URL is not accessible in public, then
this schema validation message is occurred.

There are two ways to rectify this schema validation error. 
·         Disable Swagger Validation in SwaggerConfig.cs  

GlobalConfiguration.Configuration.EnableSwagger().EnableSwaggerUi(c => { c.DisableValidator(); }); 

·         API URL should be public available. 

Thanks for visiting!!

Wednesday, February 6, 2019

Export swagger api document to pdf in Swagger UI

In this blog, we will discuss how to generate or export Swagger API documentation into PDF file. Recently I faced this challenge, when we needed to share API documentation information with external client and due to security and other reason we can’t expose production Swagger API URL to client and then we decided to go with PDF document which should have API information like API URL, input parameter, output model and HTTP verbs – GET/PUT/POST.

This blog will demonstrates steps to generate PDF document from Swagger UI/JSON data by using third party component (swagger-spec-to-pdf)

Here is swagger API URL


 
Swagger API 

In above image, you can see swagger document URL:  http://localhost:27860/swagger/docs/v1



  •  Install swagger-spec-to-pdf package by using npm
           Syntax:  
           npm install -g swagger-spec-to-pdf

  • Open the Command Prompt  and run the below command (swagger2pdf ) to generate PDF


  •  Generates api.pdf file


Generated api.pdf document:


Other swagger related posts

Thursday, January 31, 2019

Swagger UI Disable Swagger document

In this blog, we will discuss how to disable the swagger so that user can only view the API document and not able to submit any request through Swagger UI.



Here is swagger document for Order API:




You can see, here Try it Out! Button is available and user can submit the request and get Order Information by order number.

If you are API in Production or any other sensitive environment and don’t want to user to allow to submit any kind of request [GET/POST/Put] but still you want to publish the API documentation.

There is a SupportedSubmitMethods(new string[] {}) method in swagger configuration class, that will be allowed to disabled “Try it Out” button.

C#:

public class SwaggerConfig

    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {                      
                        c.SingleApiVersion("v1", "Order API Documentation");                     
                     
                    })
                .EnableSwaggerUi(c =>
                    {                      
                       c.SupportedSubmitMethods(new string[] {  });                      
                    });
        }
    }


after change, there is no ‘Try It Out’ button available but rest of API information is still available.


Other option is also available example you want hide ‘Try It Out” button for all HTTP verbs like  POST, HEAD, PUT but it should be available  for GET only

  c.SupportedSubmitMethods(new string[] { "Get" });

Other swagger related posts

Friday, March 16, 2018

Import Swagger APIs documentation into Postman

Swagger lovers can easily import APIs documentation information into Postman and Postman provides ability to import API information from Swagger directly.


below are steps :
  • Create New Collection : 
   
  •     Click on Import button on top panel , Import Popup will be opened



                                                                     (Import Window)



now you can see list of imported API requests


Tuesday, March 6, 2018

How to add Authorization Header (Custom Header) in Swagger UI

In this blog, we are going to discuss how to add custom header parameter (authorization header) in Swagger UI for ASP.Net web API.

We use request header (like authentication) to pass client information to web API but in Swagger UI, there is no any simple or straight way to add custom header parameter.
To add customer header parameter in Swagger UI, we need to implement IOperationFilter interface

internal class AuthorizationHeaderParameterOperationFilter : IOperationFilter
    {      
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (operation.parameters == null)
                operation.parameters = new List<Parameter>();

     operation.parameters.Add(new Parameter
            {
                name = "X-API-Token",
                @in = "header",
                description = "access token",
                required = true,
                type = "string"
            });
        }
    }

SwaggerConfig .cs :

public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;
            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1""DemoWebAPI");
                    c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
                })
                .EnableSwaggerUi(c => { });
        }


As below you can see, X-API-token as additional parameter for your Web API to take authorization token and pass to web API for user authentication 

Swagger UI Add Authorization Header (Custom Header)
Swagger UI Add Authorization Header (Custom Header)



Other swagger related posts:
·         Disable Swagger UI


Thanks for visiting!!

Wednesday, February 28, 2018

How to use Swagger in Web API

In this blog, we will discuss about web api documentation and will demonstrate basic steps how to setup swagger in web api project and also show how to test web api by submitting http request GET/POST/PUT via swagger UI.

Please read my previous blog when we discussed how to create a web api in ASP.NET MVC Application and describe basic steps to create an asp.net web api.

Swagger is API developer tools for the Open API Specification (OAS), enabling development across the entire API life cycle, from design and documentation, to test and deployment.

Definition -
Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.”


To add Swagger into WEBAPI Project, you need to perform only 2 steps

1.   Install SwashBuclkle open source project via Nuget.


Install SwashBuclkle Nuget Package

2.   Configure Swagger Config File :

 After package installed, you will find a new file ‘SwaggerConfig.cs’ under App_Start Folder

    
Swagger Config

public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {             
                        c.SingleApiVersion("v1""DemoWebAPI");                 
                    })
                .EnableSwaggerUi(c =>
                    {
                        c.DocumentTitle("Demo Web API");

                    });
        }

    }
 
It is done J 

now build the web api project and run it.

Browser http://localhost:65096/swagger

Swagger UI documentation















Swagger allows you to submit HTTP request GET, HTTP POST, HTTP PUT against Web API

here is an an example 

Submit HTTP GET Request For /api/Projects/{projectId}



Swagger UI GET


Other swagger related posts:



·         Disable Swagger UI



Thanks for visiting!!

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...