Skip to content

Commit

Permalink
fix: ensure that required list of properties are merged (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Apr 28, 2021
1 parent f52a770 commit be0eeae
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/models/CommonModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ export class CommonModel extends CommonSchema<CommonModel> {
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?
Expand Down
52 changes: 52 additions & 0 deletions test/models/CommonModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { };
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit be0eeae

Please sign in to comment.