Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test-Driven Documentation for your REST(ful) se...

Test-Driven Documentation for your REST(ful) service

Building RESTful APIs in Java? Getting tired of the annotation bloat forced upon you by your API specification framework which haunts your RESTful service controllers and model objects? Finding yourself in the situation where your code isn’t always in synch with your specification or vice versa?
RAML and Swagger / OpenAPI are great specifications with powerful and shiny tooling, but they do also really have their downsides when it comes to messing with our code.

Fear not! There are other alternatives worth investigating. In this talk, we’ll take a look at how we recently build, designed and documented a public REST API by doing Test Driven Documentation with some help of AsciiDoc and Spring (Auto) REST Docs.

Jeroen Reijn

April 12, 2018
Tweet

More Decks by Jeroen Reijn

Other Decks in Programming

Transcript

  1. JEROEN REIJN Software Architect at Luminis /Amsterdam • Focus on

    productivity and Developer Experience • Been building APIs for the Web since 2010 • Writes most of his code in Java • Tweets @jreijn
  2. Streaming 1% RPC 10% REST 89% REST RPC Streaming GraphQL

    source: https://www.programmableweb.com/
  3. Hypertext link specifications { "_links" : { "self" : {

    "href" : "http://localhost:8080/" }, "planets" : { "href" : "http://localhost:8080/planets" }, "people" : { "href" : "http://localhost:8080/people" } } }
  4. RAML • RESTful API Modelling Language (RAML) • Based on

    YAML file format • Powerful designer api with auto completion • Code generation for Java • No native support for HATEOAS
  5. API Blueprint / Apiary • Markdown flavoured syntax • Powerful

    designer UI • Documentation while designing • Java code generator support is *very* minimal
  6. Postman • Postman client is great for testing APIs •

    Supports designing & documenting APIs • Has support for Api Mocks • Enables fast prototyping of APIs • Tools for different phases in the API lifecycle
  7. Open API • Widely adopted community-driven specification for REST APIs

    • YAML and JSON based format • Programming language-agnostic interface • Allows both code first and contract first approaches to API design • Evolving specification (version 3.0 released in the summer of 2017)
  8. Swagger • Tools around the OpenAPI specification • Swagger editor

    • Swagger-UI • Swagger Codegen (server and client libs and many languages) • Swagger Inspector
  9. Swagger Codegen /** * Unique identifier representing a specific planet.

    * @return id **/ @ApiModelProperty(value = "Unique identifier representing a specific planet.") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Planet name(String name) { this.name = name; return this; }
  10. Retrospect • API Design tools and platform are getting really

    powerful • OpenAPI seems to be the most widely adopted api spec • Generating code from your API specification is up to you
  11. Docs based on code @ApiOperation(value = "", nickname = “getfilmbyid",

    notes = "Get a specific film by id.", response = Film.class, authorizations = { @Authorization(value = "apikeyQuery") }, tags = {}) @ApiResponses(value = { @ApiResponse(code = 200, message = "A film.", response = Film.class)}) @GetMapping(value = “/films/{id}") ResponseEntity<Film> getFilmById(@ApiParam(value = "Id of the film.", required = true) @PathVariable("id") String id);
  12. Swagger drawbacks • Hypermedia support not ‘final’ • Documentation is

    done through several annotations which are added in your implementation classes • API Implementation code overtime, gets overwhelmed with annotations • Documentation is URI centric • Documentation and API could become out-of-sync overtime
  13. Spring REST Docs “Document RESTful services by combining 
 hand-written

    documentation with 
 auto-generated snippets 
 produced with Spring MVC Test.”
  14. Spring REST Docs • Documentation is built from tests •

    Supports hypermedia (HATEOAS) APIs • Immediate reaction to API changes • Keeps your code clean • Produces AsciiDoc snippets
  15. Basic Spring MVC Test @Test public void testGetAllPlanets() throws Exception

    { mockMvc.perform(get("/planets").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()",is(2))); }
  16. With Spring REST Doc @Test public void testGetAllPlanets() throws Exception

    { mockMvc.perform(get("/planets").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.length()",is(2))) .andDo(document("planets/get")); }
  17. AsciiDoc snippets |=== |Path|Type|Description |`id` |`Number` |The id of the

    planet |`name` |`String` |The name of the planet |`diameter` |`Number` |The diameter of the planet |===
  18. AsciiDoc Plain-text writing format. AsciiDoc documents can be translated into

    various formats, including HTML, DocBook, PDF and ePub. = Hello, DevCon! Jeroen Reijn <[email protected]> An introduction to documenting Hypermedia APIs with https://projects.spring.io/spring-restdocs/[Spring REST Docs]. == First Section * item 1 * item 2
  19. Spring Auto REST Docs • Extension for Spring REST Docs

    • Response field documentation generated by means of introspection • Partial support for documenting Hypermedia ‘links’ out of the box • DRY - Uses JavaDoc for generating documentation
  20. Spring REST Docs advantages • Ability to focus on domain

    model rather than URI’s • ‘Guarantees’ that API documentation and implementation code are in-sync • Forces you to update documentation for any implementation changes • Ability to document with different payloads and explain different test scenarios • Ability to document remote APIs (with WebTestClient)
  21. In retrospect • Documenting Hypermedia APIs is getting easier •

    Validating APIs against their documentation is still ‘hard’ • Spring REST Docs can help documenting and validating the API • Good documentation is key to working with an API
  22. Those who stand for nothing, fall for anything - Alexander

    Hamilton ? Thank you Questions? is powered by Don’t forget to vote! Sources available @ https://github.com/jreijn/devcon-rest-tdd-demo