Skip to content

Commit 7a9bc1c

Browse files
Add AMF model expander (#42)
* chore(deps): add amf-json-ld-lib to deps * chore(deps): update api-model-generator * feat: expand amf model if it is in flattened form * chore(test): add flattened api * test: add test for flattened model assigning * chore(deps): update deps * 4.4.0 * chore(deps): update api-model-generator * fix: expand model manually, keep old flattened model to avoid re-renders * test: add expanded-api, add deep equal test for flattened model * chore: remove debugger * chore: remove console.log * fix: move oldFlattened check to avoid incorrect setter cut-offs
1 parent 5c4fcb9 commit 7a9bc1c

File tree

7 files changed

+252
-132
lines changed

7 files changed

+252
-132
lines changed

package-lock.json

Lines changed: 161 additions & 124 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@api-components/amf-helper-mixin",
33
"description": "A mixin with common functions user by most AMF components to compute AMF values",
4-
"version": "4.3.12",
4+
"version": "4.4.0",
55
"license": "Apache-2.0",
66
"main": "index.js",
77
"module": "index.js",
@@ -29,20 +29,20 @@
2929
"email": "[email protected]"
3030
},
3131
"devDependencies": {
32-
"@api-components/api-model-generator": "^0.2.11",
32+
"@api-components/api-model-generator": "^0.2.13",
3333
"@open-wc/eslint-config": "^4.3.0",
3434
"@open-wc/testing": "^2.5.33",
3535
"@web/dev-server": "^0.1.17",
36-
"@web/test-runner": "^0.13.5",
36+
"@web/test-runner": "^0.13.12",
3737
"@web/test-runner-playwright": "^0.8.6",
38-
"eslint": "^7.28.0",
38+
"eslint": "^7.29.0",
3939
"eslint-config-prettier": "^8.3.0",
4040
"husky": "^6.0.0",
4141
"lint-staged": "^11.0.0",
4242
"lit-element": "^2.5.1",
4343
"lit-html": "^1.4.1",
4444
"sinon": "^11.1.1",
45-
"typescript": "^4.3.2",
45+
"typescript": "^4.3.4",
4646
"typescript-lit-html-plugin": "^0.9.0"
4747
},
4848
"scripts": {
@@ -85,5 +85,8 @@
8585
"*.js": [
8686
"eslint --fix"
8787
]
88+
},
89+
"dependencies": {
90+
"amf-json-ld-lib": "0.0.13"
8891
}
8992
}

src/AmfHelperMixin.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
License for the specific language governing permissions and limitations under
1212
the License.
1313
*/
14+
// @ts-ignore
15+
import { AmfModelExpander, JsonLdOptions, JsonLd } from 'amf-json-ld-lib'
1416
import { ns } from './Namespace.js';
1517

1618
/** @typedef {import('./Namespace').ns} Namespace */
@@ -78,11 +80,23 @@ export const AmfHelperMixin = (base) => class extends base {
7880
if (old === value) {
7981
return;
8082
}
83+
let expanded;
84+
if (!value || AmfModelExpander.isInExpandedForm(value)) {
85+
this._flattenedAmf = undefined;
86+
expanded = value;
87+
} else {
88+
const oldFlattened = this._flattenedAmf;
89+
if (oldFlattened === value) {
90+
return;
91+
}
92+
this._flattenedAmf = value;
93+
expanded = this._expand(value);
94+
}
8195
// Cached keys cannot be static as this element can be using in the sane
8296
// document with different AMF models
8397
this.__cachedKeys = {};
84-
this._amf = value;
85-
this.__amfChanged(value);
98+
this._amf = expanded;
99+
this.__amfChanged(expanded);
86100
if (this.requestUpdate) {
87101
this.requestUpdate('amf', old);
88102
}
@@ -98,6 +112,22 @@ export const AmfHelperMixin = (base) => class extends base {
98112
/* eslint-disable-next-line no-unused-vars */
99113
__amfChanged(amf) {}
100114

115+
/**
116+
* Expands flattened AMF model
117+
* @param {Object} amf
118+
*/
119+
_expand(amf) {
120+
AmfModelExpander.preprocessLegacyRootNodeId(amf)
121+
const linkEmbeddingFilter = key => !key.endsWith("fixPoint")
122+
const rootNode = amf['@context'] ? '' : "amf://id";
123+
const options = JsonLdOptions.apply()
124+
.withEmbeddedLinks(linkEmbeddingFilter)
125+
.withCompactedIris()
126+
.withExpandedStructure()
127+
.withRootNode(rootNode)
128+
return JsonLd.process(amf, options)
129+
}
130+
101131
/**
102132
* Returns compact model key for given value.
103133
* @param {string} property AMF original property

test/amf-helper-mixin.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, fixture, html } from '@open-wc/testing';
1+
import { assert, fixture, html, nextFrame } from '@open-wc/testing';
22
import sinon from 'sinon';
33
import { AmfLoader } from './amf-loader.js';
44
import './test-element.js';
@@ -2466,6 +2466,38 @@ describe('AmfHelperMixin', () => {
24662466
assert.deepEqual(element.__cachedKeys, {});
24672467
});
24682468
});
2469+
2470+
describe('Expander', async () => {
2471+
const flattenedApi = 'flattened-api'
2472+
const expandedApi = 'expanded-api'
2473+
let flattenedModel;
2474+
let expandedModel;
2475+
2476+
before(async () => {
2477+
flattenedModel = await AmfLoader.load(compact, flattenedApi);
2478+
expandedModel = await AmfLoader.load(compact, expandedApi);
2479+
});
2480+
2481+
beforeEach(async () => {
2482+
element = await basicFixture();
2483+
});
2484+
2485+
it('should not call __amfChanged again if same flattened model is set', async () => {
2486+
element.amf = flattenedModel;
2487+
await nextFrame();
2488+
const spy = sinon.spy(element, '__amfChanged');
2489+
element.amf = flattenedModel;
2490+
await nextFrame();
2491+
assert.isTrue(spy.notCalled);
2492+
});
2493+
2494+
it('should create same object for flattened as originial expanded', async () => {
2495+
const expandedElement = await modelFixture(expandedModel);
2496+
element.amf = flattenedModel;
2497+
await nextFrame();
2498+
assert.deepEqual(element.amf, expandedElement.amf)
2499+
});
2500+
});
24692501
});
24702502
});
24712503
});

test/apis.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"SE-12957/SE-12957.json": "OAS 2.0",
44
"multiple-servers/multiple-servers.yaml": { "type": "OAS 3.0", "mime": "application/yaml" },
55
"async-api/async-api.yaml": "ASYNC 2.0",
6+
"flattened-api/flattened-api.raml": { "type": "RAML 1.0", "mime": "application/yaml", "flattened": true },
7+
"expanded-api/expanded-api.raml": { "type": "RAML 1.0", "mime": "application/yaml", "flattened": false },
68
"src": "test",
79
"dest": "test"
810
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#%RAML 1.0
2+
title: Simple API
3+
4+
/people:
5+
get:
6+
post:
7+
put:
8+
patch:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#%RAML 1.0
2+
title: Simple API
3+
4+
/people:
5+
get:
6+
post:
7+
put:
8+
patch:

0 commit comments

Comments
 (0)