From 7e2537914964072e6e86ea6eeaccb79dec43a61b Mon Sep 17 00:00:00 2001 From: Tushar Singh Date: Wed, 7 Oct 2026 01:26:10 +0530 Subject: [PATCH] fix(concerto-core): serialize Map DateTime values without throwing TypeNotFoundException (#1546) Closes #1546 Signed-off-by: Tushar Singh --- .../src/serializer/jsongenerator.ts | 68 ++++++++++++------- .../src/serializer/resourcevalidator.ts | 7 +- .../test/serializer/maptype/serializer.js | 66 ++++++++++++++++++ 3 files changed, 116 insertions(+), 25 deletions(-) diff --git a/packages/concerto-core/src/serializer/jsongenerator.ts b/packages/concerto-core/src/serializer/jsongenerator.ts index c1939b214..40f978084 100644 --- a/packages/concerto-core/src/serializer/jsongenerator.ts +++ b/packages/concerto-core/src/serializer/jsongenerator.ts @@ -16,6 +16,7 @@ import Resource from '../model/resource'; import Typed from '../model/typed'; import ModelUtil from '../modelutil'; import { NullUtil as Util } from '@accordproject/concerto-util'; +import dayjs from '../dayjs-setup'; /** * Converts the contents of a Resource to JSON. The parameters @@ -68,7 +69,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); @@ -102,26 +103,36 @@ class JSONGenerator { if (mapDeclaration.getValue().isRelationship()) { value = this.processRelationship(mapDeclaration.getValue(), value, parameters); - } else 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); + } else if (!Util.isNull(value)) { + if (typeof value === 'object' && typeof value.isBefore === 'function') { + value = this.formatDateTime(value); + } else if (typeof value === 'object') { + const modelFile = mapDeclaration.getModelFile(); + const isPrimitive = ModelUtil.isPrimitiveType(mapDeclaration.getValue().getType()); + const isScalar = ModelUtil.isScalar(mapDeclaration.getValue()); + if (!isPrimitive && !isScalar) { + // 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 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); - const jsonValue = decl.accept(this, parameters); + if (decl && !decl.isScalarDeclaration?.()) { + // convert declaration to JSON representation + parameters.stack.push(value); + const jsonValue = decl.accept(this, parameters); - value = jsonValue; + value = jsonValue; + } + } + } } map.set(key, value); @@ -218,6 +229,19 @@ class JSONGenerator { return result; } + /** + * Formats a DateTime object to an ISO-8601 string respecting utcOffset. + * @param {Object} obj - the dayjs instance to format + * @return {string} the formatted DateTime string + * @private + */ + formatDateTime(obj) { + const dayjsObj = typeof obj.utc === 'function' ? obj.utc() : dayjs.utc(obj); + const objWithOffset = dayjsObj.utcOffset(this.utcOffset); + const inZ = objWithOffset.utcOffset() === 0; + return objWithOffset.format(`YYYY-MM-DDTHH:mm:ss.SSS${inZ ? '[Z]' : 'Z'}`); + } + /** * Converts to JSON safe format. * @@ -229,9 +253,7 @@ class JSONGenerator { 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'}`); + return this.formatDateTime(obj); } case 'Integer': case 'Long': { @@ -323,4 +345,4 @@ class JSONGenerator { } export { JSONGenerator }; -export default JSONGenerator; +export default JSONGenerator; \ No newline at end of file diff --git a/packages/concerto-core/src/serializer/resourcevalidator.ts b/packages/concerto-core/src/serializer/resourcevalidator.ts index 091dd63bc..fc4b5545c 100644 --- a/packages/concerto-core/src/serializer/resourcevalidator.ts +++ b/packages/concerto-core/src/serializer/resourcevalidator.ts @@ -153,11 +153,14 @@ class ResourceValidator { throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of String but found '${value}' instead.`); } break; - case 'DateTime': - if (typeof value !== 'string' || !dayjs.utc(value).isValid()) { + case 'DateTime': { + const isDayJs = typeof value === 'object' && typeof value?.isBefore === 'function' && value.isValid(); + const isValidDateString = typeof value === 'string' && dayjs.utc(value).isValid(); + if (!isDayJs && !isValidDateString) { throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of DateTime but found '${value}' instead.`); } break; + } case 'Boolean': if (typeof value !== 'boolean') { throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of Boolean but found ${typeof value} instead, for value '${value}'.`); diff --git a/packages/concerto-core/test/serializer/maptype/serializer.js b/packages/concerto-core/test/serializer/maptype/serializer.js index 30bf3fb8f..d8d79081a 100644 --- a/packages/concerto-core/test/serializer/maptype/serializer.js +++ b/packages/concerto-core/test/serializer/maptype/serializer.js @@ -317,6 +317,72 @@ describe('Serializer', () => { resource.birthday.get('Alice').should.equal('2024-10-28T01:02:03Z'); }); + it('should serialize a map with dayjs DateTime values (#1546)', () => { + let concept = factory.newConcept('org.acme.sample@1.0.0', 'Concepts'); + + concept.birthday = new Map(); + concept.birthday.set('Bob', dayjs('2023-10-28T01:02:03.000Z')); + concept.birthday.set('Alice', dayjs('2024-10-28T01:02:03.000Z')); + + const json = serializer.toJSON(concept); + + json.should.deep.equal({ + $class: 'org.acme.sample@1.0.0.Concepts', + birthday: { + Bob: '2023-10-28T01:02:03.000Z', + Alice: '2024-10-28T01:02:03.000Z' + } + }); + }); + + it('should serialize a map with dayjs DateTime scalar values (#1546)', () => { + let concept = factory.newConcept('org.acme.sample@1.0.0', 'Concepts'); + + concept.celebration = new Map(); + concept.celebration.set('BobBirthday', dayjs('2022-11-28T01:02:03.000Z')); + + const json = serializer.toJSON(concept); + + json.should.deep.equal({ + $class: 'org.acme.sample@1.0.0.Concepts', + celebration: { + BobBirthday: '2022-11-28T01:02:03.000Z' + } + }); + }); + + it('should serialize a map with null value without throwing TypeNotFoundException (#1546)', () => { + let concept = factory.newConcept('org.acme.sample@1.0.0', 'Concepts'); + + concept.birthday = new Map(); + concept.birthday.set('Bob', null); + + const json = serializer.toJSON(concept, { validate: false }); + + json.should.deep.equal({ + $class: 'org.acme.sample@1.0.0.Concepts', + birthday: { + Bob: null + } + }); + }); + + it('should serialize a map with dayjs DateTime values respecting custom utcOffset (#1546)', () => { + let concept = factory.newConcept('org.acme.sample@1.0.0', 'Concepts'); + + concept.birthday = new Map(); + concept.birthday.set('Bob', dayjs('2023-10-28T01:02:03.000Z')); + + const json = serializer.toJSON(concept, { utcOffset: 330 }); + + json.should.deep.equal({ + $class: 'org.acme.sample@1.0.0.Concepts', + birthday: { + Bob: '2023-10-28T06:32:03.000+05:30' + } + }); + }); + it('should serialize -> deserialize with a Map ', () => { // setup let concept = factory.newConcept('org.acme.sample@1.0.0', 'Concepts');