Generated JSON Schema from Concerto CTO models is written by JSONSchemaVisitor (lib/codegen/fromcto/jsonschema/jsonschemavisitor.js). Today, npm test only checks snapshots, it does not run a JSON Schema validator on the output.
When you compile with Ajv (same library used in test/codegen/fromcto/jsonschema/jsonschemavisitor.js), two standard repo fixtures produce invalid schemas.
Bug 1: Empty enum in hr_base.cto
Cause
hr_base.cto defines an empty enum:
The visitor emits a JSON Schema enum: []. Ajv require at least one enum value, so compilation fails.
Reproduce
From the repo root (after npm ci):
node -e "
const fs = require('fs');
const Ajv = require('ajv');
const { ModelManager } = require('@accordproject/concerto-core');
const JSONSchemaVisitor = require('./lib/codegen/fromcto/jsonschema/jsonschemavisitor.js');
process.env.ENABLE_MAP_TYPE = 'true';
process.env.IMPORT_ALIASING = 'true';
const mm = new ModelManager();
mm.addCTOModel(
fs.readFileSync('./test/codegen/fromcto/data/model/hr_base.cto', 'utf8'),
'hr_base.cto'
);
const schema = mm.accept(new JSONSchemaVisitor(), {});
const ajv = new Ajv({ strict: false });
ajv.compile(schema);
console.log('OK');
"
Expected error
schema is invalid: data/definitions/org.acme.hr.base@1.0.0.Level/enum must NOT have fewer than 1 items
Bug 2: Ambiguous $ref in hr_base.cto + hr.cto
Cause
With both HR fixtures loaded (same pair as test/codegen/codegen.js), the generated schema contains a $ref that Ajv cannot resolve uniquely:
org.acme.hr@1.0.0.Person.nextOfKin
Reproduce
node -e "
const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');
const { ModelManager } = require('@accordproject/concerto-core');
const JSONSchemaVisitor = require('./lib/codegen/fromcto/jsonschema/jsonschemavisitor.js');
process.env.ENABLE_MAP_TYPE = 'true';
process.env.IMPORT_ALIASING = 'true';
const MODEL_DIR = './test/codegen/fromcto/data/model';
const mm = new ModelManager();
mm.addCTOModel(fs.readFileSync(path.join(MODEL_DIR, 'hr_base.cto'), 'utf8'), 'hr_base.cto');
mm.addCTOModel(fs.readFileSync(path.join(MODEL_DIR, 'hr.cto'), 'utf8'), 'hr.cto');
const schema = mm.accept(new JSONSchemaVisitor(), {
showCompositionRelationships: true,
});
const ajv = new Ajv({ strict: false });
ajv.compile(schema);
console.log('OK');
"
Expected error
reference "org.acme.hr@1.0.0.Person.nextOfKin" resolves to more than one schema
Related code
| File |
Role |
lib/codegen/fromcto/jsonschema/jsonschemavisitor.js |
Emits JSON Schema |
test/codegen/codegen.js |
Snapshot tests (no Ajv on output) |
test/codegen/fromcto/jsonschema/jsonschemavisitor.js |
Unit tests with Ajv on instances, not full HR fixtures |
Generated JSON Schema from Concerto CTO models is written by
JSONSchemaVisitor(lib/codegen/fromcto/jsonschema/jsonschemavisitor.js). Today,npm testonly checks snapshots, it does not run a JSON Schema validator on the output.When you compile with Ajv (same library used in
test/codegen/fromcto/jsonschema/jsonschemavisitor.js), two standard repo fixtures produce invalid schemas.Bug 1: Empty enum in
hr_base.ctoCause
hr_base.ctodefines an empty enum:The visitor emits a JSON Schema
enum: []. Ajv require at least one enum value, so compilation fails.Reproduce
From the repo root (after
npm ci):Expected error
Bug 2: Ambiguous
$refinhr_base.cto+hr.ctoCause
With both HR fixtures loaded (same pair as
test/codegen/codegen.js), the generated schema contains a$refthat Ajv cannot resolve uniquely:org.acme.hr@1.0.0.Person.nextOfKinReproduce
Expected error
Related code
lib/codegen/fromcto/jsonschema/jsonschemavisitor.jstest/codegen/codegen.jstest/codegen/fromcto/jsonschema/jsonschemavisitor.js