Engineering With Java

Engineering With Java

Java Coding Question – Sneaky Enum Bug

Enum bug, concept, coding and solution

Suraj Mishra's avatar
Suraj Mishra
Jan 22, 2026
∙ Paid

Problem

Backend APIs often rely on enums to represent filter, sorting, or expansion parameters. Sounds clean and safe, right? Until it isn’t. Backend system expected enum values in lower-case snake_case, while real-world API clients were sending UPPER_CASE enum names… and everything silently broke. No errors. No warnings.
Just quietly failing behavior — the worst kind.

We have an enum:

Expected client input

"created_at"

What real clients actually send

"CREATED_AT"

or sometimes…

"Created_At"

Result in production

  • Enum is not mapped

  • Expansion logic doesn’t execute

  • Sorting / filtering silently fails

To fix this, we want to accept all of these:

CREATED_AT
created_at
Created_At

But we must reject the following:

createdAt
created-at
createdAt123
anything_unknown

Behavior requirements:

  • Normalize input

  • Map correctly to enum

  • Throw IllegalArgumentException for invalid values

  • Don’t break existing behavior


📢 Get actionable Java/Spring Boot insights every week — from practical code tips to real-world use-case based interview questions.
Join 3600+ subscribers and level up your Spring & backend skills with hand-crafted content — no fluff.

I want to offer an additional 40% discount ($ 18 per year) until January 31st. That’s just $1.5/mo. Unlock all the premium benefits including 35+ real world use cases.

Subscribe to Java newsletter

Not convinced? Check out the details of the past work


Solution

We will add the fromString() method to our SortField Enum. fromString() converts a string input from the client into the correct SortField enum constant.

First, we check if the input is null or blank; if so, we raise IllegalArgumentException.

User's avatar

Continue reading this post for free, courtesy of Suraj Mishra.

Or purchase a paid subscription.
© 2026 Suraj Mishra · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture