|
| 1 | +These days type systems are all the rage in APIs, and with gRPC and GraphQL fans touting their baked in systems. REST fans have a few options for type systems, but JSON Schema and OpenAPI seem to be powering forwards as the primary candidates for not only documenting APIs but a whole lot more. |
| 2 | + |
| 3 | +JSON Schema is metadata for JSON, which can be used for a whole bunch of things. We’ve written about how JSON Schema can really simplify contract testing, how JSON Schema can add non-invasive hypermedia controls to existing APIs, and - seeing as JSON Schema is mostly compatible with OpenAPI - it can be used to generate meaningful and beautiful human-readable documentation too! |
| 4 | + |
| 5 | +We’re going to look at how you can design the initial specs with beautiful editors (or generate from other sources if they already exist), then use those specs for a myriad of amazing things, from human docs to SDK generation, automated Postman Collection syncing, to alerting folks about changes, and a lot more. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +Introduce JSON Schema |
| 10 | +Introduce OpenAPI |
| 11 | + |
| 12 | +img service model all openapi |
| 13 | +img service model openapi + json |
| 14 | + |
| 15 | +example of endpoints described |
| 16 | +example of a schema |
| 17 | + |
| 18 | + API Specifications are not just about docs |
| 19 | + - show redoc |
| 20 | + - mocking |
| 21 | + - sdk generation |
| 22 | + - api console |
| 23 | + - client-side validation |
| 24 | + - server-side validation |
| 25 | + - contract testing |
| 26 | + |
| 27 | +hard part: one single source of truth |
| 28 | + dont wanna have to maintain your contract in multiple places |
| 29 | + dont want things getting out of sync |
| 30 | + |
| 31 | +hard enough getting devs to "write docs", making them maintain an api console _and_ validation rules... no |
| 32 | + |
| 33 | +carrots instead of sticks |
| 34 | + |
| 35 | + - devs hear "you should write docs because ultuism:" |
| 36 | + - then devs struggle to keep docs up to date |
| 37 | + - offer enough useful functionality that devs _want_ to write specs |
| 38 | + - make all of these features so easy it enables devs instead of slows them down |
| 39 | + - bake contract testing into the pipeline so they're keeping their code in line with specs, not specs in line with code |
| 40 | + |
| 41 | +Show the workflow |
| 42 | + |
| 43 | +what does design first mean |
| 44 | + - write api specs before you write any code |
| 45 | + - get feedback from clients early |
| 46 | + - change things until you're happy |
| 47 | + - implement feedback without rewriting code |
| 48 | + - bonus: code can be generated from api specs once you've locked it down |
| 49 | + |
| 50 | +why annotations generally suck |
| 51 | + - annotations are shitty for most dynamic langs |
| 52 | + - comments can get out of sync, so you still need to contract test |
| 53 | + - you need more than just data type, you can provide validation and all sorts of maxLength etc, why write all that in comments |
| 54 | + |
| 55 | +ok but no time machine |
| 56 | +you probably already have APIs, so you want to get them on this workflow |
| 57 | + - importing from various sources |
| 58 | + - maybe pull the specs from code |
| 59 | + - can still use "design first" for |
| 60 | + |
| 61 | +workflow |
| 62 | + |
| 63 | +## Development |
| 64 | + |
| 65 | +workflow waaa |
| 66 | + |
| 67 | +OpenAPI GUIs |
| 68 | + - stoplight |
| 69 | + - [Rapido](https://rapidodesigner.com/) |
| 70 | + |
| 71 | +OpenAPI IDE/Editor Plugins |
| 72 | + |
| 73 | + Atom |
| 74 | + VS Code |
| 75 | + Eclipse |
| 76 | + JetBrains suite |
| 77 | + |
| 78 | +speccy linting to give you feedback |
| 79 | + speccy screenshot |
| 80 | + |
| 81 | +## Continous Integration |
| 82 | + |
| 83 | +speccy liniting to enforce advice at CI build pass/fail |
| 84 | + |
| 85 | +Contract Testing |
| 86 | + |
| 87 | +```Feature: User API |
| 88 | +
|
| 89 | +Scenario: Show action |
| 90 | + When I visit "/users/1" |
| 91 | + Then the JSON response at "first_name" should be "Steve" |
| 92 | + And the JSON response at "last_name" should be "Richert" |
| 93 | + And the JSON response should have "username" |
| 94 | + And the JSON response at "username" should be a string |
| 95 | + And the JSON response should have "created_at" |
| 96 | + And the JSON response at "created_at" should be a string |
| 97 | + And the JSON response should have "updated_at" |
| 98 | + And the JSON response at "updated_at" should be a string |
| 99 | + .... ugh it goes on and on ... |
| 100 | +``` |
| 101 | + |
| 102 | +⛔️ Contract duplication ⛔️ |
| 103 | + |
| 104 | +You _could_ validate the data against a schema using an OpenAPI data validator |
| 105 | + |
| 106 | +But they dont really exist * |
| 107 | + |
| 108 | +json schema for contract testing |
| 109 | + lots of json schema validators |
| 110 | + |
| 111 | +``` |
| 112 | + responses: |
| 113 | + 200: |
| 114 | + description: OK |
| 115 | + content: |
| 116 | + application/json: |
| 117 | + schema: |
| 118 | + # This file is JSON Schema... buuut.. |
| 119 | + $ref: ./components/schemas/user.json |
| 120 | +``` |
| 121 | + |
| 122 | +ok side note disrepencies |
| 123 | + caveats.png |
| 124 | + |
| 125 | +The most frustrating: `type: ["string", "null"]` would be invalid OpenAPI, but it is valid JSON Schema. |
| 126 | + |
| 127 | +``` |
| 128 | +$ speccy lint docs/openapi.yml |
| 129 | +Specification schema is invalid. |
| 130 | +
|
| 131 | +#/paths/~1foo/post/requestBody/content/application~1json/properties/user_uuid |
| 132 | +expected Array [ 'string', 'null' ] to be a string |
| 133 | + expected Array [ 'string', 'null' ] to have type string |
| 134 | + expected 'object' to be 'string' |
| 135 | +``` |
| 136 | + |
| 137 | +You'd need to change it to: |
| 138 | + |
| 139 | +``` |
| 140 | +type: string |
| 141 | +nullable: true |
| 142 | +``` |
| 143 | + |
| 144 | +Technically valid, but if a null shows up, a JSON Schema validator will report it as a mistake |
| 145 | + |
| 146 | +``` |
| 147 | +$ speccy lint docs/openapi.yml --json-schema |
| 148 | +Specification is valid, with 0 lint errors |
| 149 | +``` |
| 150 | + |
| 151 | +Now teams can chose to use JSON Schema and OpenAPI, or _just_ OpenAPI. |
| 152 | + |
| 153 | +Doing this gets teams to build docs whilst really just being interested in the contract testing |
| 154 | + |
| 155 | + |
| 156 | +## Aggregator |
| 157 | + |
| 158 | +``` |
| 159 | +service-a: |
| 160 | + name: Service Name |
| 161 | + description: Short blurb about the thing |
| 162 | + repo: “git@github.com:wework/service-name.git” |
| 163 | + spec_dir: api/ |
| 164 | + entry_file: openapi.yml |
| 165 | +
|
| 166 | +service-b: |
| 167 | + ... |
| 168 | +``` |
| 169 | + |
| 170 | +``` |
| 171 | +$ speccy resolve api/openapi.yml --json-schema |
| 172 | +``` |
| 173 | + |
| 174 | +``` |
| 175 | +[ |
| 176 | + { |
| 177 | + "name": "Service A", |
| 178 | + "slug": "service-a", |
| 179 | + "contact": { |
| 180 | + "team": "Some Team", |
| 181 | + "email": "some-team@example.com" |
| 182 | + }, |
| 183 | + "openapi_url": "http://apis.exampkle.com/specs/service-a/openapi", |
| 184 | + "postman_url": "http://apis.exampkle.com/specs/service-a/postman-collection", |
| 185 | + "mock_server_url": "http://apis.exampkle.com/mocks/service-name/" |
| 186 | + }, |
| 187 | + { |
| 188 | + ... |
| 189 | + } |
| 190 | +] |
| 191 | +``` |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +## Further Possibilities |
| 196 | + |
| 197 | + |
| 198 | +Make JSON Schema files available via `Link` headers for clients to use! |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +Having both means you can use JSON Schema for everything its good at: |
| 203 | + |
| 204 | + JSON Schema validators |
| 205 | + JSON Schema UI |
| 206 | + JSON HyperSchema for HATEOAS in your API |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +Stoplight can do a _lot_ of this |
| 211 | + |
| 212 | +<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">We 💖 hearing about different teams API workflows and how they are built on top of specifications. Here's a great example of that from the WeWork Technology team written by <a href="https://twitter.com/philsturgeon?ref_src=twsrc%5Etfw">@philsturgeon</a>: <a href="https://t.co/3YsGglBhpQ">https://t.co/3YsGglBhpQ</a> <a href="https://t.co/k6DHju0Xvp">pic.twitter.com/k6DHju0Xvp</a></p>— Stoplight (@stoplightio) <a href="https://twitter.com/stoplightio/status/1037399476237991938?ref_src=twsrc%5Etfw">September 5, 2018</a></blockquote> |
| 213 | +<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> |
0 commit comments