A real life example of isomorphisms in Scala: POST request

Manuel Rodríguez
Nov 11 · 1 min read

In my last post I talked quite a lot about isomorphisms, optics and testing. However you might be thinking, why would I want to do that? Well, if you are implementing an API with a POST request chances are that it is useful for you :)

Let’s imagine that you are receiving information about a person in a POST request. Your swagger file would look like this (only significant parts depicted)

This will be translated into code as the following case class. At Twilio, this process is handled by the open source tool GuardRail

The function defined in the swagger.yaml file to be executed on the PUT request, putPerson, looks like

If you want to store that person information inside your system, chances are that the id and the rest of the information will go together in a class like

Question is, how you convert from one to another? You can of course do it manually, but a way more elegant approach is to define an isomorphism from Person + Id to PersonFull :)

Implementing Property Testing for this is quite simple by using the tools that we saw on the previous post.

As you can see, this technique is not a mere mathematical curiosity but something to be adopted in your code to do real stuff.

Manuel Rodríguez

Written by

Developer at Twilio. This is where I keep the cool stuff that I learn in my free time

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade