Skip to content

Commit

Permalink
functions may return null in collections of entities (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfhandl authored Nov 6, 2024
1 parent fbdbc32 commit a40b845
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 65 deletions.
11 changes: 0 additions & 11 deletions lib/xml2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -1730,17 +1730,6 @@ module.exports.xml2json = function (
if (node.attributes[name]) {
switch (name) {
case "Nullable":
if (
target.$Collection &&
target.$Type === "Edm.EntityType" &&
node.local === "ReturnType"
) {
reportError(
`Element ${node.local}, Type=Collection(Edm.EntityType) with Nullable attribute`,
parser
);
break;
}
if (node.attributes[name].value === "true") target.$Nullable = true;
break;
case "Abstract":
Expand Down
92 changes: 38 additions & 54 deletions test/xml2json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,44 @@ describe("Edge cases", function () {
});
}
});

it("Nullable collection of entities in ReturnType", function () {
const xml = `<Edmx Version="4.01" xmlns="http://docs.oasis-open.org/odata/ns/edmx">
<DataServices>
<Schema Namespace="n" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Function Name="f">
<ReturnType Type="Collection(Edm.EntityType)" Nullable="false"/>
</Function>
<Function Name="g">
<ReturnType Type="Collection(n.bar)" Nullable="false"/>
</Function>
<EntityType Name="bar" />
</Schema>
</DataServices>
</Edmx>`;

const messages = [];
const json = csdl.xml2json(xml, { messages });
assert.deepStrictEqual(json, {
$Version: "4.01",
n: {
f: [
{
$Kind: "Function",
$ReturnType: { $Collection: true, $Type: "Edm.EntityType" },
},
],
g: [
{
$Kind: "Function",
$ReturnType: { $Collection: true, $Type: "n.bar" },
},
],
bar: { $Kind: "EntityType" },
},
});
assert.deepStrictEqual(messages, []);
});
});

describe("Error cases", function () {
Expand Down Expand Up @@ -965,60 +1003,6 @@ describe("Error cases", function () {
}
});

it("forbidden Nullable in ReturnType", function () {
const xml = `<Edmx Version="4.01" xmlns="http://docs.oasis-open.org/odata/ns/edmx">
<DataServices>
<Schema Namespace="n" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Function Name="f">
<ReturnType Type="Collection(Edm.EntityType)" Nullable="false"/>
</Function>
</Schema>
</DataServices>
</Edmx>`;

const messages = [];
const json = csdl.xml2json(xml, { messages });
assert.deepStrictEqual(json, {
$Version: "4.01",
n: {
f: [
{
$Kind: "Function",
$ReturnType: { $Collection: true, $Type: "Edm.EntityType" },
},
],
},
});
assert.deepStrictEqual(messages, [
{
message:
"Element ReturnType, Type=Collection(Edm.EntityType) with Nullable attribute",
parser: {
construct:
'<ReturnType Type="Collection(Edm.EntityType)" Nullable="false"/>',
line: 5,
column: 76,
},
},
]);

try {
csdl.xml2json(xml, { strict: true });
assert.fail("should not get here");
} catch (e) {
assert.strictEqual(
e.message.split("\n")[0],
"Element ReturnType, Type=Collection(Edm.EntityType) with Nullable attribute"
);
assert.deepStrictEqual(e.parser, {
construct:
'<ReturnType Type="Collection(Edm.EntityType)" Nullable="false"/>',
line: 5,
column: 76,
});
}
});

it("misplaced element", function () {
const xml = `<Edmx Version="4.0" xmlns="http://docs.oasis-open.org/odata/ns/edmx">
<Schema Namespace="n">
Expand Down

0 comments on commit a40b845

Please sign in to comment.