Skip to content

Commit

Permalink
Added README doc, updated build, and added licensing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpeterson committed Aug 1, 2016
1 parent 0fb4d96 commit 6b24bee
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 6 deletions.
128 changes: 126 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,126 @@
# jackson-module-model-versioning
Jackson 2.x module to support versioning and transforming of modules.
# Jackson Model Versioning Module
Jackson 2.x module for handling versioning of models.

## The Problem
Let's say we create an API that accepts the following car data JSON:
```json
{
"model": "honda:civic",
"year": 2016,
"new": "true"
}
```

Later, we decide that `model` should be split into two fields (`make` and `model`).
```json
{
"make": "honda"
"model": "civic",
"year": 2016,
"new": "true"
}
```

Then we decide that `new` should be actually be retyped and renamed (boolean `used`).
```json
{
"make": "honda"
"model": "civic",
"year": 2016,
"used": false
}
```

By this point, we have three formats of data that clients might be sending to our API. Hopefully we had the foresight to implement versioning on the models or API call's parameters.

There are many ways this could be achieved, such as creating multiple model objects (one per version) and assigning them to each API call's versioned endpoint:
```
POST /api/car/v1/ <- CarV1
GET /api/car/v1/ -> List<CarV1>
GET /api/car/v1/{id} -> CarV1
POST /api/car/v2/ <- CarV2
GET /api/car/v1/ -> List<CarV2>
GET /api/car/v2/{id} -> CarV2
...
```

Another, less boilerplate-reliant way to do this is to have a single version of the endpoints, but to version the models themselves instead. This is where this module comes in.

## The Solution
By using this Jackson module, we can annotate a single POJO with version-relevant conversion logic to execute on the raw JSON data before deserializing it into the model.
```groovy
@JsonVersionedModel(currentVersion = '3', converterClass = CarVersionedModelConverter)
class Car {
String make
String model
int year
boolean used
}
```
```groovy
class CarVersionedModelConverter implements VersionedModelConverter {
@Override
def void convert(String modelVersion, ObjectNode modelData) {
// model version is an int
def modelVersionNum = modelVersion as int
// version 1 had a single 'model' field that combined 'make' and 'model' with a colon delimiter; split
if(modelVersionNum < 2) {
def makeAndModel = modelData.get('model').asText().split(':')
modelData.put('make', makeAndModel[0])
modelData.put('model', makeAndModel[1])
}
// version 1-2 had a 'new' text field instead of a boolean 'used' field; convert and invert
if(modelVersionNum < 3)
modelData.put('used', !(modelData.remove('new').asText() as boolean))
}
}
```

All that's left is to configure the Jackson ObjectMapper with the module and test it out.
```groovy
def mapper = new ObjectMapper().registerModule(new VersioningModule())
def hondaCivic = mapper.readValue(
'{"model": "honda:civic", "year": 2016, "new": "true", "modelVersion": "1"}',
Car
)
println mapper.writeValueAsString(hondaCivic)
// prints '{"make": "honda", "model": "civic", "year": 2016, "used": false, "modelVersion": "3"}'
def toyotaCamry = mapper.readValue(
'{"make": "toyota", "model": "camry", "year": 2012, "new": "false", "modelVersion": "2"}',
Car
)
println mapper.writeValueAsString(hondaCivic)
// prints '{"make": "toyota", "model": "camry", "year": 2012, "used": true, "modelVersion": "3"}'
def mazda6 = mapper.readValue(
'{"make": "mazda", "model": "6", "year": 2017, "used": false, "modelVersion": "3"}',
Car
)
println mapper.writeValueAsString(mazda6)
// prints '{"make": "mazda", "model": "6", "year": 2017, "used": false, "modelVersion": "3"}'
```

## Compatibility
Compiled for Java 6 and tested with Jackson 2.2 - 2.8.

## Getting Started with Gradle
```groovy
dependencies {
compile 'com.github.jonpeterson:jackson-module-model-versioning:1.0.0'
}
```

## Getting Started with Maven
```xml
<dependency>
<groupId>com.github.jonpeterson</groupId>
<artifactId>jackson-module-model-versioning</artifactId>
<version>1.0.0</version>
</dependency>
```

## [JavaDoc](https://jonpeterson.github.io/docs/jackson-module-model-versioning/1.0.0/index.html)
130 changes: 129 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
buildscript {
ext {
testJacksonVersions = ['2.2.0', '2.3.0', '2.4.0', '2.5.0', '2.6.0', '2.7.0', '2.8.0']

// external dependency versions
groovyVersion = '2.4.5'
gradleVersion = '2.14.1'
jacksonVersion = '[2.2,)'
jacksonVersion = "[${testJacksonVersions.first()},)"
licensePluginVersion = '0.13.1'
releasePluginVersion = '2.4.0'
spockVersion = '1.0-groovy-2.4'

// external dependencies
Expand All @@ -14,6 +18,15 @@ buildscript {

repositories {
mavenCentral()
jcenter()
maven {
url 'https://plugins.gradle.org/m2/'
}
}

dependencies {
classpath "gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:${licensePluginVersion}"
classpath "net.researchgate:gradle-release:${releasePluginVersion}"
}
}

Expand All @@ -27,6 +40,10 @@ group = 'com.github.jonpeterson'

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'com.github.hierynomus.license'
apply plugin: 'net.researchgate.release'
apply plugin: 'maven'
apply plugin: 'signing'

sourceCompatibility = 1.6
targetCompatibility = 1.6
Expand All @@ -40,4 +57,115 @@ dependencies {

testCompile groovy
testCompile spockCore
}

license {
include '**/*.java'
include '**/*.groovy'
mapping {
java = 'SLASHSTAR_STYLE'
groovy = 'SLASHSTAR_STYLE'
}
}

// don't run tests with default test task
test.excludes = ['**/*']

// dynamically set up test executions for each version of Jackson
testJacksonVersions.each { version ->
def safeVersion = version.replaceAll('\\.', '_')

configurations {
it."testJackson${safeVersion}Compile".extendsFrom testCompile
it."testJackson${safeVersion}Runtime".extendsFrom testRuntime
}

sourceSets{
it."testJackson$safeVersion" {
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
}

dependencies {
it."testJackson${safeVersion}Compile" jacksonDatabind.replace(jacksonVersion, version)
}

task "testJackson$safeVersion"(type: Test) {
testClassesDir = sourceSets.test.output.classesDir
classpath = sourceSets."testJackson$safeVersion".runtimeClasspath
}

test.dependsOn "testJackson$safeVersion"
}


task javadocJar(type: Jar) {
dependsOn javadoc
classifier = 'javadoc'
from "$buildDir/javadoc"
}

task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}

build {
dependsOn javadocJar
dependsOn sourcesJar
}

artifacts {
archives javadocJar
archives sourcesJar
}

signing {
sign configurations.archives
}

uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { deployment -> signing.signPom(deployment) }

repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
authentication(userName: ossrhUsername, password: ossrhPassword)
}

snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots/') {
authentication(userName: ossrhUsername, password: ossrhPassword)
}

pom.project {
name 'Jackson Model Versioning Module'
packaging 'jar'
description 'Jackson 2.x module for handling versioning of models.'
url 'https://github.com/jonpeterson/jackson-module-module-versioning'

scm {
connection 'scm:git:git://github.com/jonpeterson/jackson-module-module-versioning.git'
developerConnection 'scm:git:[email protected]:jonpeterson/jackson-module-module-versioning.git'
url 'https://github.com/jonpeterson/jackson-module-module-versioning'
}

licenses {
license {
name 'MIT License'
url 'http://www.opensource.org/licenses/mit-license.php'
}
}

developers {
developer {
id 'jonpeterson'
name 'Jonathan Peterson'
email '[email protected]'
url 'https://github.com/jonpeterson'
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.jonpeterson.jackson.module.versioning;

import com.fasterxml.jackson.annotation.JacksonAnnotation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Jon Peterson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.jonpeterson.jackson.module.versioning;

import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Jon Peterson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.jonpeterson.jackson.module.versioning;

import com.fasterxml.jackson.core.JsonParser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Jon Peterson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.jonpeterson.jackson.module.versioning;

import com.fasterxml.jackson.core.JsonFactory;
Expand Down
Loading

0 comments on commit 6b24bee

Please sign in to comment.