Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
## [Unreleased]

### Added
- Support `@Validation.MinItems` and `@Validation.MaxItems` annotations on collection-typed properties and action parameters to emit `minItems`/`maxItems` in the generated OpenAPI schema
### Changed
### Deprecated
### Removed
Expand Down
2 changes: 1 addition & 1 deletion lib/compile/csdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CDS_TERMS = Object.freeze({
Core: ['AcceptableMediaTypes', 'Computed', 'ComputedDefaultValue', 'DefaultNamespace', 'Description', 'Example', 'Immutable', 'LongDescription',
'MediaType', 'OptionalParameter', 'Permissions', 'SchemaVersion'],
JSON: ['Schema'],
Validation: ['AllowedValues', 'Exclusive', 'Maximum', 'Minimum', 'Pattern']
Validation: ['AllowedValues', 'Exclusive', 'Maximum', 'MaxItems', 'Minimum', 'MinItems', 'Pattern']
})

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/compile/csdl2openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
* @param {{ url?: string, servers?: object, odataVersion?: string, scheme?: string, host?: string, basePath?: string, diagram?: boolean, maxLevels?: number, shortActionPaths?: boolean }} options Optional parameters
* @return {object} OpenAPI description
*/
module.exports.csdl2openapi = function (

Check warning on line 93 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function has a complexity of 22. Maximum allowed is 15

Check warning on line 93 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function has a complexity of 22. Maximum allowed is 15

Check warning on line 93 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function has a complexity of 22. Maximum allowed is 15

Check warning on line 93 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function has a complexity of 22. Maximum allowed is 15

Check warning on line 93 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function has a complexity of 22. Maximum allowed is 15

Check warning on line 93 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function has a complexity of 22. Maximum allowed is 15
csdl,
{
url: serviceRoot,
Expand Down Expand Up @@ -472,7 +472,7 @@
* @param {number} options.level Number of navigation segments so far
* @param {string} options.navigationPath Path for finding navigation restrictions
*/
function pathItems({ paths, prefix, prefixParameters, element, root, sourceName, targetName, target, level, navigationPath }) {

Check warning on line 475 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'pathItems' has a complexity of 19. Maximum allowed is 15

Check warning on line 475 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'pathItems' has a complexity of 19. Maximum allowed is 15

Check warning on line 475 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'pathItems' has a complexity of 19. Maximum allowed is 15

Check warning on line 475 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'pathItems' has a complexity of 19. Maximum allowed is 15

Check warning on line 475 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'pathItems' has a complexity of 19. Maximum allowed is 15

Check warning on line 475 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'pathItems' has a complexity of 19. Maximum allowed is 15
const name = prefix.substring(prefix.lastIndexOf('/') + 1);
const type = meta.modelElement(element.$Type);
const pathItem = {};
Expand Down Expand Up @@ -750,7 +750,7 @@
* @param {boolean} options.byKey Read by key
* @param {array} options.nonExpandable Non-expandable navigation properties
*/
function operationRead({ pathItem, element, name, sourceName, targetName, target, level, restrictions, byKey, nonExpandable }) {

Check warning on line 753 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'operationRead' has a complexity of 31. Maximum allowed is 15

Check warning on line 753 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'operationRead' has a complexity of 31. Maximum allowed is 15

Check warning on line 753 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'operationRead' has a complexity of 31. Maximum allowed is 15

Check warning on line 753 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'operationRead' has a complexity of 31. Maximum allowed is 15

Check warning on line 753 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'operationRead' has a complexity of 31. Maximum allowed is 15

Check warning on line 753 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'operationRead' has a complexity of 31. Maximum allowed is 15
const targetRestrictions = target?.[meta.voc.Capabilities.ReadRestrictions];
const readRestrictions = restrictions.ReadRestrictions || targetRestrictions || {};
const readByKeyRestrictions = readRestrictions.ReadByKeyRestrictions;
Expand Down Expand Up @@ -1220,7 +1220,7 @@
* @param {object} options.restrictions Navigation property restrictions of navigation segment
* @param {boolean} [options.byKey=false] Update by key
*/
function operationUpdate({ pathItem, element, name, sourceName, target, level, restrictions, byKey = false }) {

Check warning on line 1223 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'operationUpdate' has a complexity of 16. Maximum allowed is 15

Check warning on line 1223 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'operationUpdate' has a complexity of 16. Maximum allowed is 15

Check warning on line 1223 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'operationUpdate' has a complexity of 16. Maximum allowed is 15

Check warning on line 1223 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'operationUpdate' has a complexity of 16. Maximum allowed is 15

Check warning on line 1223 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'operationUpdate' has a complexity of 16. Maximum allowed is 15

Check warning on line 1223 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'operationUpdate' has a complexity of 16. Maximum allowed is 15
const updateRestrictions = restrictions.UpdateRestrictions || target?.[meta.voc.Capabilities.UpdateRestrictions] || {};
const countRestrictions = target?.[meta.voc.Capabilities.CountRestrictions]?.Countable === false;
if (updateRestrictions.Updatable !== false && !element[meta.voc.Core.Immutable]) {
Expand Down Expand Up @@ -1311,7 +1311,7 @@
*/
function pathItemsForMediaStream({ paths, prefix, prefixParameters, type, name, sourceName }) {
if (type.$HasStream) {
const mediaTypes = type[meta.voc.Core.AcceptableMediaTypes]?.map(t => t['$EnumMember'] ?? t) ?? [];

Check warning on line 1314 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

["$EnumMember"] is better written in dot notation

Check warning on line 1314 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

["$EnumMember"] is better written in dot notation

Check warning on line 1314 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

["$EnumMember"] is better written in dot notation

Check warning on line 1314 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

["$EnumMember"] is better written in dot notation

Check warning on line 1314 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

["$EnumMember"] is better written in dot notation

Check warning on line 1314 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

["$EnumMember"] is better written in dot notation
const contentTypes = mediaTypes.length > 0 ? mediaTypes : ['*/*'];
const mediaContent = Object.fromEntries(contentTypes.map(ct => [ct, { schema: { type: 'string', format: 'binary' } }]));
const lname = splitName(name);
Expand Down Expand Up @@ -1641,7 +1641,7 @@
* @param {string} options.sourceName Name of path source
* @param {object} [options.actionImport={}] Action import
*/
function pathItemAction({ paths, prefix, prefixParameters, actionName, overload, sourceName, actionImport = {} }) {

Check warning on line 1644 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'pathItemAction' has a complexity of 18. Maximum allowed is 15

Check warning on line 1644 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'pathItemAction' has a complexity of 18. Maximum allowed is 15

Check warning on line 1644 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'pathItemAction' has a complexity of 18. Maximum allowed is 15

Check warning on line 1644 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'pathItemAction' has a complexity of 18. Maximum allowed is 15

Check warning on line 1644 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'pathItemAction' has a complexity of 18. Maximum allowed is 15

Check warning on line 1644 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'pathItemAction' has a complexity of 18. Maximum allowed is 15
const name = actionName.indexOf('.') === -1 ? actionName : nameParts(actionName).name;
const pathItem = {
post: {
Expand Down Expand Up @@ -1731,7 +1731,7 @@
* @param {string} options.sourceName Name of path source
* @param {object} [options.functionImport={}] Function Import
*/
function pathItemFunction({ paths, prefix, prefixParameters, functionName, overload, sourceName, functionImport = {} }) {

Check warning on line 1734 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'pathItemFunction' has a complexity of 16. Maximum allowed is 15

Check warning on line 1734 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'pathItemFunction' has a complexity of 16. Maximum allowed is 15

Check warning on line 1734 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'pathItemFunction' has a complexity of 16. Maximum allowed is 15

Check warning on line 1734 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'pathItemFunction' has a complexity of 16. Maximum allowed is 15

Check warning on line 1734 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'pathItemFunction' has a complexity of 16. Maximum allowed is 15

Check warning on line 1734 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'pathItemFunction' has a complexity of 16. Maximum allowed is 15
const name = functionName.indexOf('.') === -1 ? functionName : nameParts(functionName).name;
let parameters = overload.$Parameter || [];
if (overload.$IsBound) parameters = parameters.slice(1);
Expand All @@ -1740,7 +1740,7 @@

const implicitAliases = csdl.$Version > '4.0' || parameters.some(p => p[meta.voc.Core.OptionalParameter]);

parameters.forEach(p => {

Check warning on line 1743 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Arrow function has a complexity of 29. Maximum allowed is 15

Check warning on line 1743 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Arrow function has a complexity of 29. Maximum allowed is 15

Check warning on line 1743 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Arrow function has a complexity of 29. Maximum allowed is 15

Check warning on line 1743 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Arrow function has a complexity of 29. Maximum allowed is 15

Check warning on line 1743 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Arrow function has a complexity of 29. Maximum allowed is 15

Check warning on line 1743 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Arrow function has a complexity of 29. Maximum allowed is 15
const description = getDescriptionWithFallback(p);
/** @type {Parameter} */
const param = {
Expand Down Expand Up @@ -2120,7 +2120,7 @@
* @param {string} options.suffix Suffix for read/create/update
* @return {object} Map of Schemas Objects
*/
function schemasForStructuredType({ schemas, qualifier, name, type, suffix }) {

Check warning on line 2123 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'schemasForStructuredType' has a complexity of 18. Maximum allowed is 15

Check warning on line 2123 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'schemasForStructuredType' has a complexity of 18. Maximum allowed is 15

Check warning on line 2123 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'schemasForStructuredType' has a complexity of 18. Maximum allowed is 15

Check warning on line 2123 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Function 'schemasForStructuredType' has a complexity of 18. Maximum allowed is 15

Check warning on line 2123 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Function 'schemasForStructuredType' has a complexity of 18. Maximum allowed is 15

Check warning on line 2123 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Function 'schemasForStructuredType' has a complexity of 18. Maximum allowed is 15
const schemaName = `${qualifier}.${name}${suffix}`;
const baseName = `${qualifier}.${name}`;
const isKey = keyMap(type);
Expand All @@ -2136,7 +2136,7 @@
const properties = propertiesOfStructuredType(type);
const expandRestrictions = type[meta.voc.Capabilities.ExpandRestrictions] ?? {};
const nonExpandableProperties = expandRestrictions.NonExpandableProperties ?? [];
Object.keys(properties).forEach(iName => {

Check warning on line 2139 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Arrow function has a complexity of 27. Maximum allowed is 15

Check warning on line 2139 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Arrow function has a complexity of 27. Maximum allowed is 15

Check warning on line 2139 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Arrow function has a complexity of 27. Maximum allowed is 15

Check warning on line 2139 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 24

Arrow function has a complexity of 27. Maximum allowed is 15

Check warning on line 2139 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / lint

Arrow function has a complexity of 27. Maximum allowed is 15

Check warning on line 2139 in lib/compile/csdl2openapi.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Arrow function has a complexity of 27. Maximum allowed is 15
if (nonExpandableProperties.includes(iName)) {
return;
}
Expand Down Expand Up @@ -2574,7 +2574,7 @@
}
}

if (element[meta.voc.Validation.Maximum] != undefined) {
if (element[meta.voc.Validation.Maximum] !== undefined) {
if (s.$ref) s = { allOf: [s] };
if (isAnyOfSchema(s) && isNumberSchema(s.anyOf[0])) {
s.anyOf[0].maximum = element[meta.voc.Validation.Maximum];
Expand All @@ -2584,7 +2584,7 @@
if (element[meta.voc.Validation.Maximum + meta.voc.Validation.Exclusive]) s.exclusiveMaximum = true;
}

if (element[meta.voc.Validation.Minimum] != undefined) {
if (element[meta.voc.Validation.Minimum] !== undefined) {
if (s.$ref) s = { allOf: [s] };
if (isAnyOfSchema(s) && isNumberSchema(s.anyOf[0])) {
s.anyOf[0].minimum = element[meta.voc.Validation.Minimum];
Expand All @@ -2599,6 +2599,10 @@
type: 'array',
items: s
};
if (element[meta.voc.Validation.MinItems] !== undefined)
s.minItems = element[meta.voc.Validation.MinItems];
if (element[meta.voc.Validation.MaxItems] !== undefined)
s.maxItems = element[meta.voc.Validation.MaxItems];
}

const description = forParameter ? undefined : (element[meta.voc.Core.LongDescription] || element[meta.voc.Core.Description]);
Expand Down
4 changes: 3 additions & 1 deletion lib/compile/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ type BooleanSchema = {

type ArraySchema = {
type: 'array',
items: Schema
items: Schema,
minItems?: number,
maxItems?: number
}

type ObjectSchema = {
Expand Down
9 changes: 9 additions & 0 deletions test/lib/compile/data/annotations.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
"$Type": "Edm.Decimal",
"$Scale": 5
},
"MinMaxItems": {
"$Collection": true,
"@Validation.MinItems": 1,
"@Validation.MaxItems": 5
},
"MinItems": {
"$Collection": true,
"@Validation.MinItems": 2
},
"Example": {
"$Nullable": true,
"@Core.Example": {
Expand Down
41 changes: 41 additions & 0 deletions test/lib/compile/data/annotations.openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
"Minimum desc",
"MinimumExt",
"MinimumExt desc",
"MinMaxItems",
"MinMaxItems desc",
"MinItems",
"MinItems desc",
"ExampleExt",
"ExampleExt desc",
"Computed",
Expand Down Expand Up @@ -149,6 +153,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -250,6 +256,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -566,6 +574,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -867,6 +877,10 @@
"Minimum desc",
"MinimumExt",
"MinimumExt desc",
"MinMaxItems",
"MinMaxItems desc",
"MinItems",
"MinItems desc",
"Example",
"Example desc",
"ExampleExt",
Expand Down Expand Up @@ -905,6 +919,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -1006,6 +1022,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -1113,6 +1131,10 @@
"Minimum desc",
"MinimumExt",
"MinimumExt desc",
"MinMaxItems",
"MinMaxItems desc",
"MinItems",
"MinItems desc",
"Example",
"Example desc",
"ExampleExt",
Expand Down Expand Up @@ -1151,6 +1173,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -1252,6 +1276,8 @@
"MaximumExt",
"Minimum",
"MinimumExt",
"MinMaxItems",
"MinItems",
"Example",
"ExampleExt",
"Computed",
Expand Down Expand Up @@ -2351,6 +2377,21 @@
"x-sap-scale": 5,
"exclusiveMinimum": true
},
"MinMaxItems": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"maxItems": 5
},
"MinItems": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2
},
"Example": {
"type": "string",
"nullable": true,
Expand Down
Loading