From 04b9926d2de73e110cfbbf9245400c1f9d4aa490 Mon Sep 17 00:00:00 2001 From: Shachi Date: Sat, 15 Jul 2017 21:09:40 +0530 Subject: [PATCH] fix of duplicate namespaces --- packages/composer-common/lib/modelmanager.js | 29 +++++++++++--- .../test/codegen/loopbackvisitor.js | 4 +- .../test/introspect/classdeclaration.js | 2 +- packages/composer-common/test/modelmanager.js | 38 ++++++++++++++++++- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/packages/composer-common/lib/modelmanager.js b/packages/composer-common/lib/modelmanager.js index e8bfcf31be..b99137eba6 100644 --- a/packages/composer-common/lib/modelmanager.js +++ b/packages/composer-common/lib/modelmanager.js @@ -152,8 +152,13 @@ class ModelManager { throw new Error('Cannot add a model file with the reserved system namspace: ' + m.getNamespace() ); } - m.validate(); - this.modelFiles[m.getNamespace()] = m; + if (!this.modelFiles[m.getNamespace()]) { + m.validate(); + this.modelFiles[m.getNamespace()] = m; + } else { + throw new Error('namespace already exists'); + } + return m; } @@ -239,14 +244,26 @@ class ModelManager { if (m.isSystemModelFile()){ throw new Error('System namespace can not be updated'); } - this.modelFiles[m.getNamespace()] = m; - newModelFiles.push(m); + + if (!this.modelFiles[m.getNamespace()]) { + this.modelFiles[m.getNamespace()] = m; + newModelFiles.push(m); + } + else { + throw new Error('namespace already exists'); + } } else { if (modelFile.isSystemModelFile()){ throw new Error('System namespace can not be updated'); } - this.modelFiles[modelFile.getNamespace()] = modelFile; - newModelFiles.push(modelFile); + + if (!this.modelFiles[modelFile.getNamespace()]) { + this.modelFiles[modelFile.getNamespace()] = modelFile; + newModelFiles.push(modelFile); + } + else { + throw new Error('namespace already exists'); + } } } diff --git a/packages/composer-common/test/codegen/loopbackvisitor.js b/packages/composer-common/test/codegen/loopbackvisitor.js index 30ca841d2c..695beb76be 100644 --- a/packages/composer-common/test/codegen/loopbackvisitor.js +++ b/packages/composer-common/test/codegen/loopbackvisitor.js @@ -892,7 +892,7 @@ describe('LoopbackVisitor', () => { }); it('should use the model file of the referencing type to resolve enumeration types', () => { - modelManager.addModelFile(` + modelManager.updateModelFile(` namespace org.acme.base enum Enum { o SOME_VALUE @@ -995,7 +995,7 @@ describe('LoopbackVisitor', () => { }); it('should use the model file of the referencing type to resolve other types', () => { - modelManager.addModelFile(` + modelManager.updateModelFile(` namespace org.acme.base asset MyInlineAsset identified by assetId { o String assetId diff --git a/packages/composer-common/test/introspect/classdeclaration.js b/packages/composer-common/test/introspect/classdeclaration.js index 69e2f102a3..cddd989b19 100644 --- a/packages/composer-common/test/introspect/classdeclaration.js +++ b/packages/composer-common/test/introspect/classdeclaration.js @@ -55,7 +55,7 @@ describe('ClassDeclaration', () => { const modelFile = new ModelFile(modelManager, modelDefinitions); modelFiles.push(modelFile); } - modelManager.addModelFiles(modelFiles, modelFileNames); + //modelManager.addModelFiles(modelFiles, modelFileNames); return modelFiles; }; diff --git a/packages/composer-common/test/modelmanager.js b/packages/composer-common/test/modelmanager.js index eecf169aed..2bbdc4863f 100644 --- a/packages/composer-common/test/modelmanager.js +++ b/packages/composer-common/test/modelmanager.js @@ -126,6 +126,24 @@ describe('ModelManager', () => { }).should.throw(); }); + it('should return error for duplicate namespaces for a string', () => { + modelManager.addModelFile(modelBase); + let mf1 = sinon.createStubInstance(ModelFile); + mf1.getNamespace.returns('org.acme.base'); + (() => { + modelManager.addModelFile(modelBase); + }).should.throw(/namespace already exists/); + }); + + it('should return error for duplicate namespaces from an object', () => { + modelManager.addModelFile(modelBase); + let mf1 = sinon.createStubInstance(ModelFile); + mf1.getNamespace.returns('org.acme.base'); + (() => { + modelManager.addModelFile(mf1); + }).should.throw(/namespace already exists/); + }); + }); describe('#addModelFiles', () => { @@ -157,7 +175,7 @@ describe('ModelManager', () => { it('should add to existing model files from strings', () => { modelManager.addModelFile(modelBase); modelManager.getModelFile('org.acme.base').getNamespace().should.equal('org.acme.base'); - modelManager.addModelFiles([modelBase, concertoModel, farm2fork]); + modelManager.addModelFiles([concertoModel, farm2fork]); modelManager.getModelFile('org.acme.base').getNamespace().should.equal('org.acme.base'); modelManager.getModelFile('org.acme').getNamespace().should.equal('org.acme'); }); @@ -215,6 +233,24 @@ describe('ModelManager', () => { modelManager.addModelFiles([mockSystemModelFile]); }).should.throw(); }); + + it('should return an error for duplicate namespace from strings', () => { + (() => { + modelManager.addModelFiles([concertoModel, modelBase, farm2fork, modelBase]); + }).should.throw(/namespace already exists/); + }); + + it('should return an error for duplicate namespace from objects', () => { + let mf1 = sinon.createStubInstance(ModelFile); + mf1.getNamespace.returns('org.doge'); + let mf2 = sinon.createStubInstance(ModelFile); + mf2.getNamespace.returns('org.doge.base'); + modelManager.addModelFiles([mf1,mf2]); + (() => { + modelManager.addModelFiles([mf1]); + }).should.throw(/namespace already exists/); + }); + }); describe('#updateModelFile', () => {