Skip to content

Commit

Permalink
Merge branch '2.10' into 2.11
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 23, 2020
2 parents c8f877e + 86a0240 commit b9b9ebb
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ protected AvroStructureReader createMapReader(Schema writerSchema, Schema reader
String valueTypeId = readerSchema.getValueType().getProp(AvroSchemaHelper.AVRO_SCHEMA_PROP_CLASS);
return MapReader.construct(dec, typeId, keyTypeId, valueTypeId);
}
return MapReader.construct(createReader(writerElementType, readerSchema.getElementType()), typeId, keyTypeId);
return MapReader.construct(createReader(writerElementType, readerSchema.getValueType()), typeId, keyTypeId);
}

protected AvroStructureReader createRecordReader(Schema writerSchema, Schema readerSchema)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.fasterxml.jackson.dataformat.avro.schemaev;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.avro.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class RecordEvolutionTest extends AvroTestBase
{
static String SCHEMA_V1 = "{\n"
+ " \"type\": \"record\",\n"
+ " \"name\": \"User\",\n"
+ " \"fields\": [\n"
+ " {\n"
+ " \"name\": \"name\",\n"
+ " \"type\": [\"string\", \"null\"],\n"
+ " \"default\": null\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"preferences\", \n"
+ " \"type\": {\n"
+ " \"type\": \"map\",\n"
+ " \"values\": {\n"
+ " \"type\": \"array\",\n"
+ " \"items\": \"string\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}";

static String SCHEMA_V2 = "{\n"
+ " \"type\": \"record\",\n"
+ " \"name\": \"User\",\n"
+ " \"fields\": [\n"
+ " {\n"
+ " \"name\": \"fullName\",\n"
+ " \"type\": [\"string\", \"null\"],\n"
+ " \"default\": null,\n"
+ " \"aliases\": [\"name\"]\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"preferences\", \n"
+ " \"type\": {\n"
+ " \"type\": \"map\",\n"
+ " \"values\": {\n"
+ " \"type\": \"array\",\n"
+ " \"items\": \"string\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}";

static class UserV1 {
public String name;
public Map<String, List<String>> preferences;

@JsonCreator
public UserV1(final String name, final Map<String, List<String>> preferences) {
this.name = name;
this.preferences = preferences;
}

public boolean equals(final Object object) {
if (this == object) {
return true;
}

if (!(object instanceof UserV1)) {
return false;
}

final UserV1 user = (UserV1) object;

return name.equals(user.name) &&
preferences.equals(user.preferences);
}

public int hashCode() {
return Objects.hash(name, preferences);
}

@Override
public String toString() {
return "UserV1{" +
"name='" + name + '\'' +
", preferences=" + preferences +
'}';
}
}

static class UserV2 {
public String fullName;
public Map<String, List<String>> preferences;

public UserV2(
@JsonProperty("fullName") final String fullName,
@JsonProperty("preferences") final Map<String, List<String>> preferences
) {
this.fullName = fullName;
this.preferences = preferences;
}

public boolean equals(final Object object) {
if (this == object) {
return true;
}

if (!(object instanceof UserV2)) {
return false;
}

final UserV2 user = (UserV2) object;

return fullName.equals(user.fullName) && preferences.equals(user.preferences);
}

public int hashCode() {
return Objects.hash(fullName, preferences);
}

@Override
public String toString() {
return "UserV2{" +
"fullName='" + fullName + '\'' +
", preferences=" + preferences +
'}';
}
}

private final AvroMapper MAPPER = getMapper();

public void testEvolutionInvolvingComplexRecords() throws Exception
{
final AvroSchema schemaV1 = MAPPER.schemaFrom(SCHEMA_V1);
final AvroSchema schemaV2 = MAPPER.schemaFrom(SCHEMA_V2);
final AvroSchema combinedSchema = schemaV1.withReaderSchema(schemaV2);

final Map<String, List<String>> preferences = new HashMap<>();
final List<String> list = new ArrayList<>();
list.add("yes");

preferences.put("jackson", list);

final UserV1 userV1 = new UserV1("foo", preferences);

final byte[] avro = MAPPER.writer(schemaV1).writeValueAsBytes(userV1);

final UserV2 userV2 = MAPPER.readerFor(UserV2.class)
.with(combinedSchema)
.readValue(avro);

assertEquals(userV2.fullName, userV1.name);
assertEquals(userV2.preferences, userV1.preferences);
}
}
4 changes: 3 additions & 1 deletion release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ Juliana Amorim (amorimjuliana@github)
Marcos Passos (marcospassos@github)
* Contributed fix for #168: (avro) `JsonMappingException` for union types with multiple Record types
(2.10.0)
* Contributed fix for #173: (Avro) Improve Union type serialization performance
* Contributed fix for #173: (avro) Improve Union type serialization performance
(2.10.0)
* Contributed fix for #211: (avro) Fix schema evolution involving maps of non-scalar
(2.10.5)

John (iziamos@github)
* Reported, suggested fix for #178: Fix issue wit input offsets when parsing CBOR from `InputStream`
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project: jackson-datatypes-binaryModules:

#204: (ion) Add `IonFactory.getIonSystem()` accessor
(contributed by Paul F)
#211: (avro) Fix schema evolution involving maps of non-scalar
(fix contributed by Marcos P)

2.10.4 (03-May-2020)

Expand Down

0 comments on commit b9b9ebb

Please sign in to comment.