diff --git a/CHANGELOG.md b/CHANGELOG.md index c7be42e..1d9ed33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). ## [Unreleased] ### Added +- Added `defaultProtocol` configuration option as fallback if no config is provided. ### Changed +- set `odata` as the service protocol if no protocol is set to match the default behavior of `@sap/cds`. Provide `cds.env.openapi.defaultProtocol = "rest"` to restore old behavior. ### Deprecated ### Removed ### Fixed diff --git a/lib/compile/index.js b/lib/compile/index.js index 1bfb40f..09753f3 100644 --- a/lib/compile/index.js +++ b/lib/compile/index.js @@ -51,7 +51,7 @@ function processor(csn, options = {}) { } const openApiOptions = toOpenApiOptions(csdl, csn, options); const serviceName = csdl.$EntityContainer.replace(/\.[^.]+$/, ""); - openApiDocs = _getOpenApi(csdl, openApiOptions,serviceName); + openApiDocs = _getOpenApi(csdl, openApiOptions, serviceName); return Object.keys(openApiDocs).length === 1 ? openApiDocs[serviceName] : _iterate(openApiDocs); @@ -118,11 +118,12 @@ function toOpenApiOptions(csdl, csn, options = {}) { } const envOptions = cds.env.openapi instanceof Object && !Array.isArray(cds.env.openapi) ? cds.env.openapi : {}; + envOptions.defaultProtocol = envOptions.defaultProtocol ?? "odata"; const fileOptions = _readConfigFile(callerOptions["config-file"]); const result = { ...envOptions, ...fileOptions, ...callerOptions }; delete result["config-file"]; - const protocols = _getProtocols(csdl, csn, result.odataVersion); + const protocols = _getProtocols(csdl, csn, result.odataVersion, result.defaultProtocol); if (result.url) { const servicePaths = _servicePath(csdl, csn, protocols); @@ -143,20 +144,20 @@ function toOpenApiOptions(csdl, csn, options = {}) { return result; } -function _getProtocols(csdl, csn, odataVersion) { +function _getProtocols(csdl, csn, odataVersion, defaultProtocol) { if (csdl.$EntityContainer) { const serviceName = csdl.$EntityContainer.replace(/\.[^.]+$/, ""); const service = csn.definitions[serviceName]; const protocols = []; if(odataVersion === "4.01"){ - protocols.push("rest"); + protocols.push("odata"); } else if(odataVersion === "4.0"){ protocols.push("odata"); } else if (!service["@protocol"]) { - protocols.push("rest"); //taking rest as default in case no relevant protocol is there + protocols.push(defaultProtocol); // matches cds's default protocol when none is annotated } else if (service["@protocol"] === "none") { // if @protocol is 'none' then throw an error throw new Error( diff --git a/test/lib/compile/openapi.test.js b/test/lib/compile/openapi.test.js index 79e5374..a74f7be 100644 --- a/test/lib/compile/openapi.test.js +++ b/test/lib/compile/openapi.test.js @@ -262,6 +262,36 @@ service CatalogService { assert(openapi.servers[0].url.includes('odata')); }); + test('default service without @protocol renders server URL with odata/v4', () => { + const csn = cds.compile.to.csn(` + @path: 'catalog' + service CatalogService { + entity Books { + key ID : Integer; + title : String; + } + }` + ); + const openapi = toOpenApi(csn); + assert(openapi.servers[0].url.includes('odata/v4')); + assert(!openapi.servers[0].url.includes('rest')); + }); + + test('defaultProtocol overrides OData for a service without @protocol', () => { + const csn = cds.compile.to.csn(` + @path: 'catalog' + service CatalogService { + entity Books { + key ID : Integer; + title : String; + } + }` + ); + const openapi = toOpenApi(csn, { 'openapi:defaultProtocol': 'rest' }); + assert(openapi.servers[0].url.includes('rest/catalog')); + assert(!openapi.servers[0].url.includes('odata')); + }); + test('options: Multiple servers', () => { const csn = cds.compile.to.csn(` service A {entity E { key ID : UUID; };};`