Skip to content

Commit 97cd38f

Browse files
committed
fix an issue with model generation
1 parent 831221f commit 97cd38f

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

src/metamodel.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ function initConfiguration(name, type, isMethod) {
12271227
};
12281228
}
12291229
break;
1230-
case typeof type === 'array' && typeof type[0] === 'boolean':
1230+
case Array.isArray(type) && type[0] === 'boolean':
12311231
if (isMethod) {
12321232
result = {
12331233
name: name,
@@ -1244,7 +1244,7 @@ function initConfiguration(name, type, isMethod) {
12441244
};
12451245
}
12461246
break;
1247-
case typeof type === 'array' && typeof type[0] === 'string':
1247+
case Array.isArray(type) && type[0] === 'string':
12481248
if (isMethod) {
12491249
result = {
12501250
name: name,
@@ -1261,7 +1261,7 @@ function initConfiguration(name, type, isMethod) {
12611261
};
12621262
}
12631263
break;
1264-
case typeof type === 'array' && typeof type[0] === 'number':
1264+
case Array.isArray(type) && type[0] === 'number':
12651265
if (isMethod) {
12661266
result = {
12671267
name: name,
@@ -1278,7 +1278,7 @@ function initConfiguration(name, type, isMethod) {
12781278
};
12791279
}
12801280
break;
1281-
case typeof type === 'array' && typeof type[0] === 'object':
1281+
case Array.isArray(type) && type[0] === 'object':
12821282
if (isMethod) {
12831283
result = {
12841284
name: name,
@@ -1295,7 +1295,7 @@ function initConfiguration(name, type, isMethod) {
12951295
};
12961296
}
12971297
break;
1298-
case typeof type === 'array' && typeof type[0] === 'date':
1298+
case Array.isArray(type) && type[0] === 'date':
12991299
if (isMethod) {
13001300
result = {
13011301
name: name,
@@ -1312,7 +1312,7 @@ function initConfiguration(name, type, isMethod) {
13121312
};
13131313
}
13141314
break;
1315-
case typeof type === 'array' && typeof type[0] === 'any':
1315+
case Array.isArray(type) && type[0] === 'any':
13161316
if (isMethod) {
13171317
result = {
13181318
name: name,
@@ -1329,7 +1329,7 @@ function initConfiguration(name, type, isMethod) {
13291329
};
13301330
}
13311331
break;
1332-
case typeof type === 'array':
1332+
case Array.isArray(type):
13331333
if (isMethod) {
13341334
result = {
13351335
name: name,
@@ -1370,7 +1370,8 @@ function generateConfiguration(model) {
13701370
if (model.hasOwnProperty(propName) && propName.indexOf('_') !== 0) {
13711371
switch (true) {
13721372
// property type
1373-
case typeof model[propName] === 'string':
1373+
case typeof model[propName] === 'string' ||
1374+
Array.isArray(model[propName]):
13741375
model[propName] = initConfiguration(propName, model[propName], false);
13751376
break;
13761377

@@ -1825,7 +1826,11 @@ exports.isValidType = function isValidType(value, typeName) {
18251826
isValid = checkCustomSchema(value[i], typeName[0]);
18261827
break;
18271828
case exports.isClassName(typeName[0]):
1828-
if (value[i] !== '' && value[i] !== null) {
1829+
if (
1830+
value[i] !== '' &&
1831+
value[i] !== null &&
1832+
typeof value[i] !== 'string'
1833+
) {
18291834
isValid = exports.inheritFrom(
18301835
getClassName(value[i]),
18311836
typeName[0]
@@ -1864,7 +1869,7 @@ exports.isValidType = function isValidType(value, typeName) {
18641869
}
18651870
break;
18661871
case exports.isClassName(typeName):
1867-
if (value !== '' && value !== null) {
1872+
if (value !== '' && value !== null && typeof value !== 'string') {
18681873
result = exports.inheritFrom(getClassName(value), typeName);
18691874
}
18701875
break;

test/runtime/metamodel-spec.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('System Runtime metamodel component', () => {
8585
'name': 'address',
8686
'type': 'object',
8787
'schema': {
88-
'planet': {'type': 'string', 'mandatory': true}
88+
'planet': { 'type': 'string', 'mandatory': true }
8989
}
9090
});
9191

@@ -194,7 +194,7 @@ describe('System Runtime metamodel component', () => {
194194
'name': 'vegetable',
195195
'type': 'object',
196196
'schema': {
197-
'kind': {'type': ['string'], 'mandatory': true}
197+
'kind': { 'type': ['string'], 'mandatory': true }
198198
}
199199
});
200200

@@ -252,15 +252,15 @@ describe('System Runtime metamodel component', () => {
252252
'name': 'foods',
253253
'type': 'object',
254254
'schema': {
255-
'vegetables': {'type': 'vegetable', 'mandatory': true}
255+
'vegetables': { 'type': 'vegetable', 'mandatory': true }
256256
}
257257
});
258258

259259
metamodel.type({
260260
'name': 'vegetable',
261261
'type': 'object',
262262
'schema': {
263-
'kind': {'type': ['string'], 'mandatory': true}
263+
'kind': { 'type': ['string'], 'mandatory': true }
264264
}
265265
});
266266

@@ -559,4 +559,44 @@ describe('System Runtime metamodel component', () => {
559559

560560
expect(leia.father().son().firstName()).equal('Luke');
561561
});
562+
563+
it('can add a type (simple mode)', () => {
564+
const metamodel = runtime.require('metamodel');
565+
566+
// create the schema
567+
metamodel.schema('PersonSimple', {
568+
'firstName': 'property',
569+
'lastName': 'property',
570+
'age': 'property',
571+
'father': 'link',
572+
'mother': 'link',
573+
'children': 'collection',
574+
'fullName': 'method'
575+
});
576+
577+
// override the generated model
578+
metamodel.model('PersonSimple', {
579+
'firstName': 'string',
580+
'lastName': 'string',
581+
'age': 'number',
582+
'father': 'Person',
583+
'mother': 'Person',
584+
'children': ['Person'],
585+
'fullName': {
586+
'=>': 'string'
587+
}
588+
});
589+
590+
// create the model and related class
591+
metamodel.create();
592+
593+
const PersonSimple = runtime.require('PersonSimple');
594+
const person = new PersonSimple({
595+
firstName: 'a first name',
596+
mother: '',
597+
children: []
598+
});
599+
600+
expect(person.fullName()).equal(undefined);
601+
});
562602
});

0 commit comments

Comments
 (0)