Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finatra-jackson: add ability to bypass case class validation
Problem The `finatra/jackson` project bundles multiple deserialization features into `FinatraCaseClassDeserializer`. There is not a way to bypass case class validation while still maintaining feature compatability with the `FinatraObjectMapper`. An example use case for this would be receiving a `POST` request with a JSON body parsed by the `FinatraObjectMapper`, which does some validation. This content is then stored somewhere. The case class representations may change or introduce new validations after that data has been stored. If that data is retrieved and parsed via the `FinatraObjectMapper`, it will fail validation. Solution Introduce a `NullValidationFinatraJacksonModule` that can be used to bypass case class validation, while retaining the `FinatraObjectMapper`'s feature set. Result Users of the framework can now use the `NullValidationFinatraJacksonModule` to optionally bypass case class validation. Differential Revision: https://phabricator.twitter.biz/D307795
- Loading branch information
Showing
22 changed files
with
203 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...scala/com/twitter/finatra/json/internal/caseclass/validation/NullCaseClassValidator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.twitter.finatra.json.internal.caseclass.validation | ||
|
||
import com.twitter.finatra.validation.ValidationResult | ||
import java.lang.annotation.Annotation | ||
|
||
/** | ||
* No-op validator, which will treat any field values as acceptable during | ||
* Finatra Validation of annotations. | ||
*/ | ||
private[json] object NullCaseClassValidator extends CaseClassValidator { | ||
|
||
override def validateField[V]( | ||
fieldValue: V, | ||
fieldValidationAnnotations: Seq[Annotation] | ||
): Seq[ValidationResult] = Seq.empty | ||
|
||
override def validateObject(obj: Any): Seq[ValidationResult] = Seq.empty | ||
} |
8 changes: 8 additions & 0 deletions
8
...scala/com/twitter/finatra/json/internal/caseclass/validation/NullValidationProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.twitter.finatra.json.internal.caseclass.validation | ||
|
||
/** | ||
* Provides a validation bypass for case class validation. | ||
*/ | ||
private[json] object NullValidationProvider extends ValidationProvider { | ||
override def apply(): CaseClassValidator = NullCaseClassValidator | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
jackson/src/main/scala/com/twitter/finatra/json/modules/NullValidationCaseClassModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.twitter.finatra.json.modules | ||
|
||
import com.fasterxml.jackson.module.scala._ | ||
import com.twitter.finatra.json.internal.caseclass.jackson.CaseClassDeserializers | ||
import com.twitter.finatra.json.internal.caseclass.validation.NullValidationProvider | ||
|
||
/** | ||
* Module that supports skipping validation of Finatra validation annotations. | ||
*/ | ||
private[json] object NullValidationCaseClassModule extends JacksonModule { | ||
override def getModuleName = "NullValidationCaseClassModule" | ||
|
||
this += { _.addDeserializers(new CaseClassDeserializers(NullValidationProvider)) } | ||
} |
13 changes: 13 additions & 0 deletions
13
.../src/main/scala/com/twitter/finatra/json/modules/NullValidationFinatraJacksonModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.twitter.finatra.json.modules | ||
|
||
/** | ||
* Provides a FinatraJacksonModule that will treat all fields as acceptable during | ||
* Finatra Validation of annotations. | ||
*/ | ||
object NullValidationFinatraJacksonModule extends NullValidationFinatraJacksonModule | ||
|
||
class NullValidationFinatraJacksonModule extends FinatraJacksonModule { | ||
|
||
override val finatraCaseClassModule = Some(NullValidationCaseClassModule) | ||
|
||
} |
Oops, something went wrong.