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

Beyond Documentation With OpenAPI

Beyond Documentation With OpenAPI

Presented at Bulgaria Web Summit 2018

Imagine a world where the mobile development team is not constantly surprised by changing endpoints, where frontend developers don’t abuse your carefully crafted APIs and we don’t have to go back again and again to fix or change stuff. In this imaginary land we are able to leave the guesswork out by using API definitions to produce even better designs and automate parts of the process. Together we will explore OpenAPI as a standard way to describe APIs and see how it can help us get there.

Boyan Yordanov

April 14, 2018
Tweet

More Decks by Boyan Yordanov

Other Decks in Technology

Transcript

  1. - also me (a couple of months later) I can

    do so many cool things with this definition. 7
  2. COMMON PROBLEMS COMMON PROBLEMS documentation and code don't match SDKs

    have a third opinion changes aren't properly tested clients constantly wait for the API 14
  3. 16

  4. 20

  5. GAME PLAN GAME PLAN 1. produce the API specification 2.

    generate docs and mock API 3. refine the specification 4. use it in integration tests 5. work on the actual server code 21
  6. 2. ALWAYS UP TO DATE ALWAYS UP TO DATE DOCUMENTATION

    AND DOCUMENTATION AND TESTS TESTS 22 . 3
  7. 3. BACKEND AND BACKEND AND CLIENTS CLIENTS CAN BE DEVELOPED

    CAN BE DEVELOPED INDEPENDENTLY INDEPENDENTLY 22 . 4
  8. API BLUEPRINT API BLUEPRINT markdown based development is open on

    GitHub it's quite mature there's tooling for the most essential things mostly documentation oriented 24
  9. RAML - RESTFUL API MODELING RAML - RESTFUL API MODELING

    LANGUAGE LANGUAGE yaml based syntax is quite similar to the OpenAPI Specification covers the whole design/development process 25
  10. JSON SCHEMA JSON SCHEMA not entirely fair to include it

    here json based focused only on the data model great for validation and tests includes syntax for describing hypermedia controls 26
  11. 27

  12. HOW TO WRITE AN HOW TO WRITE AN OPENAPI OPENAPI

    SPECIFICATION SPECIFICATION 29
  13. BASIC STRUCTURE BASIC STRUCTURE openapi: “3.0.0” info: # ... servers:

    # ... paths: # ... tags: # ... components: # ... 30
  14. BASIC INFORMATION ABOUT THE API BASIC INFORMATION ABOUT THE API

    info: description: >- *Imaginary* API for managing meetups. Imagined for this **BWS 2018** talk. version: '0.1' title: Meetups Are Awesome API contact: email: [email protected] name: Boyan Yordanov url: 'https://boyanyordanov.com' license: # ... 32
  15. HOW CAN USERS ACCESS THE API HOW CAN USERS ACCESS

    THE API servers: - url: 'https://imaginary-meetups.com/api' description: Imaginary API for managing Meetups - url: 'https://staging.imaginary-meetups.com/api' description: Staging server for the imaginary API - url: 'http://localhost:8080' description: Development version of the API 33
  16. it also supports templating servers: - url: https://{username}.api-server.com/api/ description: User

    specific URLs variables: username: default: demo description: assigned upon registration 34
  17. Just a list of paths and the possible requests and

    responses paths: /meetups # ... /meetups/{id} # ... /meetups/{id}/events # ... 35 . 2
  18. 37

  19. DESCRIBING PARAMETERS DESCRIBING PARAMETERS /meetups: # ... parameters: - name:

    city in: query description: Filter meetups based on city schema: type: string 38
  20. SCHEMA OBJECTS SCHEMA OBJECTS extended subset of JSON Schema draft

    5 support references in future might support current drafts of JSON Schema or other formats 40 . 1
  21. example schema schema: type: object properties: id: type: string format:

    uuid name: type: string starting-date: type: string format: date 40 . 2
  22. Reference object object with $ref property reference objects in the

    same document reference external documents replace inline definitions for most OpenAPI components 40 . 4
  23. Replace almost anything in the spec schema: $ref: '#/components/schemas/Meetup' #

    ... responses: '200': $ref: 'MeetupsListResponse.yaml' 40 . 5
  24. COMPONENTS COMPONENTS components: schemas: # ... responses: # ... parameters:

    # ... requestBodies: # ... headers: # ... examples: # 41
  25. We have an We have an OpenAPI Speci cation OpenAPI

    Speci cation What do we do with it? What do we do with it? 42
  26. LINTER - SPECCY LINTER - SPECCY https://github.com/wework/speccy lints your specification

    supports rulesets docs preview with ReDoc resolve spec in one file 44
  27. generate server scaffolds generate clients / SDKs converting back to

    2.0 sort of works some templates might be out of date 46 . 2
  28. POSTMAN COLLECTIONS POSTMAN COLLECTIONS Apimatic.io upload/download or use API transform

    to and from OpenAPI lots of formats including Postman 2.0 Collections 48 . 1
  29. OPENAPI / JSON SCHEMA OPENAPI / JSON SCHEMA CONVERSION CONVERSION

    https://github.com/mikunn/openapi2schema https://github.com/wework/json-schema-to- openapi-schema 49
  30. EXAMPLE TEST EXAMPLE TEST https://github.com/thephpleague/json-guard // ... $schemas = json_decode(

    file_get_contents('meetups-schema.json') ); $response = $client->get('/api/meetups') $validator = new League\JsonGuard\Validator( json_decode($response->response->content()), $schema); $this->assertTrue($validator->passes()); 51
  31. RESOURCES RESOURCES Specification: - issues / PRs - books/blog/slack https://swagger.io/specification

    https://github.com/OAI/OpenAPI-Specification https://apisyouwonthate.com https://philsturgeon.uk https://apihandyman.io https://apievangelist.com http://json-schema.org http://www.commitstrip.com/en/2018/02/26/its-good-to- have-experience/ 54