Skip to content

Commit 5786254

Browse files
Parameter aliases conflict with system query options (#66)
* oasis-tcs/odata-openapi#324 * Test case * Don't change indentation style --------- Co-authored-by: Dipto <[email protected]>
1 parent 10e0b09 commit 5786254

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

lib/compile/csdl2openapi.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ const TITLE_SUFFIX = {
3232
"-update": " (for update)",
3333
};
3434

35+
const SYSTEM_QUERY_OPTIONS = [
36+
"compute",
37+
"expand",
38+
"select",
39+
"filter",
40+
"search",
41+
"count",
42+
"orderby",
43+
"skip",
44+
"top",
45+
"format",
46+
"index",
47+
"schemaversion",
48+
"skiptoken",
49+
"apply",
50+
];
51+
3552
/**
3653
* ODM annotations in CDS that should be converted into OpenAPI.
3754
*/
@@ -1646,7 +1663,13 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
16461663
|| type && ['ComplexType', 'EntityType'].includes(type.$Kind)
16471664
|| type && type.$UnderlyingType == 'Edm.Stream') {
16481665
param.in = 'query';
1649-
if (implicitAliases) {
1666+
if (
1667+
implicitAliases &&
1668+
csdl.$Version !== '2.0' &&
1669+
SYSTEM_QUERY_OPTIONS.includes(p.$Name.toLowerCase())
1670+
) {
1671+
param.name = '@' + p.$Name;
1672+
} else if (implicitAliases) {
16501673
param.name = p.$Name;
16511674
} else {
16521675
pathSegments.push(p.$Name + '=@' + p.$Name);
@@ -1669,7 +1692,14 @@ see [Expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-prot
16691692
pathSegments.push(p.$Name + "={" + p.$Name + "}");
16701693
param.in = 'path';
16711694
}
1672-
param.name = p.$Name;
1695+
if (
1696+
implicitAliases &&
1697+
csdl.$Version !== '2.0' &&
1698+
SYSTEM_QUERY_OPTIONS.includes(p.$Name.toLowerCase())
1699+
)
1700+
param.name = '@' + p.$Name;
1701+
else
1702+
param.name = p.$Name;
16731703
if (!p.$Type || p.$Type === "Edm.String" || (type && (!type.$Type || type.$Type === "Edm.String"))) {
16741704
if (description) param.description += " \n";
16751705
else param.description = "";

test/lib/compile/csdl2openapi.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,75 @@ describe('Edge cases', function () {
814814
assert.deepStrictEqual(actual.paths[path].get.tags, expected.paths[path].get.tags, 'function tags');
815815
})
816816

817+
it('function with @ parameter aliases', function () {
818+
const csdl = {
819+
$Version: '4.01',
820+
$Reference: {
821+
dummy: {
822+
$Include: [{ $Namespace: 'Org.OData.Core.V1', $Alias: 'Core' }],
823+
},
824+
},
825+
$EntityContainer: 'model.Container',
826+
model: {
827+
$Alias: 'this',
828+
FavoritePhotos: [
829+
{
830+
$Kind: 'Function',
831+
$Parameter: [
832+
{
833+
$Name: 'SKIP',
834+
$Type: 'Edm.Date',
835+
$Collection: true,
836+
'@Core.Description': 'Dates to be skipped',
837+
},
838+
{
839+
$Name: 'filter',
840+
'@Core.Description': 'Boolean expression to filter the result',
841+
},
842+
],
843+
$ReturnType: {},
844+
},
845+
],
846+
Container: {
847+
fav: { $Function: 'this.FavoritePhotos' },
848+
},
849+
},
850+
};
851+
const expected = {
852+
paths: {
853+
'/fav': {
854+
get: {
855+
parameters: [
856+
{
857+
name: '@SKIP',
858+
in: 'query',
859+
required: true,
860+
description:
861+
'Dates to be skipped \nThis is a URL-encoded JSON array with items of type Edm.Date, see [Complex and Collection Literals](https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_ComplexandCollectionLiterals)',
862+
schema: { type: 'string' },
863+
example: '[]',
864+
},
865+
{
866+
name: '@filter',
867+
in: 'query',
868+
required: true,
869+
schema: { type: 'string', pattern: "^'([^']|'')*'$" },
870+
description:
871+
'Boolean expression to filter the result \nString value needs to be enclosed in single quotes',
872+
},
873+
],
874+
summary: 'Invokes function FavoritePhotos',
875+
tags: ['Service Operations'],
876+
},
877+
},
878+
},
879+
};
880+
const actual = lib.csdl2openapi(csdl, { diagram: true });
881+
delete actual.paths['/$batch'];
882+
delete actual.paths['/fav'].get.responses;
883+
assert.deepStrictEqual(actual.paths, expected.paths);
884+
});
885+
817886
it('return type with facets', function () {
818887
const csdl = {
819888
$EntityContainer: 'this.Container',

0 commit comments

Comments
 (0)