-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JSON schema evolution support (#1760)
* feat: JSON schema evolution support * fmt * documentation + moving @migration to the rest of annotations * fixing event handlers with version * handling migration for ES subscriptions * fixing tests * minor improvements * fixing test * ranaming to JsonMigration * removing unused classes * replacing split to substring * JsonProperties removed * workflow serialization integration test * fmt
- Loading branch information
Showing
53 changed files
with
1,884 additions
and
111 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
68 changes: 68 additions & 0 deletions
68
sdk/java-sdk-protobuf/src/main/java/kalix/javasdk/JsonMigration.java
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,68 @@ | ||
/* | ||
* Copyright 2021 Lightbend 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 kalix.javasdk; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Allows to specify dedicated strategy for JSON schema evolution. | ||
* <p> | ||
* It is used when deserializing data of older version than the | ||
* {@link JsonMigration#currentVersion}. You implement the transformation of the | ||
* JSON structure in the {@link JsonMigration#transform} method. If you have changed the | ||
* class name you should add it to {@link JsonMigration#supportedClassNames}. | ||
*/ | ||
public abstract class JsonMigration { | ||
|
||
/** | ||
* Define current version, that is, the value used when serializing new data. The first version, when no | ||
* migration was used, is always 0. | ||
*/ | ||
public abstract int currentVersion(); | ||
|
||
/** | ||
* Define the supported forward version this migration can read (must be greater or equal than `currentVersion`). | ||
* If this value is different from {@link JsonMigration#currentVersion} a {@link JsonMigration#transform} will be used to downcast | ||
* the received payload to the current schema. | ||
*/ | ||
public int supportedForwardVersion() { | ||
return currentVersion(); | ||
} | ||
|
||
/** | ||
* Implement the transformation of the incoming JSON structure to the current | ||
* JSON structure. The `JsonNode` is mutable so you can add and remove fields, | ||
* or change values. Note that you have to cast to specific sub-classes such | ||
* as `ObjectNode` and `ArrayNode` to get access to mutators. | ||
* | ||
* @param fromVersion the version of the old data | ||
* @param json the incoming JSON data | ||
*/ | ||
public JsonNode transform(int fromVersion, JsonNode json) { | ||
return json; | ||
} | ||
|
||
/** | ||
* Override this method if you have changed the class name. Return | ||
* all old class names. | ||
*/ | ||
public List<String> supportedClassNames() { | ||
return List.of(); | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
sdk/java-sdk-protobuf/src/test/java/kalix/javasdk/DummyClass.java
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,50 @@ | ||
/* | ||
* Copyright 2021 Lightbend 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 kalix.javasdk; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
|
||
public class DummyClass { | ||
public String stringValue; | ||
public int intValue; | ||
public Optional<String> optionalStringValue; | ||
|
||
@JsonCreator | ||
public DummyClass(String stringValue, int intValue, Optional<String> optionalStringValue) { | ||
this.stringValue = stringValue; | ||
this.intValue = intValue; | ||
this.optionalStringValue = optionalStringValue; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DummyClass that = (DummyClass) o; | ||
return intValue == that.intValue && Objects.equals(stringValue, that.stringValue) && Objects.equals(optionalStringValue, that.optionalStringValue); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(stringValue, intValue, optionalStringValue); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
sdk/java-sdk-protobuf/src/test/java/kalix/javasdk/DummyClass2.java
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,57 @@ | ||
/* | ||
* Copyright 2021 Lightbend 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 kalix.javasdk; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
|
||
public class DummyClass2 { | ||
public String stringValue; | ||
public int intValue; | ||
public String mandatoryStringValue; | ||
|
||
@JsonCreator | ||
public DummyClass2(String stringValue, int intValue, String mandatoryStringValue) { | ||
this.stringValue = stringValue; | ||
this.intValue = intValue; | ||
this.mandatoryStringValue = mandatoryStringValue; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DummyClass2 that = (DummyClass2) o; | ||
return intValue == that.intValue && Objects.equals(stringValue, that.stringValue) && Objects.equals(mandatoryStringValue, that.mandatoryStringValue); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(stringValue, intValue, mandatoryStringValue); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DummyClass2{" + | ||
"stringValue='" + stringValue + '\'' + | ||
", intValue=" + intValue + | ||
", mandatoryStringValue='" + mandatoryStringValue + '\'' + | ||
'}'; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
sdk/java-sdk-protobuf/src/test/java/kalix/javasdk/DummyClass2Migration.java
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,37 @@ | ||
/* | ||
* Copyright 2021 Lightbend 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 kalix.javasdk; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
|
||
public class DummyClass2Migration extends JsonMigration { | ||
@Override | ||
public int currentVersion() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public JsonNode transform(int fromVersion, JsonNode json) { | ||
if (fromVersion < 1) { | ||
return ((ObjectNode) json).set("mandatoryStringValue", TextNode.valueOf("mandatory-value")); | ||
} else { | ||
return json; | ||
} | ||
} | ||
} |
Oops, something went wrong.