Using Swagger UI For Local Development

When working on the swagger documentation, several different tools can be used.

The ‘official’ editor developed by the Swagger community is Swagger Editor, live preview: https://editor.swagger.io/, I have found it quite quick and easy to use, but for larger project it gets cumbersome. Additionally it doesn’t support ability to have multiple files that will reference each other.

This post is a quick demonstration of the workflow I’m using when working on the large, multi-file swagger documentations. It’s certainly not ideal but it helps me to get the job done.

Here it is:

Edit swagger.yaml

This file can be edited in any editor of choice, the yaml format is widely supported. The documentation can be split up into multiple files that reference each other. I use this functionality to extract examples and other JSON documents. Referencing other files is explained quite well in this post How to split a Swagger spec into smaller files

Publish swagger.yaml locally

In order to view swagger.yaml inside Swagger UI the file can be either referenced by a regular file system reference, or it can be fetched via HTTP.

I use local HTTP server to expose swagger.yaml (with all other, referenced files)

Here I’m using http-server from npm but you can use any HTTP server, make sure to disable caching and CORS

Run Swagger UI

The last component, Swagger UI can be started like this:

After viewing http://localhost:1111 Swagger UI will display the generated documentation

Now, every time you make a change swagger.yaml or any file it’s enough to refresh web page to see the changes (or validation errors if there are some)

Summary

This method is more involved that alternatives like Swagger Editor or simply running Swagger UI with file referenced by regular file system reference (possibly outside Docker), but overcomes 2 major limitations of those tools, with it you’ll be able to:

  • Not pollute your environment with different tools when Docker container will work
  • Ability to edit swagger documentation that consist of multiple files (either JSON or YAML)
  • Quick feedback loop, simply refresh web page to see the changes in swagger docs

Scala Patterns To Avoid: Implicit Arguments With Default Values

There is a tendency for the Scala projects to prefer more explicit programming style. The biggest aspect of that is in my opinion the type system of the Scala language, programmers often start writing their functions by defining types of the arguments and type of the result, only to write the body of the function as last step. That’s also because we have the Scala compiler to help us.

I recently stumbled on a snippet that contradicts this rule and can be a source of hard to spot bugs for the person unfamiliar with the code.

The problem happens when you try to mix both features of the Scala language that make sense if used on their own (although I’ll argue with that a little in a moment), but when used together they create a very serious problem.

The features (as you probably guessed) are:

  • Implicit arguments
  • Default function arguments values

Implicit arguments

Implicit arguments when used in separation make a lot of sense, they allow for context-like arguments to be passed through the whole call stack. Furthermore they are the major building block for more advanced features like typeclasses.
Many major libraries or projects would not exist without it, but because this is a very powerful feature, it’s use should be limited.

Default function arguments

In my option this feature should be avoided almost everywhere – they make it very hard to use functions as arguments and pass function around.
It’s better to use either currying (multiple parameter lists) or just define function few times taking different set of parameters in each case.

I think it makes sense to use default arguments in 2 cases:

  • Backwards compatibility – you have an existing case class and want do add another attribute to it without changing your code or underlying database structures
  • Complicated, builder-like constructors – you have constructor that takes a ton of arguments, and users would like to configure only a limited set each time, using defaults for everything else

The problematic code

Here’s a snippet of the code I stumbled on:

Here’s what I think is wrong here:

  • Caller is not aware that updateCampaign actually takes any argument unless he/she reads the source or looks up documentation (if there’s any).
    You can perfectly well write code that looks like this:

And you will not see anything wrong – the compiler won’t complain. Unless the caller is aware there’s another parameter expected, there’s no straightforward way to learn it.

  • If someone overrides updateCampaign function this information will be lost forever, all calls to the overridden version will use the default argument.

The above snippet contradicts the explicitness rule, also Scala compiler will not help you spotting a bug.

Of course the code will still run – the only problem is that every update to the campaign will be attributed to User.Default which is not what we are expecting – but there’s no way to express that intend when using default arguments.

Fixing the code

To fix it, simply remove the default value for the user argument:

After that change you will get the compilation error forcing you to fix the issue.

Scala compiler is now able to detect this issue right away, you don’t have to remember which functions take which arguments because you can leverage the compiler.

Summary

The outlined scenario is one of many examples where a programmer can use Scala compiler to his/her benefit. A simple change to the code will result in a compiler pointing out the problem right away, forcing you to address it before code is released.

It’s also one of the many logic-related mistakes that are very hard to spot in testing – the “audit logging” in this case is a side effect to the regular responsibility of the code.

JSON in Play Framework – Advanced Libraries

This is a followup post to my previous one covering JSON in Play framework. I’d like to show how the manual work I did before in trying to make JSON mapping compatible with external API can be done by using 2 small but useful libraries:

play-json-naming

This is a very simple library that can be used to convert from camelCase formatting (the default one that we use in Scala) to snake_case formatting that is common in various different languages (for example PHP or Ruby). There is a vast amount of APIs that follow that convention, examples include GitHub or Twitter APIs.

This is how you can use it:

This is enough to provide a mapping from a case class like this:

to a JSON in the following format:

As you can see holeCards was replaced with hole_cards. That’s pretty much all that library does.

This is the project page: https://github.com/tototoshi/play-json-naming. Over there you can find information how to include it in your project.

This is the commit I did to introduce the change in my own project, as you can see I have removed a bunch of type-unsafe code, and replaced it with 2 method calls.

play-json extensions

This library is definitely more comprehensive than previous one, it consists of few (de)serializers which can be used in your project. It’s main feature is case class serializer that can handle more than 22 fields in one class (common limitation in Scala), but I’ll show one other feature I had to use to make my API compatible with external service.

Unfortunately it’s documentation isn’t really comprehensive and there are few mistakes (probably related to recent migration to new package name).

I’d like to show you one particular use case in which I was able to use it, namely: “De-/Serialize single value classes”

The problem is that in a setup like this:

The resulting JSON for class Card will look like this:

And our expected format is as follows (scroll up to see complete example):

In the previous post I have shown how this can be done by writing manual mapping from case class to JsValue (JsString in this case).

The same result can be achieved with the help of play-json-extensions in the following way:

One note though: There is a problem in the library itself and I had to make to my domain objects definitions, the Suit was a sealed abstract class, now it has to be just sealed class. In my opinion this is a small workaround that is acceptable in my scenario, and hopefully the library will be fixed/extended to allow for this case.

Take a look at the commit I did to introduce the change, as you can see I have removed a bunch of lines which were responsible for manual JSON mappings.

This is the library page: https://github.com/xdotai/play-json-extensions

Summary

I have show how you can use 2 more advanced Play JSON libraries to achieve your goal of making a compatible APIs. This is a more reasonable approach than writing mappings yourself and also it should be less error prone.