From 9e008f206e5756f436ad082dee9819a32f51fd88 Mon Sep 17 00:00:00 2001 From: pechkas Date: Wed, 7 Oct 2026 02:22:09 +0530 Subject: [PATCH 1/2] fix(concerto-core): prevent TypeNotFoundException when serializing map with DateTime values Signed-off-by: pechkas --- .../src/serializer/jsongenerator.ts | 81 ++++++++++--------- .../test/serializer/jsongenerator.js | 28 +++++++ 2 files changed, 71 insertions(+), 38 deletions(-) diff --git a/packages/concerto-core/src/serializer/jsongenerator.ts b/packages/concerto-core/src/serializer/jsongenerator.ts index d4f051bb91..0433acfefe 100644 --- a/packages/concerto-core/src/serializer/jsongenerator.ts +++ b/packages/concerto-core/src/serializer/jsongenerator.ts @@ -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); @@ -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)) { 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); @@ -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 { @@ -156,7 +161,7 @@ class JSONGenerator { } result.$class = classDeclaration.getFullyQualifiedName(); - if(this.deduplicateResources && id) { + if (this.deduplicateResources && id) { result.$id = id; } @@ -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; + } } } diff --git a/packages/concerto-core/test/serializer/jsongenerator.js b/packages/concerto-core/test/serializer/jsongenerator.js index b0ca94cdac..42210db989 100644 --- a/packages/concerto-core/test/serializer/jsongenerator.js +++ b/packages/concerto-core/test/serializer/jsongenerator.js @@ -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; From 8618168afa003a17b518ad15ffbc51df11249639 Mon Sep 17 00:00:00 2001 From: pechkas Date: Thu, 8 Oct 2026 16:23:42 +0530 Subject: [PATCH 2/2] refactor(concerto-analysis): rename declaration comparer Signed-off-by: pechkas --- .../{class-declarations.ts => declaration.ts} | 13 ++++++++----- packages/concerto-analysis/src/comparers/index.ts | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) rename packages/concerto-analysis/src/comparers/{class-declarations.ts => declaration.ts} (81%) diff --git a/packages/concerto-analysis/src/comparers/class-declarations.ts b/packages/concerto-analysis/src/comparers/declaration.ts similarity index 81% rename from packages/concerto-analysis/src/comparers/class-declarations.ts rename to packages/concerto-analysis/src/comparers/declaration.ts index 9da52b59bd..8f74c4ede8 100644 --- a/packages/concerto-analysis/src/comparers/class-declarations.ts +++ b/packages/concerto-analysis/src/comparers/declaration.ts @@ -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; @@ -30,7 +29,7 @@ const classDeclarationAdded: ComparerFactory = (context) => ({ } }); -const classDeclarationRemoved: ComparerFactory = (context) => ({ +const declarationRemoved: ComparerFactory = (context) => ({ compareClassDeclaration: (a, b) => { if (!a || b) { return; @@ -44,7 +43,7 @@ const classDeclarationRemoved: ComparerFactory = (context) => ({ } }); -const classDeclarationTypeChanged: ComparerFactory = (context) => ({ +const declarationTypeChanged: ComparerFactory = (context) => ({ compareClassDeclaration: (a, b) => { if (!a || !b) { return; @@ -62,4 +61,8 @@ const classDeclarationTypeChanged: ComparerFactory = (context) => ({ } }); -export const classDeclarationComparerFactories = [classDeclarationAdded, classDeclarationRemoved, classDeclarationTypeChanged]; +export const declarationComparerFactories = [ + declarationAdded, + declarationRemoved, + declarationTypeChanged +]; \ No newline at end of file diff --git a/packages/concerto-analysis/src/comparers/index.ts b/packages/concerto-analysis/src/comparers/index.ts index cb0a8b0048..0c367707bc 100644 --- a/packages/concerto-analysis/src/comparers/index.ts +++ b/packages/concerto-analysis/src/comparers/index.ts @@ -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,