Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added exception for reserved keywords #100

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ const {
JsonSchemaModel,
} = require('./jsonSchemaClasses');

// Reserved keywords used in Concerto
const privateReservedProperties = [
'$classDeclaration',
'$namespace',
'$type',
'$modelManager',
'$validator',
'$identifierFieldName',
'$imports',
'$superTypes',
'$id',
];

const assignableReservedProperties = [
'$identifier',
'$timestamp'
];

const reservedProperties = [
'$class',
...assignableReservedProperties,
...privateReservedProperties
];

/**
* Convert the contents of a JSON Schema file to a Concerto JSON model.
* Set the following parameters to use:
Expand Down Expand Up @@ -614,7 +638,10 @@ class JsonSchemaVisitor {
};

// Handle reserved properties.
if (['$identifier', '$class', '$timestamp'].includes(propertyName)) {
if (privateReservedProperties.includes(propertyName)) {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for your contribution @kailash360 :). We already have a utility function for the check.
Please use this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, I have replaced them with the ModelUtil methods.

throw(new Error(`${propertyName} is a reserved keyword and cannot be used as a property`));
}
if(reservedProperties.includes(propertyName)) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions test/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down