swagger-play
swagger-play copied to clipboard
Swagger & java play 2 class loading problems 1.3.12
From @joantune on March 29, 2015 16:19
Hi!
I have been working with Swagger and its 1.3.12 version (actually I pushed it up to 1.3.13-SNAPSHOT, only locally, and I took the dependency that I had with rs-api 1.x see issue #900 - but this shouldn't matter, unless there have been new updates that I'm not getting that could fix this)
On a class I had implemented a Key value pair class as such:
@MappedSuperclass
public abstract class ExternallyMappedObj<T extends DoubleExternalDated<?>> extends DoubleExternalDated<T> implements
ExtenallyMappedInterface {
[...]
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "key")
@CollectionTable(joinColumns = @javax.persistence.JoinColumn(name = "id"))
@JsonInclude(Include.NON_EMPTY)
private final Map<String, Classifier> classifiers = new HashMap<String, Classifier>();
Where Classifier was simply something like:
@Embeddable
public class Classifier {
String value;
}
Problem is, when codegenerating, I had the error that the class: Map[java.lang.String,models.rawdata.Classifier] could not be found. I tried to add app.classloader().loadClass(Classifier.class.getName()); and app.classloader().loadClass("Map[java.lang.String,models.rawdata.Classifier]"); -> Which fails
and because of that, I tried also
HashMap<String, Classifier> hashMap = new HashMap<String, Classifier>();
hashMap.clear();
On Global's public void beforeStart(Application app) so that the classloader would have that class by the time that it needed it to generate the Json, but the error never disappeared.
I ended up using a simple Map<String,String> which works:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(joinColumns = @JoinColumn(name = "ID"))
@MapKeyColumn(name = "name")
@Column(name = "value")
private final Map<String, String> classifiers = new HashMap<String, String>();
But it would be cool to allow for a custom value classes. It sounds like a bug on the class loading/mapper of Swagger. As you could probably see from my app.classloader().loadClass("Map[java.lang.String,models.rawdata.Classifier]"); naive try, I have no experience/much knowledge on how the classloaders work, and how they treat these kinds of classes, and because I solved it in another way and I'm pressed for time, I didn't poke much around ModelConverters.scala or SwaggerContext.scala to try to fix this, but still, I think it's an important issue so I explain it here for more capable and less time constrained hands to address this issue.
Thanks for the good work, it has been very helpful. On a seperate note, I'm planning on giving back in the form of a Jekkyll template(s) that I use to generate the documentation (I know that swagger-codegen exists but more often than not, you want to add lots of other stuff to the documentation, plus there are lots of designers that have Jekyll experience). More news on that when I'm not time pressed.
Cheers, JA
Copied from original issue: swagger-api/swagger-core#944
Sorry for the delayed response. Since we're not going to deal with it in 1.3, I've made reference to it from #918 so we can look into it in the new module.