1

For two different ReST API I am getting different response with common fields present.

I am creating two different bean for deserialization. Both beans have common fields present. Deserialization is working fine but sonar is giving issue that duplication block of code is present for the common fields.

Harsh Kanakhara
  • 909
  • 4
  • 13
  • 38

1 Answers1

6

There are 3 approaches to solve the issue, I can think of.

  1. You can create a parent bean and extract the common attributes to it and then extend the 2 beans from the newly created parent bean, that way you will not get code duplication.

  2. You can exclude beans from sonar -- because beans are just beans and you don't have to really worry about doing a sonar analysis on them but still it still may be good to perform a sonar analysis on the beans too, depends on what you want to do. You can find details here on how to do it: SonarQube Exclude a directory

  3. You can change the order of the fields -- This is a dumb thing to do but I used to do it just to resolve the sonar issues.

for example, if both files have

int a;
int b;
int c;
int d;

change it to

int a;
int c;
int b;
int d;

this will trick the sonarqube

  • Well, I can't say it is but I used to do it when there was an added sonar issue and was in a hurry to send it to deployment. Maybe you can consider moving the common fields to a parent bean and then extending the child beans from it @HarshKanakhara, that way you can get rid of the sonar duplication issue. I think this is a good practice. – Adhitha Dias Apr 20 '21 at 06:03
  • I followed the same approach but now there are multiple beans. I cannot extend them all. I can not pass it as a reference variable also otherwise need to write logic for deserialization. – Harsh Kanakhara Apr 20 '21 at 06:07