Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import { getDeclarationType } from '../compare-utils';
import { ComparerFactory } from '../comparer';

// todo rename to declaration.ts , rename all classDeclaration -> declaration
const classDeclarationAdded: ComparerFactory = (context) => ({
const declarationAdded: ComparerFactory = (context) => ({
compareClassDeclaration: (a, b) => {
if (a || !b) {
return;
Expand All @@ -30,7 +29,7 @@ const classDeclarationAdded: ComparerFactory = (context) => ({
}
});

const classDeclarationRemoved: ComparerFactory = (context) => ({
const declarationRemoved: ComparerFactory = (context) => ({
compareClassDeclaration: (a, b) => {
if (!a || b) {
return;
Expand All @@ -44,7 +43,7 @@ const classDeclarationRemoved: ComparerFactory = (context) => ({
}
});

const classDeclarationTypeChanged: ComparerFactory = (context) => ({
const declarationTypeChanged: ComparerFactory = (context) => ({
compareClassDeclaration: (a, b) => {
if (!a || !b) {
return;
Expand All @@ -62,4 +61,8 @@ const classDeclarationTypeChanged: ComparerFactory = (context) => ({
}
});

export const classDeclarationComparerFactories = [classDeclarationAdded, classDeclarationRemoved, classDeclarationTypeChanged];
export const declarationComparerFactories = [
declarationAdded,
declarationRemoved,
declarationTypeChanged
];
4 changes: 2 additions & 2 deletions packages/concerto-analysis/src/comparers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
* limitations under the License.
*/

import { classDeclarationComparerFactories } from './class-declarations';
import { declarationComparerFactories } from './declaration';
import { modelFileComparerFactories } from './model-files';
import { propertyComparerFactories } from './properties';
import { mapDeclarationComparerFactories } from './map-declarations';
import { scalarDeclarationComparerFactories } from './scalar-declarations';

export const comparerFactories = [
...classDeclarationComparerFactories,
...declarationComparerFactories,
...propertyComparerFactories,
...modelFileComparerFactories,
...mapDeclarationComparerFactories,
Expand Down
81 changes: 43 additions & 38 deletions packages/concerto-core/src/serializer/jsongenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class JSONGenerator {
return this.visitClassDeclaration(thing, parameters);
} else if (thing.isRelationship?.()) {
return this.visitRelationshipDeclaration(thing, parameters);
}else if (thing.isMapDeclaration?.()) {
} else if (thing.isMapDeclaration?.()) {
return this.visitMapDeclaration(thing, parameters);
} else if (thing.isTypeScalar?.()) {
return this.visitField(thing.getScalarField(), parameters);
Expand All @@ -95,31 +95,36 @@ class JSONGenerator {
obj.forEach((value, key) => {

// don't serialize System Properties, other than $class
if(ModelUtil.isSystemProperty(key)) {
if (ModelUtil.isSystemProperty(key)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should-fix · correctness

This PR is a rename in concerto-analysis, but it also changes map-value serialisation here, plus whitespace-only edits in this file and its test. Those changes come from #1547. In this branch visitMapDeclaration no longer has the if (mapDeclaration.getValue().isRelationship()) { value = this.processRelationship(...) } branch that main has. If this file is taken as it stands, or the current conflict is resolved in the PR's favour, relationship handling for map values would silently be lost. Mixing the changes in also means the rename can't be reviewed as a no-behaviour-change refactor. Suggested fix: rebase on main and remove every change under packages/concerto-core, so the PR only touches packages/concerto-analysis. The DateTime-in-map fix can go through #1547.

return;
}

// Key is always a string, but value might be a ValidatedResource.
if (typeof value === 'object') {
// 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);
if (!Util.isNull(value) && typeof value === 'object') {
const mapValueDecl = mapDeclaration.getValue();

// convert declaration to JSON representation
parameters.stack.push(value);
const jsonValue = decl.accept(this, parameters);
if (ModelUtil.isPrimitiveType(mapValueDecl.getType())) {
value = this.convertToJSON(mapValueDecl, value);
} else {
// 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(mapValueDecl.getType());
const decl = modelFile.getModelManager().getType(valueType);

value = jsonValue;
// convert declaration to JSON representation
parameters.stack.push(value);
const jsonValue = decl.accept(this, parameters);
value = jsonValue;
}
}

map.set(key, value);
Expand Down Expand Up @@ -147,7 +152,7 @@ class JSONGenerator {

if (obj.isIdentifiable() && this.deduplicateResources) {
id = obj.toURI();
if( parameters.dedupeResources.has(id)) {
if (parameters.dedupeResources.has(id)) {
return id;
}
else {
Expand All @@ -156,7 +161,7 @@ class JSONGenerator {
}

result.$class = classDeclaration.getFullyQualifiedName();
if(this.deduplicateResources && id) {
if (this.deduplicateResources && id) {
result.$id = id;
}

Expand Down Expand Up @@ -225,22 +230,22 @@ class JSONGenerator {
*/
convertToJSON(field, obj) {
switch (field.getType()) {
case 'DateTime':
{
const objWithOffset = obj.utc().utcOffset(this.utcOffset);
const inZ = objWithOffset.utcOffset() === 0;
return objWithOffset.format(`YYYY-MM-DDTHH:mm:ss.SSS${inZ ? '[Z]': 'Z'}`);
}
case 'Integer':
case 'Long': {
return obj;
}
case 'Double':
case 'Boolean':
default:
{
return obj;
}
case 'DateTime':
{
const objWithOffset = obj.utc().utcOffset(this.utcOffset);
const inZ = objWithOffset.utcOffset() === 0;
return objWithOffset.format(`YYYY-MM-DDTHH:mm:ss.SSS${inZ ? '[Z]' : 'Z'}`);
}
case 'Integer':
case 'Long': {
return obj;
}
case 'Double':
case 'Boolean':
default:
{
return obj;
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/concerto-core/test/serializer/jsongenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,34 @@ describe('JSONGenerator', () => {

});

describe('#visitMapDeclaration', () => {
it('should serialize maps containing DateTime values without throwing TypeNotFoundException', () => {
const mm = new ModelManager();
mm.addCTOModel(`namespace org.example.schedule@1.0.0
map ScheduleMap {
o String
o DateTime
}
concept Calendar {
o ScheduleMap events
}`);

const localFactory = new Factory(mm);
const { Serializer } = require('../../src/serializer');
const serializer = new Serializer(localFactory, mm);

const calendar = localFactory.newConcept('org.example.schedule@1.0.0', 'Calendar');
const events = new Map();

events.set('kickoff', dayjs.utc('2025-01-01T12:00:00.000Z'));
calendar.events = events;

const json = serializer.toJSON(calendar);

json.events.kickoff.should.equal('2025-01-01T12:00:00.000Z');
});
});

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

let isEnumStub;
Expand Down