-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer to load custom property codec providers before default codec p…
…roviders. (#3038) * Prefer to load custom property codec providers before default codec providers. --------- Co-authored-by: Justin Lee <[email protected]>
- Loading branch information
1 parent
dec7d40
commit 23b0583
Showing
4 changed files
with
77 additions
and
3 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
29 changes: 29 additions & 0 deletions
29
core/src/test/java/dev/morphia/mapping/codec/MorphiaCodecProviderTest.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,29 @@ | ||
package dev.morphia.mapping.codec; | ||
|
||
import java.util.List; | ||
|
||
import dev.morphia.Datastore; | ||
import dev.morphia.test.TestBase; | ||
|
||
import org.bson.codecs.pojo.PropertyCodecProvider; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
import static org.testng.AssertJUnit.assertTrue; | ||
|
||
public class MorphiaCodecProviderTest extends TestBase { | ||
|
||
@Test | ||
public void ensureCustomCodedProvidersComeFirst() { | ||
// given a custom 'no-op' codec is provided in the service configuration | ||
// (see META-INF/services/dev.morphia.mapping.codec.MorphiaPropertyCodecProvider) | ||
// when we instantiate a morphia codec provider | ||
Datastore datastore = getDs(); | ||
MorphiaCodecProvider provider = new MorphiaCodecProvider(datastore); | ||
|
||
// then we expect that the custom provider we provided is the first codec in the list | ||
List<PropertyCodecProvider> providers = provider.getPropertyCodecProviders(); | ||
assertEquals(providers.size(), 3); | ||
assertTrue(providers.get(0) instanceof NoOpMorphiaPropertyCodecProvider); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
core/src/test/java/dev/morphia/mapping/codec/NoOpMorphiaPropertyCodecProvider.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,43 @@ | ||
package dev.morphia.mapping.codec; | ||
|
||
import org.bson.BsonReader; | ||
import org.bson.BsonWriter; | ||
import org.bson.codecs.Codec; | ||
import org.bson.codecs.DecoderContext; | ||
import org.bson.codecs.EncoderContext; | ||
import org.bson.codecs.pojo.PropertyCodecRegistry; | ||
import org.bson.codecs.pojo.TypeWithTypeParameters; | ||
|
||
public class NoOpMorphiaPropertyCodecProvider extends MorphiaPropertyCodecProvider { | ||
|
||
@Override | ||
public <T> Codec<T> get(TypeWithTypeParameters<T> typeWithTypeParameters, | ||
PropertyCodecRegistry propertyCodecRegistry) { | ||
if (NoOpClass.class.isAssignableFrom(typeWithTypeParameters.getType())) { | ||
return (Codec<T>) new NoOpCodec(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static class NoOpCodec implements Codec<NoOpClass> { | ||
|
||
@Override | ||
public NoOpClass decode(BsonReader bsonReader, DecoderContext decoderContext) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void encode(BsonWriter bsonWriter, NoOpClass unused, EncoderContext encoderContext) { | ||
|
||
} | ||
|
||
@Override | ||
public Class<NoOpClass> getEncoderClass() { | ||
return NoOpClass.class; | ||
} | ||
} | ||
|
||
static class NoOpClass { | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...c/test/resources/META-INF/services/dev.morphia.mapping.codec.MorphiaPropertyCodecProvider
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 @@ | ||
dev.morphia.mapping.codec.NoOpMorphiaPropertyCodecProvider |