Sunday, 23 December 2018

ASP.NET Core 2.2 introduces API Analyzers for improved WEB API documentation





One of the new features of ASP.NET Core 2.2 is, API Controller conventions for better documentation experience. ASP.NET Core 2.1 introduced the ApiController attribute to denote a web API controller specific conventions like performing automatic model validation and automatically responds with a 400 error. The ASP.NET Core 2.2 takes one step further to provide the metadata for API Explorer and a better end-to-end API documentation experience. To achieve this, ASP.NET Core 2.2 introduces API Analyzers which help in following a set of conventions for better API documentation. In this post, let’s find out how to use API Analyzers.

ASP.NET Core 2.2 introduces API Analyzers

Before we begin, please do the following installation (ignore if already done).
Open Visual Studio 2017 preview, hit Ctrl+Shift+N and select the ASP.NET Core Web Application (.NET Core) project type from the templates. When you click Ok, you will get the following prompt. Select ASP.NET Core 2.2 and choose the API template.
What’s New with ASP.NET Core 2.2
Once the project is created, we’ll need to enable the Swagger for API documentation. Here, I’ll not explain about enabling swagger, but you can follow this post to enable Swagger in ASP.NET Core WEB API.
Next, delete the ValuesController class (default template) as it doesn’t have enough code to document. To show you how API Analyzers are helpful, we’ll need a real-world code sample (basically, a controller with CRUD operations). To do that, add a new folder “Model” at the root of the directory. Inside the Model folder, create an Employee class with the following code.
1
2
3
4
5
6
public class Employee
{
    public int Id { get; set; }
    public string EmpName { get; set; }
    public int Age{ get; set; }
}
Next, we’ll create a new controller using the scaffolding process.
  • Right-click on the Controllers folder in Solution Explorer and select Add -> Controller.
  • Select API Controller with actions, using Entity Framework and click Ok.
  • Next, in the pop-up window to set Model class “Employee” and create a new Data Context class using the + button.
This should create a new controller named EmployeeController with the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace ASPNETCoreAPIDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeesController : ControllerBase
    {
        private readonly ASPNETCoreAPIDemoContext _context;
        public EmployeesController(ASPNETCoreAPIDemoContext <span class="hiddenGrammarError" pre="">context)
        {
            _context</span> = context;
        }
        // GET: api/Employees
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Employee>>> GetEmployee()
        {
            return await _context.Employee.ToListAsync();
        }
        // GET: api/Employees/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Employee>> GetEmployee(int id)
        {
            var employee = await _context.Employee.FindAsync(id);
            if (employee == null)
            {
                return NotFound();
            }
            return employee;
        }
        // PUT: api/Employees/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutEmployee(int id, Employee employee)
        {
            if (id != employee.Id)
            {
                return BadRequest();
            }
            _context.Entry(employee).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return NoContent();
        }
        // POST: api/Employees
        [HttpPost]
        public async Task<ActionResult<Employee>> PostEmployee(Employee employee)
        {
            _context.Employee.Add(employee);
            await _context.SaveChangesAsync();
            return CreatedAtAction("GetEmployee", new { id = employee.Id }, employee);
        }
        // DELETE: api/Employees/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<Employee>> DeleteEmployee(int id)
        {
            var employee = await _context.Employee.FindAsync(id);
            if (employee == null)
            {
                return NotFound();
            }
            _context.Employee.Remove(employee);
            await _context.SaveChangesAsync();
            return employee;
        }
        private bool EmployeeExists(int id)
        {
            return _context.Employee.Any(e => e.Id == id);
        }
    }
}
To run this application successfully, we also need to run the database migration. To do that, run these following commands on the package manager console.
1
2
PM>Add-Migration Initial -OutputDir Data/Migrations
PM>Update-Database
Now run the application and visit the Swagger URL. You should see the following.
Default Swagger documentation
In the above image, I have expanded the PUT API intentionally. The Swagger documentation just returns 200, but the actual code of PUT API returns 3 different HTTP status codes (400, 204 and 404).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// PUT: api/Employees/5
[HttpPut("{id}")]
public async Task<IActionResult> PutEmployee(int id, Employee employee)
{
    if (id != employee.Id)
    {
        return BadRequest();
    }
    _context.Entry(employee).State = EntityState.Modified;
    try
    {
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!EmployeeExists(id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }
    return NoContent();
}
API analyzers can help to fix this missing piece of documentation.

Installing and using API Analyzers

By default, ASP.NET Core 2.2 ships with a set of default conventions – DefaultApiConventions – that’s based on the controller that ASP.NET Core scaffolds. If your actions follow the pattern that scaffolding produces, you should be successful using the default conventions. These analyzers work with controllers annotated with ApiController introduced in 2.1. To use API analyzers, first install the nuget package.
1
Microsoft.ASPNETCore.MVC.Api.Analyzers
Once the nuget package is installed, just build the application. Go to Error List window and you should see a couple of warnings in the Warning tab.
ASP.NET Core 2.2 introduces API Analyzers
Clicking the particular warning will take you to the actual warning in the code. You can fix the individual warning, using Ctrl and . on the green squiggly line. In case of PUT API, the API analyzers will put the following attributes on top of the method to fix all the warnings. But, fixing each warning will take a lot of time.
1
2
3
4
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType]
ASP.NET Core 2.2 introduces API Analyzers
There are 3 ways to apply a convention to a controller action:


  • To apply it globally, use the following code.
    1
    [assembly:ApiConventionType(typeof(DefaultApiConventions))]
  • Using the ApiConventionType attribute on a controller.
    1
    2
    3
    4
    5
    6
    7
    ApiConventionType(typeof(DefaultApiConventions))]
    [ApiController]
    [Route("/api/[controller]")]
    public class EmployeesController : ControllerBase
    {
        ...
    }
  • Using ApiConventionMethod. This attributes accepts both the type and the convention method.
    1
    2
    3
    4
    5
    6
    7
    // PUT: api/Pets/5
    [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Put))]
    [HttpPut("{id}")]
    public async Task<IActionResult> PutEmployee(int id, Employee employee)
    {
        ...
    }

  • The class DefaultApiConventions is a static class, which defines a set of methods as a pattern and applicable metadata is applied to the method, if the pattern is matched. Here is the conventions for PUT in DefaultApiConventions class.
    API Analyzer Default Conventions
    Now build the application and notice all the warnings are gone. Run the application, visit the swagger URL and see the PUT API documentation. You should see the following.
    Swagger documentation after using API analyzers
    Here, the swagger shows all the possible return types for the PUT API. If you don’t like the default conventions, you can also implement your custom implementation. Custom implementation will be ideal when your controller doesn’t follow the default CRUD conventions. As an example, if you have the following API in your controller. Here, the Action method name "Search" doesn’t match with the default conventions.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [HttpGet("{name}")]
    public async Task<ActionResult<Employee>> SearchEmployee(string name)
    {
        if (name.Trim() == string.Empty)
        {
            return BadRequest();
        }
        var employee = await _context.Employee.FindAsync(name);
        if (employee == null)
        {
            return NotFound();
        }
        return employee;
    }
    You can create a custom convention implementation like,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static class MyCustomConventions
    {
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesDefaultResponseType]
        [ApiConventionNameMatch(ApiConventionNameMatchBehavior.Prefix)]
        public static void Search([ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)]string name)
        { }
    }
    Ideally, you should copy the code of DefaultApiConventions class and also add your custom code in your custom convention implementation as you can’t apply two conventions at a time. You can find the source code at GitHub.

    Summary

    The APIController attribute was a good addition to ASP.NET Core 2.1 as it adds some great functionality which makes life easier for API developers. In continuation to API improvements, ASP.NET Core 2.2 comes with API analyzers. The API analyzers helps in producing the better API documentation. Along with the set of default conventions, you can also write your custom implementation of conventions suited for your requirement.
    Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

    1 comment:

    1. I will not do in originality but your blog is so good that we can not say anything else than THANK YOU MUCH!

      Voyance gratuite

      ReplyDelete

    Angular Tutorial (Update to Angular 7)

    As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...