Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Check the Docs Against the Server

In this lesson, you ran every example in your reference, docs/api.md, against Niamh’s changed server, listed where the two now disagree, and sorted each of her changes into additive or breaking.

Key Points

  • Passing tests don’t prove the docs. Niamh changed the server and updated the tests to match, and all seven pass. However, those tests check what Niamh expects, not what the reference tells Joon-ho.
  • Check the reference the way a client would. Go through docs/api.md from the top. For each example, run its curl command exactly as written, and compare what comes back with what the reference shows: the status line, the headers it lists, and the body. A match gets a tick. A difference goes on a list.
  • GET /shows and GET /shows/6 still answer 200 OK, but the body doesn’t match. Where the reference has host, the server sends presenter, with the same name. The server also sends genre, a field the reference never mentions. The unknown id, 99, still gets its 404, as documented.
  • The reference’s own POST no longer works. Saturday Folk should get 201 Created. It gets 422, because host is an unrecognized key and presenter is missing. The example without minutes still gets 422, but its details now name host and presenter too.
  • PATCH /shows/3 answers 204 No Content, with no body, where the reference promises 200 and the whole changed show. The change did happen, though: a GET on show 3 has the new start time. Only the answer changed.
  • The other examples all match: both PATCH requests to show 99, both DELETE requests and the four error examples. Those need the last lesson’s setup: big.json for the 413, and for the 500 a damaged shows.json that you restore from the backup straight after (see the commands below).
  • An additive change leaves everything a client already does working. Joon-ho’s page never sends genre, and it doesn’t have to. A POST with presenter and no genre still gets 201 Created, because the field is optional. And their page reads only the fields it uses, so an extra one changes nothing. A client that rejected unknown fields would break, so the verdict is for Joon-ho’s page.
  • A breaking change stops something a client already does. After the rename, their POST gets 422, and reading a show’s host gives undefined instead of a name. After the PATCH change, the show is still saved, but reading its empty answer with res.json() throws SyntaxError: Unexpected end of JSON input.
  • The rule: a change is breaking when a request a client already sends, or the code that reads its answer, stops working. When everything that worked still works, it’s additive.
  • What each change needs. An additive change only needs the reference updated: genre goes into “The Show Object” and into the POST and PATCH bodies’ fields, as optional. A breaking change means Joon-ho’s page has to change too, so they need to hear first, or Niamh keeps the old behavior.
  • Public APIs plan for breaking changes. They announce breaking changes in a changelog, and can serve them as a new version, such as /v2/shows, while the existing paths, such as /shows, keep the old answers for current clients.

Where the Reference and the Server Disagree

Niamh’s change Where the examples showed it Additive or breaking
host renamed to presenter, in answers and in request bodies GET /shows and GET /shows/6 send presenter, the POST /shows example gets 422 instead of 201, and the example without minutes gets 422 with different details Breaking: the reference’s POST gets 422, and a client reading host gets undefined
A new field, genre, in every show GET /shows and GET /shows/6 Additive for Joon-ho’s page: a client never has to send it, because it’s optional (a POST without it gets 201), and a page that reads only the fields it uses sees nothing change
PATCH /shows/:id answers 204 No Content with no body PATCH /shows/3 gets 204 instead of 200 and the show Breaking: the show is saved, but res.json() on the empty answer throws SyntaxError: Unexpected end of JSON input

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your reference for Niamh’s schedule API matches the server you ran it against. Joon-ho is building the station’s website from it. This week, Niamh changed the server.

00:10 She updated the tests to match, and all seven pass. However, those tests check what Niamh expects, not what the reference tells Joon-ho. So is the reference still true?

00:22 Find out the way their page would, by sending its requests.

00:27 Niamh’s four changed files, tests included, are in the text below this video. Put them in your project, back up shows.json, and start the server. Then go through the reference from the top.

00:40 For each example, run its curl command exactly as written. Compare what comes back with what the reference shows: the status line, the headers it lists, and the body.

00:51 A match gets a tick. A difference goes on a list.

00:57 The first example lists every show. It still answers 200 OK, but the shows inside look different. Show 6 is easier to read. Its status line and Content-Type match.

01:11 The body doesn’t. Where the reference has host, the server sends presenter, with the same name.

01:18 It also sends genre, which the reference never mentions. That’s two lines on the list. The unknown id, 99, still gets its 404, as documented.

01:31 Next is the reference’s POST, Saturday Folk. It should get 201 Created. It gets 422.

01:40 The details say why: host is an unrecognized key, and presenter is missing. So the reference’s own request no longer works. In the example without minutes, the 422’s details now name host and presenter too.

01:57 The PATCH example moves Late Jazz to 22:30. The reference promises 200 and the whole changed show. The server answers 204 No Content, with no body. The change did happen, though.

02:12 A GET on show 3 has the new start time. Only the answer changed.

02:19 The four error examples need the last lesson’s setup: big.json, and a damaged shows.json you restore straight after. The rest all match: both PATCH requests to show 99, both DELETE requests and those four.

02:36 So the list has three changes: the rename, genre, and PATCH’s empty answer.

02:43 Now sort each change by what it does to a client that already works. Joon-ho’s page sends the reference’s bodies, reads each show’s host, and reads every PATCH answer as JSON.

02:56 Start with genre. Your turn: pause the video and predict whether the new genre field breaks anything their page does.

03:09 It doesn’t. Their page never sends genre, and it doesn’t have to. A POST with presenter and no genre still gets 201, because genre is optional. It reads only the fields it uses, so extra ones change nothing.

03:25 A client that rejected unknown fields would break. For Joon-ho’s page, it’s additive.

03:33 The rename is different. Their POST gets 422, and when their page reads a show’s host, it gets undefined instead of a name. The PATCH change still saves the show.

03:44 However, reading its empty answer as JSON throws a SyntaxError, because there’s no JSON to read. Those two are breaking changes. So a change is breaking when a request a client already sends, or the code that reads its answer, stops working.

04:01 When everything that worked still works, it’s additive. An additive change only needs the reference updated: genre goes into the show object and both request bodies, as optional.

04:14 A breaking change means Joon-ho’s page has to change too, so they need to hear first, or Niamh keeps the old behavior. Public APIs announce breaking changes in a changelog, and can serve them as a new version, such as v2, while the old paths keep working.

04:31 That’s all you need about versions for now. You found all three by rerunning every curl by hand. In the next lesson, Run the Docs’ Examples as Tests, you’ll turn an example into a test that checks it for you.

Become a Member to join the conversation.