Code Monkey home page Code Monkey logo

polymorphia's People

Contributors

andreasprang avatar binaryoverload avatar t-beckmann avatar webermich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

polymorphia's Issues

WildcardTypes

Is It possible for you to code WildcardTypes?
It's very critical to what I'm doing and very much appreciated if you can support it!

public class GenericsExample {

int test = 10;
public List<? extends Fruit>basket;

}

15918 [main] ERROR de.bild.codec.PojoContext - WildcardTypes are not yet supported. ? extends com.volante.mainPackage.listGenTest.Fruit
org.apache.commons.lang3.NotImplementedException: WildcardTypes are not yet supported. ? extends com.volante.mainPackage.listGenTest.Fruit
at de.bild.codec.PojoContext$1.get(PojoContext.java:58)
at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:268)
at de.bild.codec.PojoContext.getCodec(PojoContext.java:141)
at de.bild.codec.PojoContext$AnyTypeCodecRegistry.getCodec(PojoContext.java:106)
at de.bild.codec.CollectionTypeCodec.(CollectionTypeCodec.java:33)
at de.bild.codec.ListTypeCodec.(ListTypeCodec.java:17)
at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:280)
at de.bild.codec.PojoContext.getCodec(PojoContext.java:141)
at de.bild.codec.PojoContext$AnyTypeCodecRegistry.getCodec(PojoContext.java:106)
at de.bild.codec.MappedField.(MappedField.java:91)
at de.bild.codec.BasicReflectionCodec.(BasicReflectionCodec.java:45)
at de.bild.codec.PojoContext.resolve(PojoContext.java:194)
at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:337)
at de.bild.codec.PojoContext.getCodec(PojoContext.java:141)
at de.bild.codec.PojoContext.get(PojoContext.java:119)
at de.bild.codec.PojoCodecProvider.get(PojoCodecProvider.java:56)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:43)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:55)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
at com.mongodb.MongoCollectionImpl.getCodec(MongoCollectionImpl.java:1066)
at com.mongodb.MongoCollectionImpl.executeInsertMany(MongoCollectionImpl.java:546)
at com.mongodb.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:524)
at com.mongodb.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:519)
at com.volante.mainPackage.MongoTestList.insertMongo(MongoTestList.java:91)
at com.volante.mainPackage.MongoTestList.main(MongoTestList.java:135)

IllegalArgumentException: The value for key %s can not be null

Great library when coding against the new MongoDB driver! Just a minor issue to report when dealing with raw BSONs.

The SimpleMapTypeCodec seems to be broken when decoding a BSON null value. The BSON null is decoded as a Java null and passed on to org.bson.BsonDocument.put(String, BsonValue) which throws an IllegalArgumentException when encountering Java null values.

I think the correct fix is to use org.bson.BsonNull.VALUE in place of Java null here:

looking for default constructor of an enum

I've got an exception:

Exception in thread "main" java.lang.IllegalArgumentException: Please provide a default constructor for class co.skyhawk.MyEnumType!
	at de.bild.codec.AbstractTypeCodec.getDefaultConstructor(AbstractTypeCodec.java:53)
	at de.bild.codec.AbstractTypeCodec.<init>(AbstractTypeCodec.java:25)
	at de.bild.codec.BasicReflectionCodec.<init>(BasicReflectionCodec.java:34)
	at de.bild.codec.PojoContext.resolve(PojoContext.java:93)
	at de.bild.codec.PolymorphicReflectionCodec.<init>(PolymorphicReflectionCodec.java:45)
	at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:156)
	at de.bild.codec.PojoContext.getCodec(PojoContext.java:66)
	at de.bild.codec.PojoContext$AnyTypeCodecRegistry.getCodec(PojoContext.java:212)
	at de.bild.codec.MappedField.<init>(MappedField.java:76)
	at de.bild.codec.BasicReflectionCodec.<init>(BasicReflectionCodec.java:39)
	at de.bild.codec.PojoContext.resolve(PojoContext.java:93)
	at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:160)
	at de.bild.codec.PojoContext.getCodec(PojoContext.java:66)
	at de.bild.codec.PojoContext$AnyTypeCodecRegistry.getCodec(PojoContext.java:212)
	at de.bild.codec.MapTypeCodec.<init>(MapTypeCodec.java:20)
	at de.bild.codec.SimpleMapTypeCodec.<init>(SimpleMapTypeCodec.java:19)
	at de.bild.codec.MapTypeCodec.getCodecIfApplicable(MapTypeCodec.java:50)
	at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:141)
	at de.bild.codec.PojoContext.getCodec(PojoContext.java:66)
	at de.bild.codec.PojoContext$AnyTypeCodecRegistry.getCodec(PojoContext.java:212)
	at de.bild.codec.MappedField.<init>(MappedField.java:76)
	at de.bild.codec.BasicReflectionCodec.<init>(BasicReflectionCodec.java:39)
	at de.bild.codec.PojoContext.resolve(PojoContext.java:93)
	at de.bild.codec.PolymorphicReflectionCodec.<init>(PolymorphicReflectionCodec.java:45)
	at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:156)
	at de.bild.codec.PojoContext.getCodec(PojoContext.java:66)
	at de.bild.codec.PojoContext$AnyTypeCodecRegistry.getCodec(PojoContext.java:212)
	at de.bild.codec.MappedField.<init>(MappedField.java:76)
	at de.bild.codec.BasicReflectionCodec.<init>(BasicReflectionCodec.java:39)
	at de.bild.codec.PojoContext.resolve(PojoContext.java:93)
	at de.bild.codec.PolymorphicReflectionCodec.<init>(PolymorphicReflectionCodec.java:45)
	at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:156)
	at de.bild.codec.PojoContext.getCodec(PojoContext.java:66)
	at de.bild.codec.PojoContext.get(PojoContext.java:44)
	at de.bild.codec.PojoCodecProvider.get(PojoCodecProvider.java:43)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:43)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:55)
	at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
	at com.mongodb.FindIterableImpl.createQueryOperation(FindIterableImpl.java:228)
	at com.mongodb.FindIterableImpl.execute(FindIterableImpl.java:224)
	at com.mongodb.FindIterableImpl.into(FindIterableImpl.java:220)
...
Caused by: java.lang.NoSuchMethodException: co.skyhawk.MyEnumType.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at de.bild.codec.AbstractTypeCodec.getDefaultConstructor(AbstractTypeCodec.java:46)
	... 46 more

The exact MyEnumType changes, but it is always a special enum as described below.

I haven't been able to create a test case that reproduces this issue yet, but I have a suspicion that Polymorphia is being fooled by my interface/enum structure:

public interface Displayable {
    String getLocalizationTag();
}

public enum MyEnumType implements SomeInterface {
    enum members;
    public String getLocalizationTag() {
        return some function of name();
    }
}

It seems PojoContext.calculateCodec() doesn't pick up that it is an enum, and falls down to trying the PolymorphicReflectionCodec on it.

Null still showing up for Lists

Class A {
.....
@EncodeNulls(false)
private List transList = null;
....
}

Even if transList is null, it still shows up in the document when inserted into Mongo.
I also did it in the registry and the same result.
My understanding would be that like other variables, it doesn't show up in the Mongo document if the value is null.

translist

Am I not doing something correctly or my understanding is wrong? If so how do I not include transList if it's null like the other objects?

Thanks!

provide a way to exclude a class from the PojoCodecProvider scanning

I've found a workaround for this issue, but I don't like it so I thought I would bring it to your attention.

I've got a class Id for which I need a custom codec. Technically it is a POJO, but I need it structured differently than normal in the BSON (it's actually a wrapper for a string that gets converted to an ObjectId for use as _id). The Id class ideally should reside in a certain package which contains my other domain objects, because it is directly referenced and incorporated into them.

So I registered the overall package with the PojoCodecProvider, then built a registry around that with CodecRegistries.fromCodecs(new IdCodec()) and .fromRegistries(pojoCodecProvider). But no matter what order I did that registration, my custom codec was not being called. Only when I moved the Id class out of the registered package, was its codec used.

Example setup:

CodecRegistry overallRegistry = fromRegistries(
		MongoClient.getDefaultCodecRegistry(),
		fromCodecs(
			new IdCodec()),
		fromProviders(
			new EnumCodecProvider(),
			createPojoCodecProvider()));

private PojoCodecProvider createPojoCodecProvider() {
	return PojoCodecProvider.builder()
		.register(Id.class.getPackage().getName())
		.build();
}

I tried switching the parameter order to fromRegistries, creating an IdCodecProvider and putting it in the fromProviders parameters, then varying the order of those parameters, etc.

One solution would be to annotate classes that should be excluded from the PojoCodecProvider's consideration. Or, the PojoCodecProvider.Builder could have an ignore(Class<?>) method.

Curious your thoughts on this!

ignore certain POJO methods

Hi, I've just started using Polymorphia for POJO mapping with MongoDB. I'm using the Java driver 3.5.0 but have had issues getting its built-in PojoCodecProvider to behave as I need.

I have a class that is blowing up because it has a method like this:
public <T> Map<String,T> getAttributesOfType(T argument) { ... }
(This is not an actual getter, as such.)

There is a method in ReflectionHelper being called - private static void getMethodTypePairsRecursive(ParameterizedType type, List<MethodTypePair> currentList, Map<String, Type> realTypeMap) - that scans all declared and inherited methods of my class and verifies that return types are fully resolved. It then blows up inside TypeUtils of commons-lang3 when it finds the second parameter not to be resolved.

The whole stack trace from MongoCollection.insertOne downwards is:
java.lang.IllegalArgumentException: null type argument at index 1 at org.apache.commons.lang3.Validate.noNullElements(Validate.java:513) at org.apache.commons.lang3.reflect.TypeUtils.parameterizeWithOwner(TypeUtils.java:1504) at de.bild.codec.ReflectionHelper.inferRealType(ReflectionHelper.java:106) at de.bild.codec.ReflectionHelper.getMethodTypePairsRecursive(ReflectionHelper.java:184) at de.bild.codec.ReflectionHelper.getMethodTypePairsRecursive(ReflectionHelper.java:158) at de.bild.codec.ReflectionHelper.getMethodTypePairsRecursive(ReflectionHelper.java:180) at de.bild.codec.ReflectionHelper.getMethodTypePairsRecursive(ReflectionHelper.java:158) at de.bild.codec.ReflectionHelper.getMethodTypePairsRecursive(ReflectionHelper.java:180) at de.bild.codec.ReflectionHelper.getMethodTypePairsRecursive(ReflectionHelper.java:158) at de.bild.codec.ReflectionHelper.getDeclaredAndInheritedMethods(ReflectionHelper.java:150) at de.bild.codec.BasicReflectionCodec.<init>(BasicReflectionCodec.java:64) at de.bild.codec.PojoContext.resolve(PojoContext.java:93) at de.bild.codec.PolymorphicReflectionCodec.<init>(PolymorphicReflectionCodec.java:45) at de.bild.codec.PojoContext.calculateCodec(PojoContext.java:156) at de.bild.codec.PojoContext.getCodec(PojoContext.java:66) at de.bild.codec.PojoContext.get(PojoContext.java:44) at de.bild.codec.PojoCodecProvider.get(PojoCodecProvider.java:43) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:43) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:55) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37) at com.mongodb.MongoCollectionImpl.getCodec(MongoCollectionImpl.java:591) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:314)

Thinking that perhaps the name was confusing things, I changed it not to look like a getter, but that had no effect.

Is there a way to annotate a method as not to be mapped during encoding?

Thanks,
Sean

change in enforcement of @Id field type

Hello Michael,

I noticed an issue today while trying to upgrade from 2.3.0 to 2.3.5, having to do with the changes around line 63 of BasicReflectionCodec where it checks that the type of the field annotated with @id is ObjectId.class. Our code, on the other hand, is using a custom Id type that is a simple wrapper around a String. The reasons are a bit complicated, but have to do with using GWT to compile our POJOs into Javascript.

Previously this "just worked" - I assume that is because we have a custom codec registered for our Id class, that knows it is actually stored as a bare ObjectId.

I understand that the correct solution is to use a custom IdGenerator for our Id field. Unfortunately, because of the GWT constraint, our POJOs cannot reference classes such as ObjectId in the BSON library.

Is it possible to enhance the field type test to somehow allow classes for which an ObjectId to codec exists? I don't know how that could be accomplished. Or perhaps (though it is icky) a parameter in the @id annotation that disables the strict check?

Thanks,
Sean

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.