1

Something curious is happening in my application and I would like to know the reason.

I have a resource that is constructed with a backend service in my Spring server, and when the JSON arrives at the front end one of the property names is different. I have followed with breakpoints the entire construction of the resource and at no point before returning the query does the property name ever change from isHiddenOnQuote - as you probably suspect, it's defined as a boolean in the object model. My database stores the value as a 1 or 0.

When the JSON is received by my front end, the property name changes to hiddenOnQuote - the "is" magically drops off. Strange as well, is the fact that I have other boolean fields in the JSON that don't change; they retain their "is"ness.

Here is a snippet from the model. Note that none of these properties exist in the superclass, BaseEntity.

package com.company.app.model.sales;

import com.company.app.model.BaseEntity;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.List;

@Data
@Entity
@SQLDelete(sql =
        "UPDATE product_option_category " +
                "SET is_deleted = true " +
                "WHERE id = ?")
@Where(clause = "is_deleted = false")

public class ProductOptionCategory extends BaseEntity {
    private String categoryName;

    private int optionLimit;

    private int mnSegment;

    private boolean isBitwise;

    private boolean areOptionsRepeatable = false;

    private boolean isHiddenOnQuote = false;

    public boolean getIsBitwise() {
        return isBitwise;
    }
}

Is this a Lombok thing?

Taylor Burke
  • 424
  • 4
  • 15

2 Answers2

1

Seems like you may have hit upon a bug if isBitwise works well but not isHiddenOnQuote.

Also, noticed that you are not using the @Getter etc annotations from Lombok. Perhaps you can try using the following annotations to force the correct name:

@get:JsonProperty("isHiddenOnQuote")

@param:JsonProperty("isHiddenOnQuote")

I got this from this answer on Stackoverflow: https://stackoverflow.com/a/55100741/4402505

EDIT: Fixed the property name.

  • awesome! Thank you – Taylor Burke Sep 09 '20 at 16:47
  • 1
    Just for the sake of clarity, it looks like it wasn't a bug. I apologize, I had a getter function for the isBitwise field, I didn't include it because it didn't seem relevant. Now I understand Jackson would have used the method for serialization. – Taylor Burke Sep 09 '20 at 16:53
1

Yes, Lombok generate for primitive boolean variable method with prefix "is" according to Java naming convention (https://www.comp.nus.edu.sg/~cs2103/AY1617S1/contents/coding-standards-java.html). We can annotate that field with @Getter:

@Getter
private boolean running;

And Lombok will use its annotation processor to generate an isRunning() method in the class.

Sometimes, there can be conflicts. Let's say that we need to have the following lines in the same class:

@Getter
public boolean running = true;

@Getter
public boolean isRunning = false;

There are many reasons we should avoid a confusing naming convention like this. One of them is that it creates a conflict for Lombok.

Using Lombok's convention, these two fields would have the same accessor method name: isRunning. But having two methods with the same name in the same class will create a compiler error.

Lombok solves this by creating only one accessor method and, in this case, pointing it at running, based on field declaration order.

Oleg Ushakov
  • 1,200
  • 14
  • 27