beckchr / staxon

JSON via StAX
107 stars 47 forks source link

Staxon dont recognizes multiplePaths of references #23

Closed cwichoski closed 10 years ago

cwichoski commented 10 years ago

Hi,

I'm using staxon 1.3 and annotate some classes with multiplePaths but appears that I must always define the paths on RootElement, Staxon dont recognizes multiplePaths of references, here a sample:

@JsonXML(multiplePaths = { "carAccessoryList"}, virtualRoot = true, prettyPrint = false)
@Entity
@XmlRootElement
public class Car implements Serializable {
    @XmlElement
    @XmlInverseReference(mappedBy = "car")
    @OneToMany(fetch = FetchType.EAGER, cascade = ALL, mappedBy = "car")
    private ArrayList<CarAcessory> carAcessoryList;
    //... comented for brevity
}

@JsonXML(virtualRoot = true, prettyPrint = false)
@Entity
@XmlRootElement
public class CarAcessory implements Serializable {
    @OneToOne @JoinColumn(name = "accessoryID")
    private Accessory accessory;
    @XmlElement
    @XmlInverseReference(mappedBy = "accessoryList")
    @ManyToOne @JoinColumn(name = "carID", nullable = false)
    private Car car;
    //... comented for brevity
}

@JsonXML(virtualRoot = true, prettyPrint = false)
@Entity
@XmlRootElement
public class Accessory implements Serializable {
    //... comented for brevity
}

@JsonXML(virtualRoot = true, prettyPrint = false)
@Entity
@XmlRootElement
public class CarInStock implements Serializable {
    @OneToOne @JoinColumn(name = "carID")
    private Car car;
    //... comented for brevity
}

In this model if load a Car, I get correct array in carAcessoryList, but if load CarInStock the the carAcessoryList attribute of car always is one JSON object of that list and never gets to correct JSON array.

a workaround for this is to anotate the CarInStock with multiplePaths too, like this:

@JsonXML(multiplePaths = { "car/carAccessoryList"},virtualRoot = true, prettyPrint = false)
@Entity
@XmlRootElement
public class CarInStock implements Serializable {
    @OneToOne @JoinColumn(name = "carID")
    private Car car;
    //... comented for brevity
}

Then that works as expected, but I think that STAXON would deal with that automatically, as there is an anotation in Car class.

Then my doubt is this is a BUG, New Feature request or a specification matter that you need to specify all paths from root including the ones in the references?

Best regards.

Clóvis

beckchr commented 10 years ago

The marshaling is completely done by JAXB, StAXON only provides the writer. All that StAXON knows is the (root) class to be serialized. StAXON does not analyze/traverse your model to compose multiple-paths from other model classes.

While I agree that this may not seem optimal, it's the way it is: you need to specify all the reachable multiple-paths for each root element you want to marshal.

Note: In your example, you may simply use path suffix carAccessoryList instead of car/carAccessoryList in CarInStock.

cwichoski commented 10 years ago

If I use just List everything that ends with List will works? or must be exact phrase? as I have carAccessoryList, carCostList, carDriverList, etc... and many classes that points to Car, and a change in car with a new List I must change every referer class.

beckchr commented 10 years ago

Must be exact phrase, i.e. { "carAccessoryList", "carCostList", "carDriverList" }.