Skip to content

Commit

Permalink
Deep-update support on container level (#68)
Browse files Browse the repository at this point in the history
* Deep-update support on container level

* Deep update means "create" for related entities
  • Loading branch information
ralfhandl authored Mar 3, 2020
1 parent c8f9767 commit 4c6b6e9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/csdl2openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports.csdl2openapi = function (

const entityContainer = csdl.$EntityContainer ? modelElement(csdl.$EntityContainer) : {};
const keyAsSegment = entityContainer[voc.Capabilities.KeyAsSegmentSupported];
const deepUpdate = entityContainer[voc.Capabilities.DeepUpdateSupport] && entityContainer[voc.Capabilities.DeepUpdateSupport].Supported;

const openapi = {
openapi: '3.0.2',
Expand Down Expand Up @@ -174,7 +175,7 @@ module.exports.csdl2openapi = function (
function vocabularies(voc, alias) {
const terms = {
Authorization: ['Authorizations', 'SecuritySchemes'],
Capabilities: ['BatchSupported', 'ChangeTracking', 'CountRestrictions', 'DeleteRestrictions', 'ExpandRestrictions',
Capabilities: ['BatchSupported', 'ChangeTracking', 'CountRestrictions', 'DeleteRestrictions', 'DeepUpdateSupport', 'ExpandRestrictions',
'FilterRestrictions', 'IndexableByKey', 'InsertRestrictions', 'KeyAsSegmentSupported', 'NavigationRestrictions',
'SearchRestrictions', 'SelectSupport', 'SkipSupported', 'SortRestrictions',
'ReadRestrictions', 'TopSupported', 'UpdateRestrictions'],
Expand Down Expand Up @@ -1481,6 +1482,8 @@ module.exports.csdl2openapi = function (
allProperties[name] = schema(property);
if (property.$Kind == 'NavigationProperty') {
creProperties[name] = schema(property, '-create');
if (deepUpdate)
updProperties[name] = schema(property, '-create');
} else {
if (property[voc.Core.Permissions] == 'Read' || property[voc.Core.Computed]) {
let index = required.indexOf(name);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "odata-openapi",
"version": "0.4.1",
"version": "0.5.0",
"description": "Convert OData CSDL XML or CSDL JSON to OpenAPI",
"homepage": "https://github.com/oasis-tcs/odata-openapi/blob/master/lib/README.md",
"bugs": "https://github.com/oasis-tcs/odata-openapi/issues",
Expand Down
76 changes: 76 additions & 0 deletions test/csdl2openapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,82 @@ describe('Edge cases', function () {
assert.deepStrictEqual(actual.paths['/roots/act'].post, expected.paths['/roots/act'].post, 'POST /roots/act');
})

it('Deep update on container level', function () {
const csdl = {
$Reference: { dummy: { "$Include": [{ "$Namespace": "Org.OData.Capabilities.V1", "$Alias": "Capabilities" }] } },
$EntityContainer: 'this.Container',
this: {
root: {
$Kind: 'EntityType', $Key: ['key'],
key: {},
one: {},
two: {},
children: { $Type: 'this.child', $Kind: 'NavigationProperty', $ContainsTarget: true, $Collection: true },
},
child: {
$Kind: 'EntityType', $Key: ['key'],
key: {},
one: {},
two: {},
nav: { $Type: 'this.grandchild', $Kind: 'NavigationProperty', $ContainsTarget: true },
},
grandchild: {
$Kind: 'EntityType', $Key: ['key'],
key: {},
one: {},
two: {}
},
Container: {
'@Capabilities.KeyAsSegmentSupported': true,
'@Capabilities.DeepUpdateSupport': { Supported: true },
roots: {
$Type: 'this.root', $Collection: true
}
}
}
};

const expected = {
paths: {
"/$batch": { post: {} },
"/roots": { get: {}, post: {} },
"/roots/{key}": { get: {}, patch: {}, delete: {} },
"/roots/{key}/children": { get: {}, post: {} },
"/roots/{key}/children/{key-1}": { get: {}, patch: {}, delete: {} },
"/roots/{key}/children/{key-1}/nav": { get: {}, patch: {} },
},
components: {
schemas: {
'this.root-update': {
type: 'object',
title: 'root (for update)',
properties: {
one: { type: 'string' }, two: { type: 'string' },
children: { type: 'array', items: { $ref: '#/components/schemas/this.child-create' } }
}
},
'this.child-update': {
type: 'object',
title: 'child (for update)',
properties: {
one: { type: 'string' }, two: { type: 'string' },
nav: { $ref: '#/components/schemas/this.grandchild-create' }
}
}
}
}
}

const actual = lib.csdl2openapi(csdl, {});

assert.deepStrictEqual(paths(actual), paths(expected), 'Paths');
assert.deepStrictEqual(operations(actual), operations(expected), 'Operations');

//TODO: check components
assert.deepStrictEqual(actual.components.schemas['this.root-update'], expected.components.schemas['this.root-update'], 'root update structure')
assert.deepStrictEqual(actual.components.schemas['this.child-update'], expected.components.schemas['this.child-update'], 'child update structure')
})

})

function check(actual, expected) {
Expand Down

0 comments on commit 4c6b6e9

Please sign in to comment.