Skip to content

Commit 3b5577f

Browse files
OpenAPI: Replace Console warnings to cds debug (#69)
* Add debug logs instead of console warn * Add debug logs instead of console warn * First commit
1 parent 83abdaf commit 3b5577f

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

lib/compile/csdl2openapi.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/**
22
* Converts OData CSDL JSON to OpenAPI 3.0.2
33
*/
4-
4+
const cds = require('@sap/cds');
55
var pluralize = require('pluralize')
6+
const DEBUG = cds.debug('openapi'); // Initialize cds.debug with the 'openapi'
7+
68

79
//TODO
810
// - Core.Example for complex types
@@ -325,7 +327,7 @@ module.exports.csdl2openapi = function (
325327
);
326328
}
327329
if (!element) {
328-
console.warn(`Invalid annotation target '${target}'`);
330+
DEBUG?.(`Invalid annotation target '${target}'`);
329331
} else if (Array.isArray(element)) {
330332
//TODO: action or function:
331333
//- loop over all overloads
@@ -348,12 +350,12 @@ module.exports.csdl2openapi = function (
348350
if (element[segments[1]]) {
349351
Object.assign(element[segments[1]], annotations);
350352
} else {
351-
// console.warn(`Invalid annotation target '${target}'`)
353+
// DEBUG?.(`Invalid annotation target '${target}'`)
352354
}
353355
}
354356
break;
355357
default:
356-
console.warn('More than two annotation target path segments');
358+
DEBUG?.('More than two annotation target path segments');
357359
}
358360
}
359361
});
@@ -655,7 +657,7 @@ module.exports.csdl2openapi = function (
655657
} else if (child.$Function) {
656658
pathItemFunctionImport(paths, name, child);
657659
} else {
658-
console.warn('Unrecognized entity container child: ' + name);
660+
DEBUG?.('Unrecognized entity container child: ' + name);
659661
}
660662
})
661663
if (resources.length > 0) pathItemBatch(paths, container);
@@ -1148,7 +1150,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
11481150
const elementType = modelElement(element.$Type);
11491151

11501152
if (!elementType) {
1151-
console.warn(`Unknown type for element: ${JSON.stringify(element)}`);
1153+
DEBUG?.(`Unknown type for element: ${JSON.stringify(element)}`);
11521154
return paths;
11531155
}
11541156

@@ -1160,7 +1162,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
11601162
.filter(entry => !modelElement(entry[1].$Type));
11611163

11621164
// Keep old logging
1163-
ignore.forEach(entry => console.warn(`Unknown type for element: ${JSON.stringify(entry)}`));
1165+
ignore.forEach(entry => DEBUG?.(`Unknown type for element: ${JSON.stringify(entry)}`));
11641166

11651167
const properties = Object.entries(propsOfType)
11661168
.filter(entry => entry[1].$Kind !== 'NavigationProperty')
@@ -1178,7 +1180,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
11781180

11791181
// Allow full cycle to be shown (0) times
11801182
if (property.typeRefChain.filter(_type => _type === typeRefChainTail).length > 1) {
1181-
console.warn(`Cycle detected ${property.typeRefChain.join('->')}`);
1183+
DEBUG?.(`Cycle detected ${property.typeRefChain.join('->')}`);
11821184
continue;
11831185
}
11841186

@@ -2394,7 +2396,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
23942396
break;
23952397
default:
23962398
if (element.$Type.startsWith('Edm.')) {
2397-
console.warn('Unknown type: ' + element.$Type);
2399+
DEBUG?.('Unknown type: ' + element.$Type);
23982400
} else {
23992401
let type = modelElement(element.$Type);
24002402
let isStructured = type && ['ComplexType', 'EntityType'].includes(type.$Kind);
@@ -2597,7 +2599,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
25972599
break;
25982600
default:
25992601
unknown = true
2600-
console.warn('Unknown Authorization type ' + qualifiedType);
2602+
DEBUG?.('Unknown Authorization type ' + qualifiedType);
26012603
}
26022604
if (!unknown) schemes[auth.Name] = scheme;
26032605
});
@@ -2619,7 +2621,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
26192621
const securitySchemes = entityContainer && entityContainer[voc.Authorization.SecuritySchemes] ? entityContainer[voc.Authorization.SecuritySchemes] : [];
26202622
// check if securitySchemas exist if it does not exist then throw a warning
26212623
if (securitySchemes.length === 0) {
2622-
console.warn('No security schemes defined in the entity container');
2624+
DEBUG?.('No security schemes defined in the entity container');
26232625
}
26242626
if (securitySchemes.length > 0) openapi.security = [];
26252627
securitySchemes.forEach(scheme => {
@@ -2661,5 +2663,5 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
26612663
function isIdentifier(name) {
26622664
return !name.startsWith('$') && !name.includes('@');
26632665
}
2664-
2666+
26652667
};

lib/compile/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const csdl2openapi = require('./csdl2openapi')
22
const cds = require('@sap/cds/lib');
33
const fs = require("fs");
4+
const DEBUG = cds.debug('openapi');
45

56
module.exports = function processor(csn, options = {}) {
67
const edmOptions = Object.assign({
@@ -157,7 +158,7 @@ function _getProtocols(csdl, csn, odataVersion) {
157158
if (protocol === "rest" || protocol === "odata" || protocol === "odata-v4") {
158159
protocols.push(protocol);
159160
} else {
160-
console.warn(`"${protocol}" protocol is not supported`);
161+
DEBUG?.(`"${protocol}" protocol is not supported`);
161162
}
162163
});
163164
}

test/lib/compile/csdl2openapi.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
16781678
assert.deepStrictEqual(actual.paths['/Categories'].get, expected.paths['/Categories'].get, 'GET Categories');
16791679
})
16801680

1681+
16811682
it('FilterRestrictions, NavigationRestrictions, SearchRestrictions, and SortRestrictions', function () {
16821683
const csdl = {
16831684
$Version: '4.01',
@@ -2289,7 +2290,6 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
22892290
"MaxLength"
22902291
);
22912292
});
2292-
22932293
})
22942294

22952295
it("AllowedValues on various Edm types", function () {
@@ -2380,6 +2380,14 @@ it("AllowedValues on various Edm types", function () {
23802380
assert.deepStrictEqual(messages, [], "messages");
23812381
})
23822382

2383+
it('Error Logging when name and title are missing', function () {
2384+
const csdl = {name:undefined,title:undefined};
2385+
const actual = lib.csdl2openapi(csdl, {});
2386+
2387+
assert.ok(actual, 'Function should execute without errors');
2388+
console.log('Error handling executed successfully');
2389+
});
2390+
23832391
describe('CAP / CS01', function () {
23842392

23852393
it('FilterRestrictions, NavigationRestrictions, and SortRestrictions', function () {
@@ -2541,6 +2549,7 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
25412549
assert.deepStrictEqual(operations(actual), operations(expected), 'Operations');
25422550
assert.deepStrictEqual(actual.paths['/things'].get, expected.paths['/things'].get, 'GET things');
25432551
})
2552+
25442553

25452554
})
25462555

@@ -2566,4 +2575,4 @@ function schemas(openapi) {
25662575
return Object.keys(openapi.components.schemas)
25672576
.sort()
25682577
.filter((s) => s.includes("."));
2569-
}
2578+
}

0 commit comments

Comments
 (0)