Skip to content

Instantly share code, notes, and snippets.

@ttomsu
Created January 5, 2016 21:47
Show Gist options
  • Save ttomsu/5e3409cb1b7d67a70f3d to your computer and use it in GitHub Desktop.
Save ttomsu/5e3409cb1b7d67a70f3d to your computer and use it in GitHub Desktop.
Here's an example of using @JsonSetter annotations as a "preprocessor"-like way to move clients using old API while still being able to update Orca's internal model.
/*
* Copyright 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.orca.clouddriver
import com.fasterxml.jackson.annotation.JsonSetter
import com.fasterxml.jackson.databind.ObjectMapper
import groovy.transform.Canonical
import spock.lang.Specification
class MixinTest extends Specification {
def "should pass"() {
given:
ObjectMapper mapper = new ObjectMapper()
when:
Pojo result = mapper.readValue("{\"region\": \"foo\"}", Pojo)
then:
result
result.regions == ["foo"]
when:
result = mapper.readValue("{\"regions\": [\"foo\", \"bar\"]}", Pojo)
then:
result
result.regions == ["foo", "bar"]
when:
result = mapper.readValue("{\"someOlderApiValue\": [\"foo\", \"bar\"]}", Pojo)
then:
result
result.regions == ["foo", "bar"]
}
@Canonical
static class Pojo {
// Most up-to-date model
List<String> regions
@JsonSetter("region")
String fromRegion(String r) {
regions = [r]
}
@JsonSetter("someOlderApiValue")
String fromSomeOlderApiValue(List l) {
regions = l
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment