This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 155640

Summary: Entities are generated multiple times in RESTful web services
Product: webservices Reporter: pez <pez>
Component: RESTAssignee: Peter Liu <petertliu>
Status: VERIFIED FIXED    
Severity: blocker CC: ayubskhan, jungi, sustaining
Priority: P1    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:

Description pez 2008-12-17 11:02:02 UTC
The "convertor" classes for RESTful web services are not properly generated and the XML results are including same
elements multiple times. See example of the output bellow. The part of the code that seems to cause problems is this:

----->8--- Genereated code

@XmlRootElement(name = "prdBugs")
public class PrdBugsConverter {
    private Collection<PrdBug> entities;
    private Collection<PrdBugConverter> items;
    private URI uri;
    private int expandLevel;

.....

    /**
     * Creates a new instance of PrdBugsConverter.
     *
     * @param entities associated entities
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public PrdBugsConverter(Collection<PrdBug> entities, URI uri, int expandLevel) {
        this.entities = entities;
        this.uri = uri;
        this.expandLevel = expandLevel;
        getPrdBug();
    }

    /**
     * Returns a collection of PrdBugConverter.
     *
     * @return a collection of PrdBugConverter
     */
    @XmlElement
    public Collection<PrdBugConverter> getPrdBug() {
        if (items == null) {
            items = new ArrayList<PrdBugConverter>();
        }
        if (entities != null) {
            for (PrdBug entity : entities) {
                items.add(new PrdBugConverter(entity, uri, expandLevel, true));
            }
        }
        return items;
    }


---->8-- end of generated code

The problem I see is that the function getPrdBug is called twice (once from the constructor and once from the framework)
and each subsequent call of getPrdBug copies all the items from the entities collection into the items collection again.
After removing the call of getPrdBug from the constructor the issue seems to be fixed, however something else may get
broken - not tested.  


------>8--- Example of the output

<prdBugs uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/">
−
	<prdBug uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/">
<crId>123456</crId>
<id>1</id>
<idChangelog uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/idChangelog/"/>
−
	<prdContainerCollection
uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/prdContainerCollection/">
<prdContainer
uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/prdContainerCollection/1/"/>
<prdContainer
uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/prdContainerCollection/1/"/>
</prdContainerCollection>
<tstamp>2008-12-04T15:57:36+01:00</tstamp>
</prdBug>
−
	<prdBug uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/">
<crId>123456</crId>
<id>1</id>
<idChangelog uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/idChangelog/"/>
−
	<prdContainerCollection
uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/prdContainerCollection/">
<prdContainer
uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/prdContainerCollection/1/"/>
<prdContainer
uri="http://localhost:8080/prd_ws_t007/resources/prdContainers/1/prdBugCollection/1/prdContainerCollection/1/"/>
</prdContainerCollection>
<tstamp>2008-12-04T15:57:36+01:00</tstamp>
</prdBug>
</prdBugs>
Comment 1 pez 2008-12-17 11:12:39 UTC
btw. the getPrdBug() call in the constructor was added in the plugin sometimes between v 0.8 and v 1.0.1, before it
seemed to work properly ;-)
Comment 2 Lukas Jungmann 2009-01-12 12:19:49 UTC
Peter, Ayub, can I ask you to evaluate this issue ASAP, please? Thanks.
Comment 3 Lukas Jungmann 2009-01-12 14:38:47 UTC
btw: I'd like to add the fix for this to patch2 candidates list....
Comment 4 Ayub Khan 2009-01-12 23:51:20 UTC
This is a bug in the generated code. The correct code should be

    /**
     * Returns a collection of PrdBugConverter.
     *
     * @return a collection of PrdBugConverter
     */
    @XmlElement
    public Collection<PrdBugConverter> getPrdBug() {
        if (items == null) {
            items = new ArrayList<PrdBugConverter>();
        }
        if (entities != null) {
            items.clear();
            for (PrdBug entity : entities) {
                items.add(new PrdBugConverter(entity, uri, expandLevel, true));
            }
        }
        return items;
    }
Comment 5 Ayub Khan 2009-01-12 23:53:07 UTC
The line added "getXYZ()" to the constructor for dealing with lazy instantiation issue when Hibernate is used as the
Persistence layer. So it will stay there.
Comment 6 Ayub Khan 2009-01-13 10:32:29 UTC
Fixed.
Comment 7 Ayub Khan 2009-01-13 10:34:45 UTC
.
Comment 8 Lukas Jungmann 2009-01-13 23:04:48 UTC
v.
Comment 9 Lukas Jungmann 2009-01-13 23:06:18 UTC
Can I ask you to add a link to changeset with the fix to this issue, please? Thanks.
Comment 10 Quality Engineering 2009-01-14 07:46:09 UTC
Integrated into 'main-golden', will be available in build *200901140201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/c92d35a5eab3
User: Ayub Khan <ayubskhan@netbeans.org>
Log: Fix issue#155640 - Entities are generated multiple times in RESTful web services
Comment 11 jinb 2009-01-14 15:31:11 UTC
fix backported into release65_fixes branch
http://hg.netbeans.org/release65_fixes/rev/9148e08f62c5