Skip to content
Merged
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
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
# Don't block the PR when the Coveralls webhook is unavailable (e.g.
# 5xx/timeout). Coverage is still uploaded per-matrix above, which also
# uses fail-on-error: false.
fail-on-error: false
16 changes: 13 additions & 3 deletions packages/concerto-core/src/serializer/jsongenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,19 @@ class JSONGenerator {

// Key is always a string, but value might be a ValidatedResource.
if (typeof value === 'object') {
let decl = mapDeclaration.getModelFile()
.getAllDeclarations()
.find(decl => decl.name === value.getType());
// Resolve the declaration for the map value. Prefer the instance's
// own fully-qualified type so that polymorphic values (subclasses of
// the map's declared value type) are serialized using their actual
// declaration. Fall back to the map's declared value type - honouring
// imports - for instances that do not expose a fully-qualified type
// (e.g. those created by the populator). Either way the value concept
// may live in another namespace, so resolve it via the model manager
// rather than the map's own model file.
const modelFile = mapDeclaration.getModelFile();
const valueType = typeof value.getFullyQualifiedType === 'function'
? value.getFullyQualifiedType()
: modelFile.getFullyQualifiedTypeName(mapDeclaration.getValue().getType());
const decl = modelFile.getModelManager().getType(valueType);

// convert declaration to JSON representation
parameters.stack.push(value);
Expand Down
27 changes: 17 additions & 10 deletions packages/concerto-core/src/serializer/jsonpopulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,23 @@ class JSONPopulator {
*/
processMapType(mapDeclaration, parameters: JsonPopulatorParameters, value, 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);
// The map's key/value concept may be imported from another namespace, so it
// is not necessarily present in the map's own model file. Resolve it via the
// model manager (which spans all loaded models). A failed lookup means the
// type is a scalar/primitive and the raw value is returned as-is, preserving
// the previous "not found => treat as scalar" behaviour.
const modelFile = mapDeclaration.getModelFile();
const modelManager = modelFile.getModelManager();
try {
if (value && typeof value === 'object' && value.$class) {
// The $class property is already fully-qualified.
decl = modelManager.getType(value.$class);
} else {
// Resolve the declared type honouring the map model file's imports.
decl = modelManager.getType(modelFile.getFullyQualifiedTypeName(type));
}
} catch (err) {
decl = undefined;
}

// if its a ClassDeclaration, populate the Concept.
Expand Down
6 changes: 4 additions & 2 deletions packages/concerto-core/test/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,10 @@ concept Bar {
}`, 'internal.cto', true);
modelManager.getModelFile('org.acme@1.0.0').should.not.be.null;

// import all external models
return modelManager.updateExternalModels().should.be.rejectedWith(Error, 'Failed to load model file. Job: github://external.cto Details: Error: HTTP request failed with status: 400');
// import all external models. The exact HTTP status returned by GitHub
// for the bad URL can vary (e.g. 400 vs 404), so assert on the stable
// failure message rather than pinning a specific status code.
return modelManager.updateExternalModels().should.be.rejectedWith(Error, /Failed to load model file\. Job: github:\/\/external\.cto Details: Error: HTTP request failed with status: \d+/);
});

it('should fail using bad protocol and default model file loader', () => {
Expand Down
77 changes: 77 additions & 0 deletions packages/concerto-core/test/serializer/maptype/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,5 +1027,82 @@ describe('Serializer', () => {
}).should.throw('Unexpected properties for type org.acme.sample@1.0.0.Concepts: stateLog');
});
});

describe('# cross-namespace concept values in maps', () => {
let xnsModelManager;
let xnsFactory;
let xnsSerializer;

beforeEach(() => {
xnsModelManager = new ModelManager();
Util.addComposerModel(xnsModelManager);

// the value concept lives in its own namespace
xnsModelManager.addCTOModel(`
namespace org.acme.unit@1.0.0

concept MonetaryUnit {
o String code
o Integer scale
}
`, 'unit.cto');

// the map (and its container) live in a different namespace and
// import the value concept
xnsModelManager.addCTOModel(`
namespace org.acme.registry@1.0.0

import org.acme.unit@1.0.0.{MonetaryUnit}

map UnitMap {
o String
o MonetaryUnit
}

concept Registry {
o UnitMap units optional
}
`, 'registry.cto');

xnsFactory = new Factory(xnsModelManager);
xnsSerializer = new Serializer(xnsFactory, xnsModelManager);
});

it('should serialize -> deserialize a Map whose value concept is imported from another namespace', () => {
const registry = xnsFactory.newConcept('org.acme.registry@1.0.0', 'Registry');

const usd = xnsFactory.newConcept('org.acme.unit@1.0.0', 'MonetaryUnit');
usd.code = 'USD';
usd.scale = 2;
const jpy = xnsFactory.newConcept('org.acme.unit@1.0.0', 'MonetaryUnit');
jpy.code = 'JPY';
jpy.scale = 0;

registry.units = new Map();
registry.units.set('USD', usd);
registry.units.set('JPY', jpy);

// previously threw: TypeError: Cannot read properties of undefined (reading 'accept')
// because the value declaration was looked up only in the map's own model file.
const json = xnsSerializer.toJSON(registry);

json.should.deep.equal({
$class: 'org.acme.registry@1.0.0.Registry',
units: {
USD: { $class: 'org.acme.unit@1.0.0.MonetaryUnit', code: 'USD', scale: 2 },
JPY: { $class: 'org.acme.unit@1.0.0.MonetaryUnit', code: 'JPY', scale: 0 }
}
});

const resource = xnsSerializer.fromJSON(json);
resource.units.should.be.an.instanceOf(Map);
resource.units.get('USD').code.should.equal('USD');
resource.units.get('USD').scale.should.equal(2);
resource.units.get('JPY').scale.should.equal(0);

// round-trips back to the same JSON
xnsSerializer.toJSON(resource).should.deep.equal(json);
});
});
});

Loading