From be0eeae3df083bc972b7b7a17522f0d8d3cb8582 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Wed, 28 Apr 2021 11:22:38 +0200 Subject: [PATCH] fix: ensure that required list of properties are merged (#159) --- src/models/CommonModel.ts | 9 +++--- test/models/CommonModel.spec.ts | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/models/CommonModel.ts b/src/models/CommonModel.ts index 8f64f20cd5..bb04798eee 100644 --- a/src/models/CommonModel.ts +++ b/src/models/CommonModel.ts @@ -133,11 +133,10 @@ export class CommonModel extends CommonSchema { CommonModel.mergeTypes(mergeTo, mergeFrom); if (mergeFrom.enum !== undefined) { - if (mergeTo.enum === undefined) { - mergeTo.enum = mergeFrom.enum; - } else { - mergeTo.enum = [...mergeTo.enum, ...mergeFrom.enum]; - } + mergeTo.enum = [... new Set([...(mergeTo.enum || []), ...mergeFrom.enum])]; + } + if (mergeFrom.required !== undefined) { + mergeTo.required = [... new Set([...(mergeTo.required || []), ...mergeFrom.required])]; } // Which values are correct to use here? Is allOf required? diff --git a/test/models/CommonModel.spec.ts b/test/models/CommonModel.spec.ts index 0619cd2a73..61e3c4fce9 100644 --- a/test/models/CommonModel.spec.ts +++ b/test/models/CommonModel.spec.ts @@ -180,6 +180,49 @@ describe('CommonModel', function() { expect(doc1.$id).toBeUndefined(); }); }); + describe('required', function() { + test('should contain the same if right side is not defined', function() { + const doc: Schema = { }; + let doc1 = CommonModel.toCommonModel(doc); + let doc2 = CommonModel.toCommonModel(doc); + doc1.required = ["test"]; + doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); + expect(doc1.required).toEqual(["test"]); + }); + test('should be merged when only right side is defined', function() { + const doc: Schema = { }; + let doc1 = CommonModel.toCommonModel(doc); + let doc2 = CommonModel.toCommonModel(doc); + doc2.required = ["test"]; + doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); + expect(doc1.required).toEqual(doc2.required); + }); + test('should be merged when both sides are defined', function() { + const doc: Schema = { }; + let doc1 = CommonModel.toCommonModel(doc); + let doc2 = CommonModel.toCommonModel(doc); + doc1.required = ["test"]; + doc2.required = ["test2"]; + doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); + expect(doc1.required).toEqual(["test", "test2"]); + }); + test('should only contain one if duplicate', function() { + const doc: Schema = { }; + let doc1 = CommonModel.toCommonModel(doc); + let doc2 = CommonModel.toCommonModel(doc); + doc1.required = ["test"]; + doc2.required = ["test"]; + doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); + expect(doc1.required).toEqual(["test"]); + }); + test('should not change if nothing is defined', function() { + const doc: Schema = { }; + let doc1 = CommonModel.toCommonModel(doc); + let doc2 = CommonModel.toCommonModel(doc); + doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); + expect(doc1.required).toBeUndefined(); + }); + }); describe('$ref', function() { test('should be merged when only right side is defined', function() { const doc: Schema = { }; @@ -267,6 +310,15 @@ describe('CommonModel', function() { doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); expect(doc1.enum).toEqual(doc2.enum); }); + test('Should not contain duplicate values', function() { + const doc: Schema = { }; + let doc1 = CommonModel.toCommonModel(doc); + let doc2 = CommonModel.toCommonModel(doc); + doc2.enum = ["string"]; + doc1.enum = ["string"]; + doc1 = CommonModel.mergeCommonModels(doc1, doc2, doc); + expect(doc1.enum).toEqual(["string"]); + }); test('should be merged when both sides are defined', function() { const doc: Schema = { }; let doc1 = CommonModel.toCommonModel(doc);