Skip to content

Commit

Permalink
Convert SAP Annotations for OData V2 into Capabilities (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfhandl authored Oct 20, 2022
1 parent 4d4ec07 commit e17284f
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 153 deletions.
19 changes: 9 additions & 10 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"recommendations": [
"bierner.github-markdown-preview",
"dbaeumer.vscode-eslint",
"eg2.vscode-npm-script",
"esbenp.prettier-vscode",
"hbenl.vscode-mocha-test-adapter",
"hbenl.vscode-test-explorer",
"redhat.vscode-xml",
"streetsidesoftware.code-spell-checker"
]
"recommendations": [
"bierner.github-markdown-preview",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"hbenl.vscode-mocha-test-adapter",
"hbenl.vscode-test-explorer",
"redhat.vscode-xml",
"streetsidesoftware.code-spell-checker"
]
}
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"cSpell.words": [
"annotatable",
"csdl",
"Edmx",
"Insertable",
"odata",
"onclosetag",
"onopentag",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.7.0 2022-10-20

### Added

- `xml2json`: accept some [SAP Annotations for OData Version 2.0](https://github.com/SAP/odata-vocabularies/blob/main/docs/v2-annotations.md) and turn them into [Capabilities](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Capabilities.V1.md)

## 0.6.0 2022-08-31

### Changed
Expand Down
60 changes: 60 additions & 0 deletions lib/xml2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,11 @@ module.exports.xml2json = function (
"EntityType",
"IncludeInServiceDocument",
]);
setCapabilities(current.containerChild, node, [
"creatable",
"updatable",
"deletable",
]);
current.container[node.attributes.Name.value] = current.containerChild;
annotatable.target = current.containerChild;
const bindings = preV4.entitySet[node.attributes.Name.value];
Expand Down Expand Up @@ -1835,6 +1840,61 @@ module.exports.xml2json = function (
}
}

/**
* Set attributes from an XML node
* @param {Object} target The object to fill
* @param {Object} node The XML node
* @param {Array} convert An array of attribute names to convert
*/
const SAP_V2_URI = "http://www.sap.com/Protocols/SAPData";
const CAPABILITIES_URI =
"https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Capabilities.V1.json";
const CAPABILITIES = {
creatable: {
attrValue: "false",
term: "InsertRestrictions",
value: { Insertable: false },
},
updatable: {
attrValue: "false",
term: "UpdateRestrictions",
value: { Updatable: false },
},
deletable: {
attrValue: "false",
term: "DeleteRestrictions",
value: { Deletable: false },
},
};
function setCapabilities(target, node, convert) {
if (result.$Version > "2.0") return;
let includeCapabilities = false;
const attributes = Object.values(node.attributes);
for (const name of convert) {
const attribute = attributes.find(
(a) => a.local === name && a.uri === SAP_V2_URI
);
const anno = CAPABILITIES[name];
if (!attribute || !anno || attribute.value !== anno.attrValue) continue;
includeCapabilities = true;
target[`@Capabilities.${anno.term}`] = anno.value;
}

if (includeCapabilities) {
if (!result.$Reference) result.$Reference = {};
if (!result.$Reference[CAPABILITIES_URI]) {
result.$Reference[CAPABILITIES_URI] = {
$Include: [
{
$Namespace: "Org.OData.Capabilities.V1",
$Alias: "Capabilities",
},
],
};
}
}
}

/**
* Construct OData namespace from XML namespace URI
* @param {string} uri The XML namespace name
Expand Down
Loading

0 comments on commit e17284f

Please sign in to comment.