networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
807 stars 320 forks source link

Refactored code: Pull-up var/method, Extract class, Polymorphism #1004

Closed meer1616 closed 3 months ago

meer1616 commented 3 months ago
  1. PropertiesValidator, PrefixItemsValidator, ItemsValidator202012, and ItemsValidator class have duplicate code i.e. “getDefaultNode” method and all these classes extend the same class i.e. BaseJsonValidator. Hence, I have pulled up the “getDefaultNode” method in its BaseJsonValidator class. This promotes code reusability and eliminates duplication within those subclasses. This refactoring can simplify maintenance and reduce the risk of inconsistencies if the same logic is duplicated across multiple classes.

  2. The BaseJsonValidator class contains many useful methods. Creating the new class called DiscriminatorHandler encapsulates all the methods and functionality related to discriminator handling. The BaseJsonValidator class now delegates the calls to discriminator-related methods to the DiscriminatorHandler class. By extracting the discriminator-related functionality into a separate class, the code organization, maintainability, and reusability are improved. The BaseJsonValidator class is now more focused on its core responsibilities, making it easier to understand and maintain.

  3. In AnnotationKeyword class, getAnnotationValue method’s conditional logic has been replaced with polymorphic behavior. Each type of JSON node is now handled by a separate subclass of AnnotationsHandler which promotes cleaner code and easier maintenance and also be used for future functionality enhancement concerned with annotation value.