Kotlin

Propagating the Spring SecurityContext to your Kotlin Coroutines

Posted on by  
Riccardo Lippolis

Spring Security provides a lot of convenience to develop secure web applications. However, it relies strongly on a SecurityContext stored in a thread-local (inside the SecurityContextHolder class). If not mitigated, this causes issues in multi-threaded contexts. When using Kotlin Coroutines, there is an additional abstraction layer where you don’t really know (and don’t want to know) on which thread(s) your code will be running. Luckily, there is a relatively easy solution!

Continue reading →

Deprecation with Replace hits

Posted on by  
Christophe Hesters

When code evolves we usually deprecate old code. Sometimes we come across deprecations without any hints with what to replace it with. Kotlin has a solution for this by allowing you to specify a replace instruction.

For example, we created have an old REST client.

Continue reading →

Filter a Kotlin Map to get non-null values only

Posted on by  
Riccardo Lippolis

When dealing with Maps in Kotlin, sometimes we’re only interested in entries for which the value is not null. Although the Kotlin Standard Library contains a filterValues function that seems to be appropriate, this function does not do any type conversions, resulting in a Map which won’t contain null values, but is still a Map with values of a nullable type according to the compiler. There is a feature request for the JetBrains team to add this functionality, but for now it has not been implemented (yet?).

Continue reading →

Converting Char to Int in Kotlin

Posted on by  
Riccardo Lippolis

The Kotlin standard library contains a lot of helper functions on top of the Java standard library for our convenience. Some of those functions help us in converting between different data types. For example, the String.toInt() function converts a number formatted as String to its Int representation. But how do we accomplish the same with a Char? Spoiler alert: NOT by using Char.toInt(), but by using Char.digitToInt()!

Continue reading →

Kotlin method reference to companion object function

Posted on by  
Riccardo Lippolis

Functions defined in Kotlin companion objects are often seen as the 'equivalent' of static methods in Java. Although there are some similarities, there are also some caveats you should be aware of. For example, how to use method references (or, to be pedantic: function references) to refer to functions defined in a companion object.

Continue reading →

Publish your backend API typings as an NPM package

Posted on by  
Christophe Hesters
In this post I suggest a way to publish your backend API typings as an NPM package. Frontend projects using can then depend on these typings to gain compile time type safety and code completion when using Typescript.

It’s quite common in a microservice style architecture to provide a type-safe client library that other services can use to communicate with your service. This can be package with a Retrofit client published to nexus by the maintainer of the service. Some projects might also generate that code from a OpenAPI spec or a gRPC proto file.

However, when we expose some of these APIs to the frontend, we lose the types. In this post I suggest a way to publish your backend API types as an NPM package. The frontend can then depend on these typings. Using typescript you now have compile time type safety and code completion. To see a sneak peak, scroll to the end :).

Continue reading →

shadow-left