jaystack / jaydata

Notice: this library isn't maintained anymore
http://jaydata.org
GNU General Public License v2.0
352 stars 95 forks source link

$format=json doesn't work correct #219

Open englertm opened 9 years ago

englertm commented 9 years ago

Hi,

I built an odata service with a header- ("Einsatz") and an item table ("EinsatzkraftSet") using node.js with modules jaydata 1.3.6, express 3.20.3 and mongodb 1.4.38

The ODATA-Service itself is working, when I call the URL http://localhost:52999/odata/asb/einsatz.svc/Einsaetze then I receive the header data

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><feed xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="http://localhost:52999/odata/asb/einsatz.svc/Einsaetze" xmlns="http://www.w3.org/2005/Atom"><id>http://localhost:52999/odata/asb/einsatz.svc/Einsaetze</id><title type="text">Einsaetze</title><updated>2015-06-08T09:51:26.149Z</updated><link href="Einsaetze" rel="self" title="Einsaetze"></link><entry><id>http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')</id><category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="$asb.Einsatz"></category><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')" rel="edit" title="Einsatz"></link><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EASet" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EASet" type="application/atom+xml;type=feed"></link><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EinsatzkraftSet" type="application/atom+xml;type=feed"></link><title></title><updated>2015-06-08T09:51:26.150Z</updated><author><name></name></author><content type="application/xml"><m:properties><d:Id>NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk</d:Id><d:Einsatznummer m:type="Edm.Int32">1</d:Einsatznummer><d:Einsatzdatum m:type="Edm.DateTime">2015-06-01T00:00:00.000</d:Einsatzdatum><d:Einsatzname>Erster Testeinsatz</d:Einsatzname><d:Einsatzort>irgendwo</d:Einsatzort><d:Status m:type="Edm.Int32">0</d:Status></m:properties></content></entry></feed>

The URL http://localhost:52999/odata/asb/einsatz.svc/Einsaetze?$format=json delivers me the same entry:

{"d":{"results":[{"__metadata":{"type":"$asb.Einsatz","id":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')","uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')"},"Id":"NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk","Einsatznummer":1,"Einsatzdatum":"/Date(1433116800000)/","Einsatzname":"Erster Testeinsatz","Einsatzort":"irgendwo","Status":0,"EASet":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EASet"}},"EinsatzkraftSet":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet"}}}]}}

So far so good, now when I call the URL for the item table ("EinsatzkraftSet"), I receive one entry: http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="http://localhost:52999/odata/asb/einsatz.svc/Einsaetze" xmlns="http://www.w3.org/2005/Atom"><id>http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')</id><category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="$asb.Einsatz"></category><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')" rel="edit" title="Einsatz"></link><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EASet" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EASet" type="application/atom+xml;type=feed"></link><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EinsatzkraftSet" type="application/atom+xml;type=feed"><m:inline><feed><id>http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte</id><title type="text">Einsatzkraefte</title><updated>2015-06-08T09:57:48.888Z</updated><link href="Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet" rel="self" title="Einsatzkraefte"></link><entry><id>http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')</id><category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="$asb.Einsatzkraft"></category><link href="Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')" rel="edit" title="Einsatzkraft"></link><title></title><updated>2015-06-08T09:57:48.891Z</updated><author><name></name></author><content type="application/xml"><m:properties><d:Id>NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm</d:Id><d:Helfernummer m:type="Edm.Int32">1</d:Helfernummer><d:Nachname>Nachname1</d:Nachname><d:Vorname m:null="true"></d:Vorname><d:Geschlecht m:null="true"></d:Geschlecht><d:Organisation m:null="true"></d:Organisation><d:ZeitstempelAn m:null="true" m:type="Edm.DateTime"></d:ZeitstempelAn><d:ZeitstempelAb m:null="true" m:type="Edm.DateTime"></d:ZeitstempelAb><d:Funkrufname m:null="true"></d:Funkrufname></m:properties></content></entry></feed></m:inline></link><title></title><updated>2015-06-08T09:57:48.893Z</updated><author><name></name></author><content type="application/xml"><m:properties></m:properties></content></entry>

BUT when I call this URL with extension $format=json I receive no entries!!! http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet?$format=json:

{"d":{"__metadata":{"type":"$asb.Einsatz","id":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')","uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')"},"EinsatzkraftSet":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet"}}}}

My workaround is to call the header URL with $expand for the item table "EinsatzkraftSet" http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')?$format=json&$expand=EinsatzkraftSet

{"d":{"__metadata":{"type":"$asb.Einsatz","id":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')","uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')"},"Id":"NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk","Einsatznummer":1,"Einsatzdatum":"/Date(1433116800000)/","Einsatzname":"Erster Testeinsatz","Einsatzort":"irgendwo","Status":0,"EASet":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EASet"}},"EinsatzkraftSet":[{"__metadata":{"id":"http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')","uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')","type":"$asb.Einsatzkraft"},"Id":"NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm","Helfernummer":1,"Nachname":"Nachname1","Vorname":null,"Geschlecht":null,"Organisation":null,"Qualifikation":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')/Qualifikation"}},"ZeitstempelAn":null,"ZeitstempelAb":null,"Funkrufname":null,"Einsatz":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')/Einsatz"}},"Einsatzabschnitt":{"__deferred":{"uri":"http://localhost:52999/odata/asb/einsatz.svc/Einsatzkraefte('NTU2ZWNmNzNkZjk1ZDUyMDFiNTI0MDBm')/Einsatzabschnitt"}}}]}}

Is it a bug in jaydata?

Marcus

Here's my code:

var c = require('express');
require('jaydata');
window.DOMParser=require('xmldom').DOMParser;
require('q');
require('./einsatz_model.js');
var app = c();
app.use(c.query());
app.use(c.bodyParser());
app.use(c.cookieParser());
app.use(c.methodOverride());
app.use(c.session({ secret: 'session key' }));
app.use("/odata/asb/einsatz.svc", $data.JayService.OData.Utils.simpleBodyReader());
app.use("/odata/asb/einsatz.svc", $data.JayService.createAdapter($asb.Context, function (req, res) {
    return new $asb.Context({name: "mongoDB", databaseName:"ASB", address: "localhost", port: 27017 });
}));
app.use("/", c.static(__dirname));
app.use(c.errorHandler());
app.listen(52999);
console.log("ODATA Server running at http://localhost:52999/odata/asb/einsatz.svc");

einsatz_model.js:

$data.Class.define("$asb.Einsatz", $data.Entity, null, {
    Id: { type: "id", key: true, computed: true, nullable: false },
    Einsatznummer: { type: "int"},
    Einsatzdatum: { type: "date"},
    Einsatzname: { type: "string" },
    Einsatzort: { type: "string" },
    Status: { type: "int" },
    EinsatzkraftSet: { type: Array, elementType: "$asb.Einsatzkraft", inverseProperty: "Einsatz" }
}, null);
...
$data.Class.define("$asb.Einsatzkraft", $data.Entity, null, {
    Id: { type: "id", key: true, computed: true, nullable: false },
    Helfernummer: { type: "int"},
    Nachname: { type: "string" },
    Vorname: { type: "string" },
    Geschlecht: { type: "string" },
    Organisation: { type: "string" },
    Qualifikation: { type: "$asb.Qualifikation", inverseProperty: "EinsatzkraftSet"},
    ZeitstempelAn: { type: "datetime" },
    ZeitstempelAb: { type: "datetime" },
    Funkrufname: { type: "string" },
    Einsatz: { type: "$asb.Einsatz", inverseProperty: "EinsatzkraftSet"},
}, null);
$data.Class.defineEx("$asb.Context", [$data.EntityContext,$data.ServiceBase], null, {
    Einsaetze: { type: $data.EntitySet, elementType: $asb.Einsatz },
    Einsatzkraefte: { type: $data.EntitySet, elementType: $asb.Einsatzkraft },
...
});
exports = $asb.Context;
englertm commented 9 years ago

It's not only a problem with the extension '$format=json', it's every time when I want ja JSON response for a collection that is not at the first level. If the HTTP header is set to 'Accept: application/json' then the odata service returns also no items when I do a GET request at the item table: http://localhost:52999/odata/asb/einsatz.svc/Einsaetze('NTU2ZGM3NDljNTZiY2NjNDE0NGQ3ODNk')/EinsatzkraftSet

I created a openUI5 application that uses this ODATA service, openUI5 sets automatically the header 'Accept: application/json', so I receive no data. I can't use this Jaydata ODATA service for a OpenUI5 application.

Does anyone have a solution for this?

Thank you, Marcus