From 5204904217b38308e91354c1a00178cccf3d212c Mon Sep 17 00:00:00 2001 From: Kailash Kejriwal <75155230+kailash360@users.noreply.github.com> Date: Tue, 7 May 2024 20:43:09 +0530 Subject: [PATCH] feat: added exception for reserved keywords (#100) * feat: added exception for reserved keywords Signed-off-by: Kailash Kejriwal * chore: added tests Signed-off-by: Kailash Kejriwal * feat: added ModelUtil method Signed-off-by: Kailash Kejriwal * Update lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js Co-authored-by: jonathan-casey <109082377+jonathan-casey@users.noreply.github.com> Signed-off-by: Dan Selman * Update lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js Co-authored-by: jonathan-casey <109082377+jonathan-casey@users.noreply.github.com> Signed-off-by: Dan Selman --------- Signed-off-by: Kailash Kejriwal Signed-off-by: Dan Selman Co-authored-by: Dan Selman Co-authored-by: jonathan-casey <109082377+jonathan-casey@users.noreply.github.com> --- .../fromJsonSchema/cto/jsonSchemaVisitor.js | 6 +++++- .../fromJsonSchema/cto/jsonSchemaVisitor.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js b/lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js index cabe2532..61a7db73 100644 --- a/lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js +++ b/lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js @@ -21,6 +21,7 @@ const draft7MetaSchema = require('ajv/dist/refs/json-schema-draft-07.json'); const addFormats = require('ajv-formats'); const getValue = require('get-value'); const { Identifiers } = require('@accordproject/concerto-util'); +const { ModelUtil } = require('@accordproject/concerto-core'); const { LocalReference, @@ -614,7 +615,10 @@ class JsonSchemaVisitor { }; // Handle reserved properties. - if (['$identifier', '$class', '$timestamp'].includes(propertyName)) { + if (ModelUtil.isPrivateSystemProperty(propertyName)) { + throw new Error(`${propertyName} is a reserved keyword and cannot be used as a property`); + } + if (ModelUtil.isSystemProperty(propertyName)) { return; } diff --git a/test/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js b/test/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js index 61b45a85..639cd7e2 100644 --- a/test/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js +++ b/test/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js @@ -92,6 +92,21 @@ describe('JsonSchemaVisitor', () => { ); }).should.throw('schema is invalid: data/properties/Foo/type must be equal to one of the allowed values, data/properties/Foo/type must be array, data/properties/Foo/type must match a schema in anyOf'); }); + it('should not generate when reserved keywords are used', async () => { + (function () { + const jsonSchemaModelClass = new JsonSchemaModel({ + $schema: 'http://json-schema.org/draft-07/schema#', + type: 'object', + properties: { + '$namespace': { type: 'integer' } + } + }); + + jsonSchemaModelClass.accept( + jsonSchemaVisitor, jsonSchemaVisitorParameters + ); + }).should.throw('$namespace is a reserved keyword and cannot be used as a property'); + }); it('should generate for a simple definition', async () => { const inferredConcertoJsonModel = JsonSchemaVisitor