Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsonpopulator) Updated processMapType to handle nested abstract c… #827

Closed
Closed
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
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 @@ -105,7 +105,7 @@
this.strictQualifiedDateTimes = strictQualifiedDateTimes;

if (process.env.TZ){
console.warn(`Environment variable 'TZ' is set to '${process.env.TZ}', this can cause unexpected behaviour when using unqualified date time formats.`);

Check warning on line 108 in packages/concerto-core/lib/serializer/jsonpopulator.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x, ubuntu-latest)

Unexpected console statement

Check warning on line 108 in packages/concerto-core/lib/serializer/jsonpopulator.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x, ubuntu-latest)

Unexpected console statement

Check warning on line 108 in packages/concerto-core/lib/serializer/jsonpopulator.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x, windows-latest)

Unexpected console statement

Check warning on line 108 in packages/concerto-core/lib/serializer/jsonpopulator.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x, macOS-latest)

Unexpected console statement

Check warning on line 108 in packages/concerto-core/lib/serializer/jsonpopulator.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x, macOS-latest)

Unexpected console statement

Check warning on line 108 in packages/concerto-core/lib/serializer/jsonpopulator.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x, windows-latest)

Unexpected console statement
}
}

Expand Down Expand Up @@ -211,9 +211,18 @@
* @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
Loading