Skip to content

bug: JSON Schema verification bugs (generated output fails Ajv) #240

@apoorv7g

Description

@apoorv7g

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:

enum Level {}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions