Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @EntityRelationship annotation #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions lib/compile/csdl2openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ const ODM_ANNOTATIONS = Object.freeze(
'@ODM.oid': 'x-sap-odm-oid'
});

const ER_ANNOTATION_PREFIX = '@EntityRelationship'
const ER_ANNOTATIONS = Object.freeze(
{
'@EntityRelationship.entityType': 'x-entity-relationship-entity-type',
'@EntityRelationship.entityIds': 'x-entity-relationship-entity-ids',
'@EntityRelationship.propertyType': 'x-entity-relationship-property-type',
'@EntityRelationship.reference': 'x-entity-relationship-reference',
'@EntityRelationship.compositeReferences': 'x-entity-relationship-composite-references',
'@EntityRelationship.temporalIds': 'x-entity-relationship-temporal-ids',
'@EntityRelationship.temporalReferences': 'x-entity-relationship-temporal-references',
'@EntityRelationship.referencesWithConstantIds': 'x-entity-relationship-references-with-constant-ids'
});

/**
* Construct an OpenAPI description from a CSDL document
* @param {object} csdl CSDL document
Expand Down Expand Up @@ -106,7 +119,7 @@ module.exports.csdl2openapi = function (
if (externalDocs && Object.keys(externalDocs).length > 0) {
openapi.externalDocs = externalDocs;
}

if (!csdl.$EntityContainer) {
delete openapi.servers;
delete openapi.tags;
Expand Down Expand Up @@ -467,18 +480,18 @@ module.exports.csdl2openapi = function (
function getServers(serviceRoot, serversObject, csdl) {
let servers;

if (serversObject && csdl.$EntityContainer ) {
if (serversObject && csdl.$EntityContainer) {
try {
servers = JSON.parse(serversObject);
// if csdl.$Version is 4.01 then protocol is rest if it is less than 4.01 then protocol is odata
const protocol = csdl.$Version <= "4.01" ? "odata/v4" : "rest";
const serviceName = nameParts(csdl.$EntityContainer).qualifier;
// append /protocol/{$serviceName} to the URL
servers.forEach((server) => {
server.url = server.url + "/" + protocol + "/" + serviceName;
});
servers = JSON.parse(serversObject);
// if csdl.$Version is 4.01 then protocol is rest if it is less than 4.01 then protocol is odata
const protocol = csdl.$Version <= "4.01" ? "odata/v4" : "rest";
const serviceName = nameParts(csdl.$EntityContainer).qualifier;
// append /protocol/{$serviceName} to the URL
servers.forEach((server) => {
server.url = server.url + "/" + protocol + "/" + serviceName;
});
} catch (err) {
throw new Error(`The input server object is invalid.`);
throw new Error(`The input server object is invalid.`);
}

if (!servers.length) {
Expand All @@ -497,7 +510,7 @@ module.exports.csdl2openapi = function (
*/
function getTags(container) {
const tags = new Map();
// all entity sets and singletons
// all entity sets and singletons
Object.keys(container)
.filter(name => isIdentifier(name) && container[name].$Type)
.forEach(child => {
Expand Down Expand Up @@ -1902,6 +1915,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot

if (suffix === SUFFIX.read && type["@ODM.root"]) schemas[schemaName]["x-sap-root-entity"] = type["@ODM.root"]
odmExtensions(type, schemas[schemaName]);
erExtensions(type, schemas[schemaName]);

if (suffix === SUFFIX.create && required.length > 0)
schemas[schemaName].required = [...new Set(required)];
Expand Down Expand Up @@ -1931,6 +1945,17 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
}
}

/**
* Add entity relationship extensions to OpenAPI schema for a structured type
* @param {object} type Structured type
* @param {object} schema OpenAPI schema to augment
*/
function erExtensions(type, schema) {
for (const [annotation, openApiExtension] of Object.entries(ER_ANNOTATIONS)) {
if (type[annotation]) schema[openApiExtension] = type[annotation];
}
}

/**
* Collect all properties of a structured type along the inheritance hierarchy
* @param {object} type Structured type
Expand Down Expand Up @@ -2302,6 +2327,12 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
if (element['@ODM.oidReference']?.entityName) {
s['x-sap-odm-oid-reference-entity-name'] = element['@ODM.oidReference'].entityName
}

for (const key in element) {
if (key.startsWith(ER_ANNOTATION_PREFIX) && ER_ANNOTATIONS[key]) {
s[ER_ANNOTATIONS[key]] = element[key];
}
}
return s;
}

Expand Down
Loading
Loading