Skip to content

fix(codegen): update Level enum to include NONE value and adjust JSON…#243

Open
apoorv7g wants to merge 2 commits into
accordproject:mainfrom
apoorv7g:fork-main
Open

fix(codegen): update Level enum to include NONE value and adjust JSON…#243
apoorv7g wants to merge 2 commits into
accordproject:mainfrom
apoorv7g:fork-main

Conversation

@apoorv7g
Copy link
Copy Markdown
Contributor

@apoorv7g apoorv7g commented Jun 4, 2026

fix(jsonschema): make HR fixture JSON Schema pass Ajv validation

Closes #240

Fixes JSON Schema verification failures for the canonical HR fixtures (hr_base.cto and hr_base.cto + hr.cto). Generated schemas from JSONSchemaVisitor now compile with Ajv, matching the approach used for the TypeScript HR fixes in #237 / #241.

Related issue: accordproject/concerto-codegen#240

Changes

  • Added NONE to the empty Level enum in hr_base.cto so Ajv no longer rejects enum: []
  • Removed $id on inlined map field schemas in jsonschemavisitor.js so inherited fields (e.g. Person.nextOfKin on Employee, Contractor, Manager) do not share one id and break Ajv
  • Updated codegen snapshots for the Level enum and JSON Schema output
  • Enabled hr_base and hr_integration in test/verification/cases.js (removed pending skips)

Flags

  • Level.NONE is a minimal fixture placeholder for an enum that was empty; adjust values if product semantics need more levels
  • Map fields still emit the { schema: ... } wrapper for compatibility with existing JSONSchemaVisitor unit tests; only the duplicate $id was removed
  • Full verification: npm run mocha

Screenshots or Video

Before (Bug 1 - hr_base only):

schema is invalid: data/definitions/org.acme.hr.base@1.0.0.Level/enum must NOT have fewer than 1 items

Before (Bug 2 - hr_base + hr):

reference "org.acme.hr@1.0.0.Person.nextOfKin" resolves to more than one schema

After: both repro scripts below print OK.

Reproduce / verify locally

npm ci

Bug 1 - empty Level enum (hr_base only):

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');
"

Bug 2 - duplicate $id on Person.nextOfKin (hr_base + hr):

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');
"

Related Issues

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from fork:branchname

Copilot AI review requested due to automatic review settings June 4, 2026 03:07
… schema generation

- Added 'NONE' value to the Level enum in the model definition.
- Removed $id assignment for inlined map schemas to prevent conflicts in JSON schema generation.
- Updated related test snapshots to reflect changes in the Level enum.

Signed-off-by:  Apoorv <130035517+APOORV7G@users.noreply.github.com>
Signed-off-by: Apoorv <130035517+APOORV7G@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates code generation fixtures and tests to avoid Sinon stub leakage, and improves generated outputs by replacing an empty CTO enum with an explicit value while removing $id from inlined map schemas to prevent Ajv schema-ID collisions.

Changes:

  • Add test cleanup to restore Sinon sandbox between tests.
  • Define a concrete Level enum value (NONE) in the CTO model and refresh snapshots accordingly.
  • Stop emitting $id for inlined map schemas in JSON Schema generation to avoid Ajv conflicts.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
test/codegen/fromcto/typescript/typescriptvisitor.js Adds afterEach cleanup intended to prevent Sinon leakage across tests.
test/codegen/fromcto/data/model/hr_base.cto Replaces an empty enum with a concrete enum member (NONE) to drive consistent codegen.
lib/codegen/fromcto/jsonschema/jsonschemavisitor.js Removes $id from inlined map schemas to prevent Ajv $id collisions (notably with inherited fields).
test/codegen/snapshots/codegen.js.snap Updates snapshots to reflect the enum addition and $id removal in generated schemas.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +41 to +44
// Leaked Sinon stub doesn't break hr_integration tests
afterEach(() => {
sandbox.restore();
});
typescriptVisitor = new TypescriptVisitor();
mockFileWriter = sinon.createStubInstance(FileWriter);
});
// Leaked Sinon stub doesn't break hr_integration tests
Comment on lines 1037 to 1040
type Level int
const (
NONE Level = 1 + iota
)
…N schema generation

- Introduced tests to verify the correct handling of non-empty enums, ensuring they emit valid values and compile with Ajv.
- Added a test to confirm that schemas with empty enums fail Ajv compilation as expected.
- Updated the JSON schema visitor to accommodate these new test cases.

Signed-off-by: Apoorv <130035517+APOORV7G@users.noreply.github.com>
@apoorv7g
Copy link
Copy Markdown
Contributor Author

apoorv7g commented Jun 4, 2026

@mttrbrts Let me know if any more changes are required, If this gets merged I can create a new PR for verification of the generated code for

  • TypeScript
  • JSON

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants