Skip to content

Commit

Permalink
Merge 0de44ea into 3dddd1e
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitij79 committed Mar 25, 2024
2 parents 3dddd1e + 0de44ea commit 0a67f50
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/concerto-core/lib/serializer/jsonpopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,18 @@ class JSONPopulator {
* @private
*/
processMapType(mapDeclaration, parameters, value, type) {
let decl = mapDeclaration.getModelFile()
.getAllDeclarations()
.find(decl => decl.name === type);
let decl;
if (value && typeof value === 'object' && value.$class) {
// Use the $class property to find the class declaration
decl = mapDeclaration.getModelFile()
.getAllDeclarations()
.find(decl => decl.getFullyQualifiedName() === value.$class);
} else {
// Fallback to the original type lookup if value is not an object or doesn't have $class
decl = mapDeclaration.getModelFile()
.getAllDeclarations()
.find(decl => decl.name === type);
}

// if its a ClassDeclaration, populate the Concept.
if (decl?.isClassDeclaration()) {
Expand Down
37 changes: 37 additions & 0 deletions packages/concerto-core/test/serializer/jsonpopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ describe('JSONPopulator', () => {
o String assetId
}
`);
modelManager.addCTOModel(`
namespace org.acme.abstract
abstract asset Asset3 {
o String assetId
}
asset Asset4 extends Asset3 {}
map AssetByName {
o String
o Asset3
}
concept MyContainerAsset3 {
o AssetByName assetByName
}
`);
assetDeclaration1 = modelManager.getType('org.acme.MyContainerAsset1').getProperty('myAsset');
relationshipDeclaration1 = modelManager.getType('org.acme.MyTx1').getProperty('myAsset');
relationshipDeclaration2 = modelManager.getType('org.acme.MyTx2').getProperty('myAssets');
Expand Down Expand Up @@ -479,6 +493,29 @@ describe('JSONPopulator', () => {
jsonPopulator.visit(modelManager.getType('org.acme.MyContainerAsset2'), options);
}).should.throw(/Expected value at path `\$.rootObj.myAssets\[0\].assetValue` to be of type `Integer`/);
});

it('should be able to deserialise a map that uses abstract types as values', () => {
let options = {
jsonStack: new TypedStack({
$class: 'org.acme.abstract.MyContainerAsset3',
assetByName: {
'asset3': {
$class: 'org.acme.abstract.Asset4'
}
}
}),
resourceStack: new TypedStack({}),
factory: mockFactory,
modelManager: modelManager
};

let mockResource1 = sinon.createStubInstance(Resource);
mockFactory.newResource.withArgs('org.acme.abstract', 'MyAsset4', 'asset3').returns(mockResource1);
(() => {
jsonPopulator.visit(modelManager.getType('org.acme.abstract.MyContainerAsset3'), options);
}).should.not.throw();
});

});

describe('#visitField', () => {
Expand Down

0 comments on commit 0a67f50

Please sign in to comment.