The Madness that is Programming – Hacker Noon

JWT Tokens with Spring Boot Didn’t Work

There are instructions out there, but the session data didn’t persist inside Spring. I always was the anonymous user according to Spring no matter how much I authenticated myself. I debugged the internals of Spring request handling for the longest time to no avail until I figured it out. Turns out it’s a bad idea to use @Autowire to the pass the Spring SecurityContext to classes. Use the SecurityContextHolder every time.

Lesson Learned: If you’re having trouble with libraries and are lucky enough to have all of the code at your disposal, look at the code. Stare and debug at it long enough and you might figure it out.

Maybe I should try a job as a car mechanic?

How do I return data from Exposed transactions?

(Exposed is an ORM for Kotlin). In the framework you wrap database operations into transactions like this:

transaction {
val dao = UserDao.find{ name eq someName }
dao.password = newPass
return toPojo(dao) // Error
}

I wanted to return something from that transaction (POJO with updated data), but the return keyword was no allowed. I had no idea what even to Google to fix this.

Lesson Learned: Kotlin Higher-Order functions return the last statement and that transaction is just a method with a lambda parameter. So the last example would be just:

transaction {
val dao = UserDao.find{ name eq someName }
dao.password = newPass
toPojo(dao)
}

For some inexplicable reason my brain just didn’t make the connection to Kotlin lambdas and I thought this was some other magic. All I really would’ve needed to do is look how the transaction function is defined and internalize it properly.

There’s no such thing as magic, only things you don’t understand. If the magic fails you stop and figure it out before using it. Frantically googling for a solution to a problem you don’t understand won’t get you anywhere.

Maybe there’s opportunity in brewing?

Angular Routing Errors in Spring Routing

In Spring you use the WebSecurityConfigurerAdapter class to declare security rules how each URI is resolved, which can bypass the session authentication etc. But if I used routing in Angular i.e. different controllers with different URIs. Spring got a hissy fit and spat out errors.

Lesson Learned: Turns out you need something like this:

This make sure all requests (that don’t match the other Spring routing rules) are forwarded to index.html, and from thereon to the correct Angular controller. Google will give you all kinds of obsolete no longer working versions of this.

Maybe a plumber?

Spring JUnit Testing Woes

I had two problems at the same time, the Spring Context loading didn’t quite work the way I wanted and autowiring fields was flaky. The context didn’t load configuration files written in YML for example, especially in integration tests. And field injection didn’t quite work with a test Context.

Lesson Learned: Turns out you can’t use YML in tests properties (look at supported formats). Also don’t inject fields if you can avoid it. Better yet, avoid Spring in tests altogether if you can get away with it.

Garbage man?

Configuration via ConfigurationProperties

Partly related to the previous one, ConfigurationProperties, while cool, were an utter pain to get working. Again the magic factor was a bit too high — quite often the defined configuration POJO just said null and I didn’t have a clue what to do. Them not working in unit tests didn’t help either.

Lesson Learned: Endless tinkering sometimes is all you can do.

How about distilling?

Multitable Queries in Exposed

Again Exposed woes. I had no clue how to write queries that touch multiple tables. Finally I asked Stack Overflow and got an answer.

Lesson Learned: DAO framework magic that generate SQL queries are a failure by design. I felt like this before I even started and here I was again. Maybe this time they’ll just work I said to my self. Nope. You have a clean and fast SQL query in your head, but you’re spending hours endlessly massaging the DAO layer hoping it would generate it correctly and not be horrendously slow. Things like Android Room are simply superior.

Wine making perhaps?

Karma refused to work with PhantomJS

Unit tests fail if I run against the PhantomJS browser engine. I would get get something like [object ErrorEvent] and the tests would randomly fail.

Lesson Learned: The JavaScript ecosystem is a mess. But I knew that already.

I wonder if you can even throw a newer flat screen through a window or will just bounce

How about organized crime?

Security Vulnerability in Hoek

I had no idea what Hoek is, but Github complained that it has a vulnerability open so I spent hours and hours to figure out how to fix it. That issue is closed but I’m not convinced that it has been fixed.

Lesson Learned: JavaScript is mess. Security problems in your dependencies are always also your problems.

Live under a bridge and live on social security?

Angular 5->6

In the middle of my development push Angular 6 was released. In my infinite wisdom I thought the upgrade should be a breeze, I have only a few hundred lines of Angular code, right? I also thought I’ll bite the bullet and not use the compatibility layer since the code base was small. I ran the auto upgrade thingy and …problems. Here’s a list:

  • I used Observable.of and Rx stream operators heavily, both were not upgraded properly. Especially stream pipe() change was painful.
  • Imports were fiddled around with. I think they’re just messing with us. They are aware that this breaks everything, right?
  • WebStorm lost the ability to run tests

Lesson learned: Use extreme caution when using anything latest and greatest. Maybe all of these were avoided if I had just waited for things to settle.

Farming?

TypeScript Compilation Error

Suddenly everything on the Angular side broke down. I got:

ERROR in src/app/auth.interceptor.ts(6,2): error TS2345: Argument of type 'typeof AuthInterceptor' is not assignable to parameter of type '({ providedIn: Type | "root"; } & ValueSansProvider)
| ({ providedIn: Type | "root"; } ...'.
Type 'typeof AuthInterceptor' is not assignable to type '{ providedIn: Type | "root"; } & ClassSansProvider'.
Type 'typeof AuthInterceptor' is not assignable to type '{ providedIn: Type | "root"; }'.
Property 'providedIn' is missing in type 'typeof AuthInterceptor'.

Google didn’t offer any help. Yes, the error was on line 6 in the auth.interceptor.ts but I didn’t see it. I rewrote the class and diffed the results to find out the error. I missed parentheses in @Injectable annotation.

Lesson Learned: I immediately saw the error after a break. Check your fundamentals when “Hello World!” level stuff doesn’t work.

A hobo perhaps?

Json List Types in Kotlin

I had troubles mapping JSON lists to Kotlin Pojos. Basically this problem. While I did figure it out, it looked so ugly and flaky I opted for plan B and just changed the JSON instead.

Lesson Learned: Workaround is always an option.

Park ranger?

Spring Integration tests with Gradle 4

I had no luck doing Spring Integration tests, all I got was:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

Everywhere I looked everything was still at Gradle 3 and of no help.

Lesson Learned: You can always open more search tabs. Endless googling got me to the correct magic:

configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom testRuntimeOnly
}

It’s still magic. I don’t even know what to read to unmagic it.

Professional golfer? That’s still an option at age 37 right?

My Rest controller returns HTML

One of my rest endpoints stubbornly returned HTML instead of JSON. I laboriously went through the annotations and couldn’t understand what was going on. I added all kinds of the accept and return type annotations but nothing worked.

I missed that the class was annotated with @Controller instead of the correct @RestController. Sigh.

Lesson Learned: Again with the fundamentals.

Construction? I hear they’re in demand.

Howto Return 403 if User Has No Access

Spring has an interface called UserDetailsService that can be used to define how the user is loaded from the database for example. But I couldn’t figure out how to differentiate authentication to authorization, i.e. the user is known but not allowed in said page. I got nowhere. I hacked a solution at the front end side.

Lesson Learned: Failure is always an option.

Nannying? Oh wait I do that already.

Computer Meltdown

My old Surface Pro 3 started to buckle under the pressure so I bought a new computer. My unit test execution time went from 15 seconds to 2. No more total IntelliJ freezes. I could run WebStorm, IDEA and the app in an Docker container at the same time and still had memory to spare.

Lesson Learned: Solving problems with money is always a good idea, assuming you have it.

Urge to kill fading..

Angular Route Parameters

I needed to send parameters between controllers. Sure there are guides out there, but again the magic factor was too high. It felt like if strayed a millimeter from the examples and suddenly the route.params was empty or something. This was doubly true with Karma tests. Why is this RouterTestingModule now required when it wasn’t before kind of stuff. Especially how to set parameters to controllers seemed to differ wildly. I finally resolved it to something like this to pass paramaters to the controllers:

TestBed.configureTestingModule({
declarations: [GroupComponent],
imports: [FormsModule, RouterTestingModule.withRoutes([]), HttpClientTestingModule],
providers: [GroupsService, UserService, AlertService, AuthenticationService,
[GroupComponent, {
provide: ActivatedRoute,
useValue: {snapshot: {params: {'group': '123'}}}

}]]
}).compileComponents();

About as ugly as you can get as this gets passed to every test but hey, it works.

Lesson Learned: Again with the magic. Bleeding edge magic with limited documentation.

read original article here